Revision 1045

View differences:

org.gvsig.lrs/tags/org.gvsig.lrs-1.0.192/org.gvsig.lrs.lib/org.gvsig.lrs.lib.impl/src/main/resources/META-INF/services/org.gvsig.tools.library.Library
1
org.gvsig.lrs.lib.impl.DefaultLrsAlgorithmsLibrary
0 2

  
org.gvsig.lrs/tags/org.gvsig.lrs-1.0.192/org.gvsig.lrs.lib/org.gvsig.lrs.lib.impl/src/main/java/org/gvsig/lrs/lib/impl/DefaultLrsEditRouteCalibrationAlgorithmParams.java
1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2015 gvSIG Association
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.lrs.lib.impl;
24

  
25
import java.util.List;
26
import java.util.Map;
27
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
28
import org.gvsig.fmap.dal.feature.FeatureReference;
29
import org.gvsig.lrs.lib.api.LrsEditRouteCalibrationAlgorithmParams;
30

  
31

  
32

  
33
/**
34
 * @author dmartinez
35
 *
36
 */
37
public class DefaultLrsEditRouteCalibrationAlgorithmParams implements LrsEditRouteCalibrationAlgorithmParams {
38

  
39

  
40
    private FeatureAttributeDescriptor idRouteField;
41
    private String selectedRouteName;
42
    private final String NAME = "LrsEditRouteCalibrationAlgorithm";
43
    private final String DESCRIPTION = "Algorithm to edit route calibration with linear reference system.";
44
    private Map<FeatureReference, List<Double>> modifiedMCoordinates;
45

  
46

  
47
    /**
48
     *
49
     */
50
    public DefaultLrsEditRouteCalibrationAlgorithmParams() {
51

  
52
    }
53

  
54
    /* (non-Javadoc)
55
     * @see org.gvsig.lrs.lib.api.LrsAlgorithmParams#getName()
56
     */
57
    @Override
58
    public String getName() {
59
        return NAME;
60
    }
61

  
62
    /* (non-Javadoc)
63
     * @see org.gvsig.lrs.lib.api.LrsAlgorithmParams#getDescription()
64
     */
65
    @Override
66
    public String getDescription() {
67
        return DESCRIPTION;
68
    }
69

  
70

  
71
    @Override
72
    public FeatureAttributeDescriptor getIdRouteField() {
73
        return idRouteField;
74
    }
75

  
76

  
77
    @Override
78
    public void setIdRouteField(FeatureAttributeDescriptor idRouteField) {
79
        this.idRouteField = idRouteField;
80
    }
81

  
82

  
83
    @Override
84
    public String getSelectedRouteName() {
85
        return selectedRouteName;
86
    }
87

  
88

  
89
    @Override
90
    public void setSelectedRouteName(String selectedRouteName) {
91
        this.selectedRouteName = selectedRouteName;
92
    }
93

  
94

  
95
    @Override
96
    public Map<FeatureReference, List<Double>> getModifiedMCoordinates() {
97
        return this.modifiedMCoordinates;
98
    }
99

  
100
    @Override
101
    public void setModifiedMCoordinates(Map<FeatureReference, List<Double>> modifiedMCoordinates) {
102
        this.modifiedMCoordinates = modifiedMCoordinates;
103
    }
104

  
105

  
106
}
org.gvsig.lrs/tags/org.gvsig.lrs-1.0.192/org.gvsig.lrs.lib/org.gvsig.lrs.lib.impl/src/main/java/org/gvsig/lrs/lib/impl/LrsCalibrateRouteAlgorithm.java
1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2015 gvSIG Association
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.lrs.lib.impl;
24

  
25
import java.util.ArrayList;
26
import java.util.HashMap;
27
import java.util.List;
28
import java.util.Map;
29
import java.util.Map.Entry;
30

  
31
import org.cresques.cts.IProjection;
32
import org.slf4j.Logger;
33
import org.slf4j.LoggerFactory;
34

  
35
import org.gvsig.fmap.dal.feature.EditableFeature;
36
import org.gvsig.fmap.dal.feature.Feature;
37
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
38
import org.gvsig.fmap.dal.feature.FeatureSet;
39
import org.gvsig.fmap.dal.feature.FeatureStore;
40
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
41
import org.gvsig.fmap.geom.Geometry;
42
import org.gvsig.fmap.geom.GeometryLocator;
43
import org.gvsig.fmap.geom.GeometryManager;
44
import org.gvsig.fmap.geom.exception.CreateGeometryException;
45
import org.gvsig.fmap.geom.operation.GeometryOperationException;
46
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
47
import org.gvsig.fmap.geom.primitive.Line;
48
import org.gvsig.fmap.geom.primitive.Point;
49
import org.gvsig.lrs.lib.api.DistanceUnits;
50
import org.gvsig.lrs.lib.api.LrsAlgorithm;
51
import org.gvsig.lrs.lib.api.LrsAlgorithmParams;
52
import org.gvsig.lrs.lib.api.LrsCalibrateRouteAlgorithmParams;
53
import org.gvsig.lrs.lib.api.LrsCreateRouteAlgorithmParams;
54
import org.gvsig.lrs.lib.api.LrsMeasureCalculationMethods;
55
import org.gvsig.lrs.lib.api.MeasuresCalculator;
56
import org.gvsig.lrs.lib.api.exceptions.LrsCalibrateRouteException;
57
import org.gvsig.lrs.lib.api.exceptions.LrsException;
58
import org.gvsig.tools.ToolsLocator;
59
import org.gvsig.tools.exception.BaseException;
60
import org.gvsig.tools.i18n.I18nManager;
61
import org.gvsig.tools.service.Manager;
62
import org.gvsig.tools.task.SimpleTaskStatus;
63
import org.gvsig.tools.visitor.VisitCanceledException;
64
import org.gvsig.tools.visitor.Visitor;
65

  
66
/**
67
 * @author dmartinez
68
 *
69
 */
70
public class LrsCalibrateRouteAlgorithm implements LrsAlgorithm {
71

  
72
    private static final Logger logger = LoggerFactory.getLogger(LrsCalibrateRouteAlgorithm.class);
73

  
74
    private LrsCalibrateRouteAlgorithmParams parameters;
75

  
76
    /**
77
     *
78
     */
79
    public LrsCalibrateRouteAlgorithm(LrsCalibrateRouteAlgorithmParams parameters) {
80
        this.parameters = parameters;
81

  
82
    }
83

  
84
    /*
85
     * (non-Javadoc)
86
     *
87
     * @see org.gvsig.tools.service.Service#getManager()
88
     */
89
    public Manager getManager() {
90
        return null;
91
    }
92

  
93
    /*
94
     * (non-Javadoc)
95
     *
96
     * @see org.gvsig.lrs.lib.api.LrsAlgorithm#getParams()
97
     */
98
    public  LrsAlgorithmParams getParams() {
99
        return this.parameters;
100
    }
101

  
102
    /* (non-Javadoc)
103
     * @see org.gvsig.lrs.lib.api.LrsAlgorithm#setParams(org.gvsig.lrs.lib.api.LrsAlgorithmParams)
104
     */
105
    public void setParams(LrsAlgorithmParams params) throws IllegalArgumentException {
106
        if(!(params instanceof LrsCreateRouteAlgorithmParams)){
107
            throw new IllegalArgumentException("params should be LrsCalibrateRouteAlgorithmParams type.");
108
        }
109
        this.parameters = (LrsCalibrateRouteAlgorithmParams) params;
110
    }
111

  
112
    /*
113
     * (non-Javadoc)
114
     * @see org.gvsig.lrs.lib.api.LrsAlgorithm#execute(org.gvsig.tools.task.SimpleTaskStatus)
115
     */
116
    public void execute(final SimpleTaskStatus taskStatus) throws LrsException {
117
        NewFeatureStoreParameters newFeatureStoreParameters =
118
            parameters.getNewFeatureStoreParameters();
119
        FeatureStore sourceFeatureStore = parameters.getSourceFeatureStore();
120
        FeatureAttributeDescriptor idRouteField = parameters.getIdRouteField();
121
        FeatureStore calibratePointFeatureStore= parameters.getCalibratePointFeatureStore();
122
        FeatureAttributeDescriptor idRouteCalibratePointField =parameters.getCalibratePointIdRouteField();
123
        FeatureAttributeDescriptor fromMeasureField = parameters.getFromMeasureField();
124
        final boolean includeAll=parameters.includeAll();
125

  
126
        logger.info(parameters.toString());
127

  
128
        taskStatus.setTitle(parameters.getName());
129
        I18nManager i18nManager = ToolsLocator.getI18nManager();
130
        taskStatus.message(i18nManager.getTranslation("grouping_features"));
131

  
132
        try {
133
            final String routeFieldName = idRouteField.getName();
134
            final String routeCalibratePointFieldName=idRouteCalibratePointField.getName();
135
            final String measureFieldName=fromMeasureField.getName();
136
            final Double convertedSearchRadius=calculateConvertedSearchRadius();
137

  
138
            FeatureStore newFeatureStore = LrsAlgorithmUtils
139
                .createNewDataStore(newFeatureStoreParameters, idRouteField);
140

  
141
            FeatureSet sourceFeatures;
142
            if (sourceFeatureStore.getFeatureSelection().getSize() > 0) {
143
                sourceFeatures = sourceFeatureStore.getFeatureSelection();
144
            } else {
145
                sourceFeatures = sourceFeatureStore.getFeatureSet();
146
            }
147

  
148
            final Map<String, RouteAndPoints> featuresMap =
149
                new HashMap<String, RouteAndPoints>();
150
            sourceFeatures.accept(new Visitor() {
151

  
152
                public void visit(Object obj)
153
                    throws VisitCanceledException, BaseException {
154
                    Feature feature = (Feature) obj;
155
                    String routeName = (String) feature.get(routeFieldName);
156
                    RouteAndPoints routeAndPoints = new RouteAndPoints(feature.getDefaultGeometry());
157

  
158
                    featuresMap.put(routeName, routeAndPoints);
159
                }
160
            });
161

  
162
            FeatureSet pointFeatures=calibratePointFeatureStore.getFeatureSet();
163
            pointFeatures.accept(new Visitor() {
164

  
165
                public void visit(Object obj) throws VisitCanceledException, BaseException {
166
                    Feature feature = (Feature) obj;
167
                    String routeName = (String) feature.get(routeCalibratePointFieldName);
168
                    Double measure= (Double) feature.get(measureFieldName);
169
                    Geometry geomPoint=(Point)feature.getDefaultGeometry();
170

  
171
                    if (geomPoint instanceof Point){
172
                        Point point=(Point) geomPoint;
173
                        Object valueObject=featuresMap.get(routeName);
174
                        if (valueObject != null && valueObject instanceof RouteAndPoints) {
175
                            RouteAndPoints routeAndPoints = (RouteAndPoints) valueObject;
176
                            List<Point> points = routeAndPoints.getPoints();
177
                            if (feature.getDefaultGeometry() instanceof Point) {
178
                                Geometry route = routeAndPoints.getRoute();
179
                                if (isValidPoint(point, route, convertedSearchRadius)) {
180
                                    try {
181
                                        Point mPoint = getInRouteMPoint(route, point, measure);
182
                                        points.add(mPoint);
183
                                    } catch (Exception e) {
184
                                        logger.debug("Error adding point", e);
185
                                    }
186
                                }
187
                            }
188
                        }
189
                    }
190
                }
191
            });
192

  
193
            taskStatus.setRangeOfValues(0, featuresMap.size());
194
            int taskCount = 0;
195

  
196
            newFeatureStore.edit(FeatureStore.MODE_FULLEDIT);
197

  
198
            for (Entry<String, RouteAndPoints> entry : featuresMap.entrySet()) {
199
                String routeName = entry.getKey();
200
                Object valueObject = entry.getValue();
201
                List<Point> points=null;
202
                Geometry geometry=null;
203

  
204
                if (valueObject!=null && valueObject instanceof RouteAndPoints){
205
                    RouteAndPoints routeAndPoints=(RouteAndPoints)valueObject;
206
                        geometry=routeAndPoints.getRoute();
207
                        points=routeAndPoints.getPoints();
208
                }
209
                if (!points.isEmpty()){
210
                    EditableFeature newFeature =
211
                        newFeatureStore.createNewFeature(true);
212
                    newFeature.set(routeFieldName, routeName);
213
                    Geometry calibratedRoute = calibrateRoute(geometry,points);
214
                    newFeature.setDefaultGeometry(calibratedRoute);
215
                    newFeatureStore.update(newFeature);
216
                } else if (includeAll) {
217
                    EditableFeature newFeature = newFeatureStore.createNewFeature(true);
218
                    newFeature.set(routeFieldName, routeName);
219
                    newFeature.setDefaultGeometry(geometry);
220
                    newFeatureStore.update(newFeature);
221
                }
222
                taskCount++;
223
                taskStatus.setCurValue(taskCount);
224
            }
225
            newFeatureStore.finishEditing();
226
        } catch (Exception e1) {
227
            taskStatus.abort();
228
            throw new LrsCalibrateRouteException("Error calibrating routes", e1);
229
        }
230

  
231
        taskStatus.terminate();
232
    }
233

  
234

  
235
    private Point getInRouteMPoint(Geometry geometry,Point point, Double measure) throws CreateGeometryException, GeometryOperationNotSupportedException, GeometryOperationException{
236
        GeometryManager geomanager = GeometryLocator.getGeometryManager();
237

  
238
        Point[] pointsInLine=(Point[])geometry.closestPoints(point);
239
        Point pointInLine=pointsInLine[0];
240

  
241
        Point mPoint = (Point) geomanager
242
            .create(Geometry.TYPES.POINT, Geometry.SUBTYPES.GEOM2DM);
243
        mPoint.setX(pointInLine.getX());
244
        mPoint.setY(pointInLine.getY());
245
        int mDimension=mPoint.getDimension()-1;
246
        mPoint.setCoordinateAt(mDimension, measure);
247

  
248
        return mPoint;
249
    }
250

  
251
    private Double calculateConvertedSearchRadius(){
252
        Double searchRadius=parameters.getSearchRadius();
253
        DistanceUnits measureUnits = parameters.getMeasureUnits();
254
        FeatureStore sourceFeatureStore = parameters.getSourceFeatureStore();
255

  
256
        try {
257
            IProjection projection = sourceFeatureStore.getDefaultFeatureType().getDefaultSRS();
258

  
259
            Double searchRadiusInMeters=searchRadius*measureUnits.getTransToMeter();
260
            //TODO
261
            //DistanceUnits projectionDistanceUnits=getProjectionDistanceUnits(projection);
262
            //return searchRadiusInMeters/projectionDistanceUnits.getTransToMeter();
263
            return searchRadiusInMeters;
264
        } catch (Exception e) {
265
            return new Double(0);
266
        }
267
    }
268

  
269
    private boolean isValidPoint(Point point,Geometry geometry, Double convertedSearchRadius){
270
        try {
271
            return geometry.isWithinDistance(point, convertedSearchRadius);
272
        } catch (Exception e) {
273
            return false;
274
        }
275
    }
276

  
277
    private Geometry calibrateRoute(Geometry geometry, List<Point> points) {
278
        LrsMeasureCalculationMethods calculationMethod= parameters.getMeasureCalculationMethods();
279
        boolean ignoreSpatialGaps = parameters.ignoreSpatialGaps();
280
        boolean interpolateBetweenCalibrationPoints=parameters.interpolateBetweenCalibrationPoints();
281
        boolean extrapolateBeforeCalibrationPoints=parameters.extrapolateBeforeCalibrationPoints();
282
        boolean extrapolateAfterCalibrationPoints=parameters.extrapolateAfterCalibrationPoints();
283

  
284
        if (points.isEmpty()){
285
            return geometry;
286
        }
287
        MeasuresCalculator expandedRoute=new DefaultMeasuresCalculator(geometry, ignoreSpatialGaps);
288

  
289

  
290
        expandedRoute.addControlPoints(points);
291
        expandedRoute.calculate(calculationMethod,extrapolateBeforeCalibrationPoints,interpolateBetweenCalibrationPoints,extrapolateAfterCalibrationPoints);
292
        return expandedRoute.getResult();
293
    }
294

  
295
    private class RouteAndPoints {
296
        private Geometry route;
297
        private List<Point> points;
298

  
299

  
300
        /**
301
         *
302
         */
303
        public RouteAndPoints(Geometry route) {
304
            this.setRoute(route);
305
            this.setPoints(new ArrayList<Point>());
306
        }
307

  
308

  
309
        /**
310
         * @return the route
311
         */
312
        public Geometry getRoute() {
313
            return route;
314
        }
315

  
316

  
317
        /**
318
         * @param route the route to set
319
         */
320
        public void setRoute(Geometry route) {
321
            this.route = route;
322
        }
323

  
324

  
325
        /**
326
         * @return the points
327
         */
328
        public List<Point> getPoints() {
329
            return points;
330
        }
331

  
332

  
333
        /**
334
         * @param points the points to set
335
         */
336
        public void setPoints(List<Point> points) {
337
            this.points = points;
338
        }
339

  
340
    }
341
}
342

  
343

  
344

  
345

  
org.gvsig.lrs/tags/org.gvsig.lrs-1.0.192/org.gvsig.lrs.lib/org.gvsig.lrs.lib.impl/src/main/java/org/gvsig/lrs/lib/impl/DefaultLrsGenerateDynamicSegmentationAlgorithmParams.java
1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2015 gvSIG Association
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.lrs.lib.impl;
24

  
25
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
26
import org.gvsig.fmap.dal.feature.FeatureStore;
27
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
28
import org.gvsig.lrs.lib.api.DistanceUnits;
29
import org.gvsig.lrs.lib.api.LrsCalibrateRouteAlgorithmParams;
30
import org.gvsig.lrs.lib.api.LrsGenerateDynamicSegmentationAlgorithmParams;
31
import org.gvsig.lrs.lib.api.LrsMeasureCalculationMethods;
32

  
33

  
34
/**
35
 * @author dmartinez
36
 *
37
 */
38
public class DefaultLrsGenerateDynamicSegmentationAlgorithmParams implements LrsGenerateDynamicSegmentationAlgorithmParams {
39

  
40
    private FeatureStore sourceFeatureStore;
41
    private FeatureAttributeDescriptor idRouteField;
42
    private NewFeatureStoreParameters newFeatureStoreParameters;
43
    private FeatureStore valueTableFeatureStore;
44
    private FeatureAttributeDescriptor valueTableIdRouteField;
45
    private FeatureAttributeDescriptor landmarkField;
46
    private FeatureAttributeDescriptor finalLandmarkField;
47
    private FeatureAttributeDescriptor valueField;
48
    private final String NAME = "LrsCalibrateRouteAlgorithm";
49
    private final String DESCRIPTION = "Algorithm to calibrate routes with linear reference system.";
50

  
51

  
52

  
53
    /**
54
     *
55
     */
56
    public DefaultLrsGenerateDynamicSegmentationAlgorithmParams() {
57

  
58
    }
59

  
60
    /* (non-Javadoc)
61
     * @see org.gvsig.lrs.lib.api.LrsAlgorithmParams#getName()
62
     */
63
    public String getName() {
64
        return NAME;
65
    }
66

  
67
    /* (non-Javadoc)
68
     * @see org.gvsig.lrs.lib.api.LrsAlgorithmParams#getDescription()
69
     */
70
    public String getDescription() {
71
        return DESCRIPTION;
72
    }
73

  
74

  
75
    public FeatureStore getSourceFeatureStore() {
76
        return sourceFeatureStore;
77
    }
78

  
79

  
80
    public void setSourceFeatureStore(FeatureStore sourceFeatureStore) {
81
        this.sourceFeatureStore = sourceFeatureStore;
82
    }
83

  
84

  
85
    public FeatureAttributeDescriptor getIdRouteField() {
86
        return idRouteField;
87
    }
88

  
89

  
90
    public void setIdRouteField(FeatureAttributeDescriptor idRouteField) {
91
        this.idRouteField = idRouteField;
92
    }
93

  
94

  
95
    public NewFeatureStoreParameters getNewFeatureStoreParameters() {
96
        return newFeatureStoreParameters;
97
    }
98

  
99

  
100
    public void setNewFeatureStoreParameters(
101
        NewFeatureStoreParameters newFeatureStoreParameters) {
102
        this.newFeatureStoreParameters = newFeatureStoreParameters;
103
    }
104

  
105

  
106
    public FeatureStore getValueTableFeatureStore() {
107
        return valueTableFeatureStore;
108
    }
109

  
110

  
111
    public void setValueTableFeatureStore(FeatureStore valueTableFeatureStore) {
112
        this.valueTableFeatureStore = valueTableFeatureStore;
113
    }
114

  
115

  
116
    public FeatureAttributeDescriptor getValueTableIdRouteField() {
117
        return valueTableIdRouteField;
118
    }
119

  
120

  
121
    public void setValueTableIdRouteField(
122
        FeatureAttributeDescriptor valueTableIdRouteField) {
123
        this.valueTableIdRouteField = valueTableIdRouteField;
124
    }
125

  
126

  
127
    public FeatureAttributeDescriptor getLandmarkField() {
128
        return landmarkField;
129
    }
130

  
131

  
132
    public void setLandmarkField(FeatureAttributeDescriptor landmarkField) {
133
        this.landmarkField = landmarkField;
134
    }
135

  
136

  
137
    public FeatureAttributeDescriptor getFinalLandmarkField() {
138
        return finalLandmarkField;
139
    }
140

  
141

  
142
    public void setFinalLandmarkField(
143
        FeatureAttributeDescriptor finalLandmarkField) {
144
        this.finalLandmarkField = finalLandmarkField;
145
    }
146

  
147

  
148
    public FeatureAttributeDescriptor getValueField() {
149
        return valueField;
150
    }
151

  
152

  
153
    public void setValueField(FeatureAttributeDescriptor valueField) {
154
        this.valueField = valueField;
155
    }
156

  
157

  
158

  
159
}
org.gvsig.lrs/tags/org.gvsig.lrs-1.0.192/org.gvsig.lrs.lib/org.gvsig.lrs.lib.impl/src/main/java/org/gvsig/lrs/lib/impl/expressionevaluator/symboltable/LRSSymbolTableFactory.java
1
package org.gvsig.lrs.lib.impl.expressionevaluator.symboltable;
2

  
3
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
4
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager;
5
import org.gvsig.expressionevaluator.SymbolTable;
6
import org.gvsig.expressionevaluator.spi.AbstractSymbolTableFactory;
7

  
8
/**
9
 *
10
 * @author jjdelcerro
11
 */
12
public class LRSSymbolTableFactory extends AbstractSymbolTableFactory {
13

  
14
    private SymbolTable symbolTable;
15
    
16
    public LRSSymbolTableFactory() {
17
        super(LRSSymbolTable.NAME, true);
18
    }
19
    
20
    @Override
21
    public SymbolTable create(Object... args) {
22
        if( this.symbolTable==null ) {
23
            this.symbolTable = new LRSSymbolTable();
24
        }
25
        return this.symbolTable;
26
    }
27
    
28
    public static final void selfRegister() {
29
        ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
30
        
31
        manager.registerSymbolTable(new LRSSymbolTableFactory());
32
    }
33
}
org.gvsig.lrs/tags/org.gvsig.lrs-1.0.192/org.gvsig.lrs.lib/org.gvsig.lrs.lib.impl/src/main/java/org/gvsig/lrs/lib/impl/expressionevaluator/symboltable/LRSSymbolTable.java
1
package org.gvsig.lrs.lib.impl.expressionevaluator.symboltable;
2

  
3
import org.gvsig.expressionevaluator.spi.AbstractSymbolTable;
4
import org.gvsig.lrs.lib.impl.expressionevaluator.function.lrs.STLineInterpolatePointFunction;
5
import org.gvsig.lrs.lib.impl.expressionevaluator.function.lrs.STLineLocatePointFunction;
6

  
7
/**
8
 *
9
 * @author jjdelcerro
10
 */
11
public class LRSSymbolTable extends AbstractSymbolTable {
12
    public static final String GROUP_LRS = "Linear reference system";
13
    
14
    static final String NAME = "Linear reference system";
15
    
16
    @SuppressWarnings("OverridableMethodCallInConstructor")
17
    public LRSSymbolTable() {
18
        super(NAME);
19

  
20
//        this.addFunction(new LRSPointFromGeometryFunction());
21
        this.addFunction(new STLineLocatePointFunction());
22
        this.addFunction(new STLineInterpolatePointFunction());
23
    }
24

  
25
}
org.gvsig.lrs/tags/org.gvsig.lrs-1.0.192/org.gvsig.lrs.lib/org.gvsig.lrs.lib.impl/src/main/java/org/gvsig/lrs/lib/impl/expressionevaluator/function/lrs/STLineInterpolatePointFunction.java
1
package org.gvsig.lrs.lib.impl.expressionevaluator.function.lrs;
2

  
3
import org.apache.commons.lang3.Range;
4
import org.gvsig.expressionevaluator.Interpreter;
5
import org.gvsig.expressionevaluator.spi.AbstractGeometryFunction;
6
import org.gvsig.fmap.geom.Geometry;
7
import org.gvsig.fmap.geom.aggregate.MultiLine;
8
import org.gvsig.fmap.geom.primitive.Line;
9
import org.gvsig.fmap.geom.primitive.Point;
10
import org.gvsig.lrs.lib.impl.expressionevaluator.symboltable.LRSSymbolTable;
11

  
12
public class STLineInterpolatePointFunction extends AbstractGeometryFunction {
13

  
14
    public static final String FUNCTION_ST_LINEINTERPOLATEPOINT = "ST_LineInterpolatePoint";
15
    
16
    public STLineInterpolatePointFunction() {
17
        super(
18
                LRSSymbolTable.GROUP_LRS,
19
                FUNCTION_ST_LINEINTERPOLATEPOINT,
20
                Range.is(2),
21
                "Returns a point interpolated along a line at a fractional location. First argument must be a LINESTRING. Second argument is a float between 0 and 1 representing the fraction of line length where the point is to be located. The Z and M values are interpolated if present.\n" +
22
                "See ST_LineLocatePoint for computing the line location nearest to a Point.",
23
                FUNCTION_ST_LINEINTERPOLATEPOINT + "( {{ a_linestring}},  a_fraction)",
24
                new String[]{
25
                    " a_linestring - a LineString with M coordinate.",
26
                    " a_fraction - the fractional location",},
27
                "Point",
28
                false
29
        );
30
    }
31

  
32
    @Override
33
    public boolean allowConstantFolding() {
34
        return true;
35
    }
36
    
37
    @Override
38
    public Object call(Interpreter interpreter, Object[] args) throws Exception {
39
        Geometry geom = this.getGeom(args, 0);
40
        double fraction = this.getDouble(args, 1);
41
        
42
        return lineInterpolatePoint(geom, fraction);
43
    }
44
    
45
    public static Point lineInterpolatePoint(Geometry geom, double fraction) throws Exception{
46
        double l = geom.perimeter();
47
        Point point;
48
        if (geom instanceof Line){
49
            point = ((Line)geom).extractPoint(fraction*l);
50
        } else if (geom instanceof MultiLine){
51
            point = ((MultiLine)geom).extractPoint(fraction*l);
52
        } else {
53
            throw new IllegalArgumentException("Need Line or MultiLine geometry.");
54
        }
55
        
56
        return point;
57
    }
58

  
59
}
org.gvsig.lrs/tags/org.gvsig.lrs-1.0.192/org.gvsig.lrs.lib/org.gvsig.lrs.lib.impl/src/main/java/org/gvsig/lrs/lib/impl/expressionevaluator/function/lrs/STLineLocatePointFunction.java
1
package org.gvsig.lrs.lib.impl.expressionevaluator.function.lrs;
2

  
3
import org.apache.commons.lang3.Range;
4
import org.gvsig.expressionevaluator.Interpreter;
5
import org.gvsig.expressionevaluator.spi.AbstractGeometryFunction;
6
import org.gvsig.fmap.geom.Geometry;
7
import org.gvsig.fmap.geom.aggregate.MultiLine;
8
import org.gvsig.fmap.geom.primitive.Line;
9
import org.gvsig.fmap.geom.primitive.Point;
10
import org.gvsig.lrs.lib.impl.expressionevaluator.symboltable.LRSSymbolTable;
11

  
12
public class STLineLocatePointFunction extends AbstractGeometryFunction {
13

  
14
    public static final String FUNCTION_ST_LINELOCATEPOINT = "ST_LineLocatePoint";
15
    
16
    public STLineLocatePointFunction() {
17
        super(
18
                LRSSymbolTable.GROUP_LRS,
19
                FUNCTION_ST_LINELOCATEPOINT,
20
                Range.is(2),
21
                "Returns a float between 0 and 1 representing the location of the closest point on a LineString to the given Point, as a fraction of 2d line length.\n"
22
                + "You can use the returned location to extract a Point (ST_LineInterpolatePoint) or a substring (ST_LineSubstring).\n"
23
                + "This is useful for approximating numbers of addresses\n",
24
                FUNCTION_ST_LINELOCATEPOINT + "( {{ a_linestring}},  a_point)",
25
                new String[]{
26
                    " a_linestring - a LineString with M coordinate.",
27
                    " a_point - a Point (with or without M coordinate) near of the LineString",},
28
                "Double",
29
                false
30
        );
31
    }
32

  
33
    @Override
34
    public boolean allowConstantFolding() {
35
        return true;
36
    }
37
    
38
    @Override
39
    public Object call(Interpreter interpreter, Object[] args) throws Exception {
40
        Geometry geom = this.getGeom(args, 0);
41
        Point point =this.getPoint(args, 1);
42
        
43
        return lineLocatePoint(geom, point);
44
    }
45
    
46
    public static double lineLocatePoint(Geometry geom, Point point) throws Exception{
47
        double l = geom.perimeter();
48
        double locatePoint;
49
        if (geom instanceof Line){
50
            locatePoint = ((Line)geom).getPathLength(point);
51
        } else if (geom instanceof MultiLine){
52
            locatePoint = ((MultiLine)geom).getPathLength(point);
53
        } else {
54
            throw new IllegalArgumentException("Need Line or MultiLine geometry.");
55
        }
56
        
57
        double fraction = locatePoint / l;
58
        
59
        return fraction;
60
    }
61

  
62
}
org.gvsig.lrs/tags/org.gvsig.lrs-1.0.192/org.gvsig.lrs.lib/org.gvsig.lrs.lib.impl/src/main/java/org/gvsig/lrs/lib/impl/expressionevaluator/function/lrs/LRSPointFromGeometryFunction.java
1
package org.gvsig.lrs.lib.impl.expressionevaluator.function.lrs;
2

  
3
import org.apache.commons.lang3.Range;
4
import org.gvsig.expressionevaluator.Interpreter;
5
import org.gvsig.expressionevaluator.spi.AbstractGeometryFunction;
6
import org.gvsig.fmap.geom.Geometry;
7
import org.gvsig.fmap.geom.primitive.Point;
8
import org.gvsig.lrs.lib.api.LrsAlgorithmsLocator;
9
import org.gvsig.lrs.lib.api.LrsAlgorithmsManager;
10
import org.gvsig.lrs.lib.impl.expressionevaluator.symboltable.LRSSymbolTable;
11

  
12
public class LRSPointFromGeometryFunction extends AbstractGeometryFunction {
13

  
14
    public static final String FUNCTION_LRSPOINTFROMGEOMETRY = "LRSPointFromGeometry";
15
    
16
    public LRSPointFromGeometryFunction() {
17
        super(
18
                LRSSymbolTable.GROUP_LRS, 
19
                FUNCTION_LRSPOINTFROMGEOMETRY, 
20
                Range.is(2),
21
                "Returns ?????? TODO.\n",
22
                FUNCTION_LRSPOINTFROMGEOMETRY+"( {{geom}}, point_or_distance)",
23
                new String[]{
24
                    "geom - TODO.",
25
                    "point_or_distance - TODO",
26
                },
27
                "Point",
28
                false
29
        );
30
    }
31

  
32
    @Override
33
    public boolean allowConstantFolding() {
34
        return true;
35
    }
36
    
37
    @Override
38
    public Object call(Interpreter interpreter, Object[] args) throws Exception {
39
        Geometry geom = this.getGeom(args, 0);
40
        Object v2 = this.getObject(args, 1);
41

  
42
        LrsAlgorithmsManager manager = LrsAlgorithmsLocator.getLrsAlgorithmsManager();
43
        Point r = null;
44
        if( v2 instanceof Number ) {
45
            double distance = this.getDouble(args, 1);
46
            r = manager.getMPointFromGeometry(geom, distance);
47
        } else {
48
            Point point =this.getPoint(args, 1);
49
            r = manager.getMPointFromGeometry(geom, point);
50
        }
51
        return r;
52
    }
53

  
54
}
org.gvsig.lrs/tags/org.gvsig.lrs-1.0.192/org.gvsig.lrs.lib/org.gvsig.lrs.lib.impl/src/main/java/org/gvsig/lrs/lib/impl/DefaultLrsCreateRouteAlgorithmParams.java
1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2015 gvSIG Association
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.lrs.lib.impl;
24

  
25
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
26
import org.gvsig.fmap.dal.feature.FeatureStore;
27
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
28
import org.gvsig.lrs.lib.api.LrsCoordinatesPriority;
29
import org.gvsig.lrs.lib.api.LrsCreateRouteAlgorithmParams;
30
import org.gvsig.lrs.lib.api.LrsSourceOfMeasures;
31

  
32

  
33
/**
34
 * @author fdiaz
35
 *
36
 */
37
public class DefaultLrsCreateRouteAlgorithmParams implements LrsCreateRouteAlgorithmParams {
38

  
39
    private FeatureStore sourceFeatureStore;
40
    private FeatureAttributeDescriptor idRouteField;
41
    private NewFeatureStoreParameters newFeatureStoreParameters;
42
    private LrsSourceOfMeasures sourceOfMeasures;
43
    private FeatureAttributeDescriptor fromMeasureField;
44
    private FeatureAttributeDescriptor toMeasureField;
45
    private LrsCoordinatesPriority coordinatePriority;
46
    private double measureFactor = 1.0;
47
    private double measureOffset = 0.0;
48
    private boolean ignoreSpatialGaps;
49
    private final String NAME = "LrsCreateRouteAlgorithm";
50
    private final String DESCRIPTION = "Algorithm to create routes with linear reference system.";
51

  
52

  
53

  
54
    /**
55
     *
56
     */
57
    public DefaultLrsCreateRouteAlgorithmParams() {
58
        measureFactor = 1.0;
59
        measureOffset = 0.0;
60
    }
61

  
62
    /* (non-Javadoc)
63
     * @see org.gvsig.lrs.lib.api.LrsAlgorithmParams#getName()
64
     */
65
    public String getName() {
66
        return NAME;
67
    }
68

  
69
    /* (non-Javadoc)
70
     * @see org.gvsig.lrs.lib.api.LrsAlgorithmParams#getDescription()
71
     */
72
    public String getDescription() {
73
        return DESCRIPTION;
74
    }
75

  
76
    /*
77
     * (non-Javadoc)
78
     * @see org.gvsig.lrs.lib.api.LrsCreateRouteAlgorithmParams#getSourceFeatureStore()
79
     */
80
    public FeatureStore getSourceFeatureStore() {
81
        return sourceFeatureStore;
82
    }
83

  
84

  
85
    /*
86
     * (non-Javadoc)
87
     * @see org.gvsig.lrs.lib.api.LrsCreateRouteAlgorithmParams#setSourceFeatureStore(org.gvsig.fmap.dal.feature.FeatureStore)
88
     */
89
    public void setSourceFeatureStore(FeatureStore sourceFeatureStore) {
90
        this.sourceFeatureStore = sourceFeatureStore;
91
    }
92

  
93
    /*
94
     * (non-Javadoc)
95
     * @see org.gvsig.lrs.lib.api.LrsCreateRouteAlgorithmParams#getIdRouteField()
96
     */
97
    public FeatureAttributeDescriptor getIdRouteField() {
98
        return idRouteField;
99
    }
100

  
101

  
102
    /*
103
     * (non-Javadoc)
104
     * @see org.gvsig.lrs.lib.api.LrsCreateRouteAlgorithmParams#setIdRouteField(org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor)
105
     */
106
    public void setIdRouteField(FeatureAttributeDescriptor idRouteField) {
107
        this.idRouteField = idRouteField;
108
    }
109

  
110
    /*
111
     * (non-Javadoc)
112
     * @see org.gvsig.lrs.lib.api.LrsCreateRouteAlgorithmParams#getSourceOfMeasures()
113
     */
114
    public LrsSourceOfMeasures getSourceOfMeasures() {
115
        return sourceOfMeasures;
116
    }
117

  
118
    /*
119
     * (non-Javadoc)
120
     * @see org.gvsig.lrs.lib.api.LrsCreateRouteAlgorithmParams#setSourceOfMeasures(org.gvsig.lrs.lib.api.SourceOfMeasures)
121
     */
122
    public void setSourceOfMeasures(LrsSourceOfMeasures sourceOfMeasures) {
123
        this.sourceOfMeasures = sourceOfMeasures;
124
    }
125

  
126
    /*
127
     * (non-Javadoc)
128
     * @see org.gvsig.lrs.lib.api.LrsCreateRouteAlgorithmParams#getFromMeasureField()
129
     */
130
    public FeatureAttributeDescriptor getFromMeasureField() {
131
        return fromMeasureField;
132
    }
133

  
134
    /*
135
     * (non-Javadoc)
136
     * @see org.gvsig.lrs.lib.api.LrsCreateRouteAlgorithmParams#setFromMeasureField(org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor)
137
     */
138
    public void setFromMeasureField(FeatureAttributeDescriptor fromMeasureField) {
139
        this.fromMeasureField = fromMeasureField;
140
    }
141

  
142
    /*
143
     * (non-Javadoc)
144
     * @see org.gvsig.lrs.lib.api.LrsCreateRouteAlgorithmParams#getToMeasureField()
145
     */
146
    public FeatureAttributeDescriptor getToMeasureField() {
147
        return toMeasureField;
148
    }
149

  
150
    /*
151
     * (non-Javadoc)
152
     * @see org.gvsig.lrs.lib.api.LrsCreateRouteAlgorithmParams#setToMeasureField(org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor)
153
     */
154
    public void setToMeasureField(FeatureAttributeDescriptor toMeasureField) {
155
        this.toMeasureField = toMeasureField;
156
    }
157

  
158
    /*
159
     * (non-Javadoc)
160
     * @see org.gvsig.lrs.lib.api.LrsCreateRouteAlgorithmParams#getCoordinatePriority()
161
     */
162
    public LrsCoordinatesPriority getCoordinatePriority() {
163
        return coordinatePriority;
164
    }
165

  
166
    /*
167
     * (non-Javadoc)
168
     * @see org.gvsig.lrs.lib.api.LrsCreateRouteAlgorithmParams#setCoordinatePriority(org.gvsig.lrs.lib.api.CoordinatesPriority)
169
     */
170
    public void setCoordinatePriority(LrsCoordinatesPriority coordinatePriority) {
171
        this.coordinatePriority = coordinatePriority;
172
    }
173

  
174
    /*
175
     * (non-Javadoc)
176
     * @see org.gvsig.lrs.lib.api.LrsCreateRouteAlgorithmParams#getMeasureFactor()
177
     */
178
    public double getMeasureFactor() {
179
        return measureFactor;
180
    }
181

  
182
    /*
183
     * (non-Javadoc)
184
     * @see org.gvsig.lrs.lib.api.LrsCreateRouteAlgorithmParams#setMeasureFactor(double)
185
     */
186
    public void setMeasureFactor(double measureFactor) {
187
        this.measureFactor = measureFactor;
188
    }
189

  
190
    /*
191
     * (non-Javadoc)
192
     * @see org.gvsig.lrs.lib.api.LrsCreateRouteAlgorithmParams#getMeasureOffset()
193
     */
194
    public double getMeasureOffset() {
195
        return measureOffset;
196
    }
197

  
198
    /*
199
     * (non-Javadoc)
200
     * @see org.gvsig.lrs.lib.api.LrsCreateRouteAlgorithmParams#setMeasureOffset(double)
201
     */
202
    public void setMeasureOffset(double measureOffset) {
203
        this.measureOffset = measureOffset;
204
    }
205

  
206
    /*
207
     * (non-Javadoc)
208
     * @see org.gvsig.lrs.lib.api.LrsCreateRouteAlgorithmParams#isIgnoreSpatialGaps()
209
     */
210
    public boolean ignoreSpatialGaps() {
211
        return ignoreSpatialGaps;
212
    }
213

  
214
    /*
215
     * (non-Javadoc)
216
     * @see org.gvsig.lrs.lib.api.LrsCreateRouteAlgorithmParams#setIgnoreSpatialGaps(boolean)
217
     */
218
    public void setIgnoreSpatialGaps(boolean ignoreSpatialGaps) {
219
        this.ignoreSpatialGaps = ignoreSpatialGaps;
220
    }
221

  
222
    public NewFeatureStoreParameters getNewFeatureStoreParameters() {
223
        return this.newFeatureStoreParameters;
224
    }
225

  
226
    public void setNewFeatureStoreParameters(
227
        NewFeatureStoreParameters newFeatureStoreParameters) {
228
        this.newFeatureStoreParameters = newFeatureStoreParameters;
229
    }
230

  
231
}
org.gvsig.lrs/tags/org.gvsig.lrs-1.0.192/org.gvsig.lrs.lib/org.gvsig.lrs.lib.impl/src/main/java/org/gvsig/lrs/lib/impl/DefaultMeasuresCalculator.java
1
package org.gvsig.lrs.lib.impl;
2

  
3
import org.gvsig.lrs.lib.api.MeasuresCalculator;
4
import java.util.ArrayList;
5
import java.util.List;
6
import org.gvsig.fmap.geom.Geometry;
7
import org.gvsig.fmap.geom.GeometryLocator;
8
import org.gvsig.fmap.geom.GeometryManager;
9
import org.gvsig.fmap.geom.operation.GeometryOperationException;
10
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
11
import org.gvsig.fmap.geom.primitive.Line;
12
import org.gvsig.fmap.geom.primitive.Point;
13
import org.gvsig.lrs.lib.api.LrsMeasureCalculationMethods;
14
import org.slf4j.Logger;
15
import org.slf4j.LoggerFactory;
16

  
17
class DefaultMeasuresCalculator implements MeasuresCalculator {
18

  
19
    private static final Logger logger = LoggerFactory.getLogger(DefaultMeasuresCalculator.class);
20

  
21
    private Geometry route;
22
    private List<Boolean> isFixedPoint;
23
    private List<Double> distances;
24
    private List<Double> oldMValues;
25
    private List<Point> vertices;
26
    private List<Integer> lineIndexes;
27
    private int MDIMENSION;
28

  
29
    private final Double PRECISION = new Double(1.0e-5);
30

  
31
    @Override
32
    public Geometry getResult() {
33
        return route;
34
    }
35

  
36
    public DefaultMeasuresCalculator(Geometry geometry, boolean ignoreSpatialGaps) {
37
        isFixedPoint = new ArrayList<Boolean>();
38
        distances = new ArrayList<Double>();
39
        oldMValues = new ArrayList<Double>();
40
        vertices = new ArrayList<Point>();
41
        lineIndexes = new ArrayList<Integer>();
42
        route = geometry.cloneGeometry();
43

  
44
        double distance = 0;
45
        List<Line> lines = LrsAlgorithmUtils.extractLines(geometry);
46
        Point previousVertex = null;
47
        for (int i = 0; i < lines.size(); i++) {
48
            Line line = lines.get(i);
49
            for (int j = 0; j < line.getNumVertices(); j++) {
50
                Point vertex = line.getVertex(j);
51
                if (j == 0) {
52
                    if (previousVertex == null) {
53
                        //First point in geometry
54
                        MDIMENSION = vertex.getDimension() - 1;
55
                        distance = 0;
56
                    }
57
                    Integer visitedIndex = getIndexVisitedVertex(vertex);
58
                    if (visitedIndex != null) {
59
                        previousVertex = vertices.get(visitedIndex);
60
                        distance = distances.get(visitedIndex);
61
                    } else {
62
                        if (previousVertex != null) {
63
                            try {
64
                                if (!LrsAlgorithmUtils.equalPoints(vertex, previousVertex)) {
65
                                    Integer nearestVertexPos = getIndexNearestVisitedVertex(vertex);
66
                                    if (ignoreSpatialGaps) {
67
                                        distance = distances.get(nearestVertexPos);
68
                                    } else {
69
                                        Point nearestVertex = vertices.get(nearestVertexPos);
70
                                        distance = distances.get(nearestVertexPos);
71
                                        distance += vertex.distance(nearestVertex);
72
                                    }
73
                                }
74
                            } catch (Exception e) {
75
                                distance = Double.NaN;
76
                            }
77
                        }
78
                    }
79
                } else {
80
                    try {
81
                        distance += vertex.distance(previousVertex);
82
                    } catch (Exception e) {
83
                        distance = Double.NaN;
84
                    }
85
                }
86
                Double mValue = vertex.getCoordinateAt(MDIMENSION);
87

  
88
                isFixedPoint.add(false);
89
                vertices.add(vertex);
90
                distances.add(distance);
91
                oldMValues.add(mValue);
92
                lineIndexes.add(i);
93

  
94
                previousVertex = vertex;
95
            }
96
        }
97
    }
98

  
99
    private Integer getIndexNearestVisitedVertex(Point vertex) throws GeometryOperationNotSupportedException, GeometryOperationException {
100
        double nearestDistance = Double.POSITIVE_INFINITY;
101
        Integer nearestPointPos = null;
102
        for (int i = 0; i < vertices.size(); i++) {
103
            Double distance = vertex.distance(vertices.get(i));
104
            if (nearestDistance > distance && distance > Double.valueOf(0)) {
105
                nearestDistance = distance;
106
                nearestPointPos = i;
107
            }
108
        }
109
        return nearestPointPos;
110
    }
111

  
112
    private Integer getIndexVisitedVertex(Point point) {
113
        double distance = Double.NEGATIVE_INFINITY;
114
        Integer index = null;
115
        for (int i = 0; i < vertices.size(); i++) {
116
            if (LrsAlgorithmUtils.equalPoints(point, vertices.get(i))) {
117
                if (this.distances.get(i) > distance) {
118
                    distance = this.distances.get(i);
119
                    index = i;
120
                }
121
            }
122
        }
123
        return index;
124
    }
125

  
126
    @Override
127
    public void addControlPoints(List<Point> fixedPoints) {
128
        for (Point fixedPoint : fixedPoints) {
129
            Double length = Double.valueOf(0);
130
            int index = 0;
131
            List<Line> lines = LrsAlgorithmUtils.extractLines(route);
132
            List<Integer> insertAtList = new ArrayList<Integer>();
133
            List<Double> lengths = new ArrayList<Double>();
134
            List<Double> oldValues = new ArrayList<Double>();
135
            List<Integer> lineReference = new ArrayList<Integer>();
136
            for (int i = 0; i < lines.size(); i++) {
137
                Line line = lines.get(i);
138
                try {
139
                    if (line.isWithinDistance(fixedPoint, PRECISION)) {
140
                        length = distances.get(index);
141
                        for (int j = 0; j < line.getNumVertices(); j++) {
142
                            Point vertex = line.getVertex(j);
143
                            Point nextVertex;
144
                            if (j + 1 < line.getNumVertices()) {
145
                                nextVertex = line.getVertex(j + 1);
146
                            } else {
147
                                nextVertex = null;
148
                            }
149
                            if (LrsAlgorithmUtils.equalPoints(vertex, fixedPoint) || vertex.distance(fixedPoint) <= PRECISION) {
150
                                oldMValues.set(index, vertex.getCoordinateAt(MDIMENSION));
151
                                Double mValue = fixedPoint.getCoordinateAt(MDIMENSION);
152
                                vertex.setCoordinateAt(MDIMENSION, mValue);
153
                                isFixedPoint.set(index, true);
154
                                vertices.set(index, vertex);
155
                            } else {
156
                                if (nextVertex != null
157
                                        && !LrsAlgorithmUtils.equalPoints(nextVertex, fixedPoint)
158
                                        && vertex.distance(fixedPoint) > PRECISION
159
                                        && nextVertex.distance(fixedPoint) > PRECISION) {
160

  
161
                                    GeometryManager geomanager = GeometryLocator.getGeometryManager();
162
                                    Line segment = (Line) geomanager
163
                                            .create(Geometry.TYPES.LINE, Geometry.SUBTYPES.GEOM2DM);
164
                                    segment.addVertex(vertex);
165
                                    segment.addVertex(nextVertex);
166
                                    //FIXME
167
                                    //if (line.intersects(fixedPoint)){
168
                                    if (segment.isWithinDistance(fixedPoint, PRECISION)) {
169
                                        double distanceFirstVertex = distances.get(index);
170
                                        double distanceToFixedPoint = vertex.distance(fixedPoint);
171
                                        length = distanceFirstVertex + distanceToFixedPoint;
172
                                        Double oldMValue = LrsAlgorithmUtils.straightLineThroughTwoPointsEquation(
173
                                                distances.get(index),
174
                                                distances.get(index + 1),
175
                                                vertex.getCoordinateAt(MDIMENSION),
176
                                                nextVertex.getCoordinateAt(MDIMENSION),
177
                                                length);
178
                                        insertAtList.add(index + 1);
179
                                        lengths.add(length);
180
                                        oldValues.add(oldMValue);
181
                                        lineReference.add(i);
182
                                    }
183
                                }
184
                            }
185
                            index++;
186
                        }
187
                    } else {
188
                        index += line.getNumVertices();
189
                    }
190
                } catch (Exception e) {
191
                    logger.warn("Error inserting point " + fixedPoint.toString(), e);
192
                }
193
            }
194
            int pos = 0;
195
            for (Integer insertAt : insertAtList) {
196
                Double distance = lengths.get(pos);
197
                Double oldMValue = oldValues.get(pos);
198
                Integer linePos = lineReference.get(pos);
199
                int correctedPosition = insertAt + pos;
200
                route = LrsAlgorithmUtils.insertVertex(route, fixedPoint, correctedPosition);
201
                isFixedPoint.add(correctedPosition, true);
202
                distances.add(correctedPosition, distance);
203
                oldMValues.add(correctedPosition, oldMValue);
204
                lineIndexes.add(correctedPosition, linePos);
205
                vertices.add(correctedPosition, fixedPoint);
206
                pos++;
207
            }
208
        }
209
    }
210

  
211
    @Override
212
    public void setFirstMeasure(double mvalue) {
213
        Point mPoint = (Point) this.vertices.get(0).cloneGeometry();
214
        int mDimension = mPoint.getDimension() - 1;
215
        mPoint.setCoordinateAt(mDimension, mvalue);
216
        List<Point> points = new ArrayList<>();
217
        points.add(mPoint);
218
        this.addControlPoints(points);
219
    }
220

  
221
    @Override
222
    public void setLastMeasure(double mvalue) {
223
        Point mPoint = (Point) this.vertices.get(this.vertices.size()-1).cloneGeometry();
224
        int mDimension = mPoint.getDimension() - 1;
225
        mPoint.setCoordinateAt(mDimension, mvalue);
226
        List<Point> points = new ArrayList<>();
227
        points.add(mPoint);
228
        this.addControlPoints(points);
229
    }
230

  
231
    @Override
232
    public void setMeasuresToDistances() {
233
        List<Point> points = LrsAlgorithmUtils.extractPoints(route);
234
        for (int i = 0; i < points.size(); i++) {
235
            if (!isFixedPoint.get(i)) {
236
                points.get(i).setCoordinateAt(MDIMENSION, distances.get(i));
237
                vertices.get(i).setCoordinateAt(MDIMENSION, distances.get(i));
238
                oldMValues.set(i, distances.get(i));
239
            }
240
        }
241
    }
242

  
243
    @Override
244
	public void calculate() {
245
        this.calculate(
246
                LrsMeasureCalculationMethods.DISTANCE,
247
                false,
248
                true,
249
                false
250
        );
251
    }
252
    
253
    @Override
254
    public void calculate(LrsMeasureCalculationMethods calculationMethod, boolean before, boolean between, boolean after) {
255
        for (int i = 0; i < vertices.size(); i++) {
256
            if (!isFixedPoint.get(i)) {
257
                Integer prevFixedPointPos = getPreviousPosFixedPoint(i);
258
                Integer nextFixedPointPos = getNextPosFixedPoint(i, false);
259

  
260
                if (prevFixedPointPos == null && nextFixedPointPos != null && before) {
261
                    extrapolateBeforeCalibrationPoints(calculationMethod, nextFixedPointPos, i);
262
                }
263
                if (prevFixedPointPos != null && nextFixedPointPos != null && between) {
264
                    calculateMValue(calculationMethod, prevFixedPointPos, nextFixedPointPos, i);
265
                }
266
                if (prevFixedPointPos != null && nextFixedPointPos == null && after) {
267
                    extrapolateAfterCalibrationPoints(calculationMethod, prevFixedPointPos, i);
268
                }
269
            }
270
        }
271
    }
272

  
273
    private void extrapolateBeforeCalibrationPoints(LrsMeasureCalculationMethods calculationMethod, int firstFixedPointPos, int pos) {
274
        Integer secondFixedPointPos = getNextPosFixedPoint(firstFixedPointPos, false);
275
        if (secondFixedPointPos != null) {
276

  
277
            if (calculationMethod.equals(LrsMeasureCalculationMethods.DISTANCE)) {
278
                if (!isFixedPoint.get(pos)) {
279
                    List<Point> points = LrsAlgorithmUtils.extractPoints(route);
280
                    points.get(pos).setCoordinateAt(MDIMENSION, distances.get(pos));
281
                    vertices.get(pos).setCoordinateAt(MDIMENSION, distances.get(pos));
282
                    oldMValues.set(pos, distances.get(pos));
283
                }
284
            }
285

  
286
            Double newMValueFirstPoint = vertices.get(firstFixedPointPos).getCoordinateAt(MDIMENSION);
287
            Double newMValueSecondPoint = vertices.get(secondFixedPointPos).getCoordinateAt(MDIMENSION);
288

  
289
            Double oldMValueFirstPoint;
290
            Double oldMValueSecondPoint;
291
            if (calculationMethod.equals(LrsMeasureCalculationMethods.DISTANCE)) {
292
                oldMValueFirstPoint = distances.get(firstFixedPointPos);
293
                oldMValueSecondPoint = distances.get(secondFixedPointPos);
294
            } else {
295
                oldMValueFirstPoint = oldMValues.get(firstFixedPointPos);
296
                oldMValueSecondPoint = oldMValues.get(secondFixedPointPos);
297
            }
298

  
299
            Double distanceBetweenFixedPoints = newMValueSecondPoint - newMValueFirstPoint;
300
            Double distanceBetweenOldFixedPoints = oldMValueSecondPoint - oldMValueFirstPoint;
301

  
302
            Double calibrationRatio = distanceBetweenOldFixedPoints / distanceBetweenFixedPoints;
303

  
304
            Double valueToRecalculate = vertices.get(pos).getCoordinateAt(MDIMENSION);
305
            Double distanceBetween = oldMValueFirstPoint - valueToRecalculate;
306
            Double mValue = newMValueFirstPoint - (distanceBetween / calibrationRatio);
307
            refreshMValueGeometryVertex(pos, mValue, false);
308
        }
309
    }
310

  
311
    private void extrapolateAfterCalibrationPoints(LrsMeasureCalculationMethods calculationMethod, int lastFixedPointPos, int pos) {
312
        Integer prevFixedPointPos = getPreviousPosFixedPoint(lastFixedPointPos);
313
        if (prevFixedPointPos != null) {
314
            calculateMValue(calculationMethod, prevFixedPointPos, lastFixedPointPos, pos);
315
        }
316
    }
317

  
318
    private void calculateMValue(LrsMeasureCalculationMethods calculationMethod, Integer prevFixedPointPos, int nextFixedPointPos, int pos) {
319

  
320
        if (calculationMethod.equals(LrsMeasureCalculationMethods.DISTANCE)) {
321
            if (!isFixedPoint.get(pos)) {
322
                List<Point> points = LrsAlgorithmUtils.extractPoints(route);
323
                points.get(pos).setCoordinateAt(MDIMENSION, distances.get(pos));
324
                vertices.get(pos).setCoordinateAt(MDIMENSION, distances.get(pos));
325
                oldMValues.set(pos, distances.get(pos));
326
            }
327
        }
328

  
329
        Double newMValueBeforePoint = vertices.get(prevFixedPointPos).getCoordinateAt(MDIMENSION);
330
        Double newMValueAfterPoint = vertices.get(nextFixedPointPos).getCoordinateAt(MDIMENSION);
331
        while (newMValueBeforePoint.equals(newMValueAfterPoint)) {
332
            prevFixedPointPos = getPreviousPosFixedPoint(prevFixedPointPos);
333
            if (prevFixedPointPos != null) {
334
                newMValueBeforePoint = vertices.get(prevFixedPointPos).getCoordinateAt(MDIMENSION);
335
            } else {
336
                refreshMValueGeometryVertex(pos, Double.NaN, false);
337
                return;
338
            }
339
        }
340

  
341
        Double oldMValueBeforePoint;
342
        Double oldMValueAfterPoint;
343
        if (calculationMethod.equals(LrsMeasureCalculationMethods.DISTANCE)) {
344
            oldMValueBeforePoint = distances.get(prevFixedPointPos);
345
            oldMValueAfterPoint = distances.get(nextFixedPointPos);
346
        } else {
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff