Revision 188

View differences:

org.gvsig.lrs/tags/org.gvsig.lrs-1.0.24/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 org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
26
import org.gvsig.fmap.geom.Geometry;
27
import org.gvsig.lrs.lib.api.LrsEditRouteCalibrationAlgorithmParams;
28

  
29

  
30

  
31
/**
32
 * @author dmartinez
33
 *
34
 */
35
public class DefaultLrsEditRouteCalibrationAlgorithmParams implements LrsEditRouteCalibrationAlgorithmParams {
36

  
37

  
38
    private FeatureAttributeDescriptor idRouteField;
39
    private String selectedRouteName;
40
    private Geometry modifiedGeometry;
41
    private final String NAME = "LrsEditRouteCalibrationAlgorithm";
42
    private final String DESCRIPTION = "Algorithm to edit route calibration with linear reference system.";
43

  
44

  
45
    /**
46
     *
47
     */
48
    public DefaultLrsEditRouteCalibrationAlgorithmParams() {
49

  
50
    }
51

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

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

  
66

  
67
    public FeatureAttributeDescriptor getIdRouteField() {
68
        return idRouteField;
69
    }
70

  
71

  
72
    public void setIdRouteField(FeatureAttributeDescriptor idRouteField) {
73
        this.idRouteField = idRouteField;
74
    }
75

  
76

  
77
    public String getSelectedRouteName() {
78
        return selectedRouteName;
79
    }
80

  
81

  
82
    public void setSelectedRouteName(String selectedRouteName) {
83
        this.selectedRouteName = selectedRouteName;
84
    }
85

  
86

  
87
    public Geometry getModifiedGeometry() {
88
        return modifiedGeometry;
89
    }
90

  
91

  
92
    public void setModifiedGeometry(Geometry modifiedGeometry) {
93
        this.modifiedGeometry = modifiedGeometry;
94
    }
95

  
96

  
97
}
org.gvsig.lrs/tags/org.gvsig.lrs-1.0.24/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.24/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.24/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.24/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 {
347
            oldMValueBeforePoint = oldMValues.get(prevFixedPointPos);
348
            oldMValueAfterPoint = oldMValues.get(nextFixedPointPos);
349
        }
350

  
351
        Double distanceBetweenFixedPoints = newMValueAfterPoint - newMValueBeforePoint;
352
        Double distanceBetweenOldFixedPoints = oldMValueAfterPoint - oldMValueBeforePoint;
353

  
354
        Double calibrationRatio = distanceBetweenOldFixedPoints / distanceBetweenFixedPoints;
355

  
356
        Double valueToRecalculate = vertices.get(pos).getCoordinateAt(MDIMENSION);
357
        Double distanceBetween = valueToRecalculate - oldMValueBeforePoint;
358
        Double mValue = newMValueBeforePoint + (distanceBetween / calibrationRatio);
359
        refreshMValueGeometryVertex(pos, mValue, true);
360
    }
361

  
362
    private Integer getNextPosFixedPoint(int pos, boolean selfInclude) {
363
        Integer nextFixedPointPos = findNextFixedPointInLine(pos, selfInclude);
364
        if (nextFixedPointPos != null) {
365
            return nextFixedPointPos;
366
        } else {
367
            int lastPositionInLine = getLastPositionInLine(pos);
368
            nextFixedPointPos = findNextFixedPointInNextLines(lastPositionInLine, new ArrayList<Integer>());
369
            if (nextFixedPointPos != null) {
370
                return nextFixedPointPos;
371
            }
372
            try {
373
                Integer nearestVertex = findNearestNextPoint(lastPositionInLine);
374
                return getNextPosFixedPoint(nearestVertex, true);
375
            } catch (Exception e) {
376
                return null;
377
            }
378
        }
379
    }
380

  
381
    private Integer getLastPositionInLine(int pos) {
382
        Integer lineVisited = lineIndexes.get(pos);
383
        for (int i = pos + 1; i < vertices.size(); i++) {
384
            if (!lineVisited.equals(lineIndexes.get(i))) {
385
                return i - 1;
386
            }
387
        }
388
        return vertices.size() - 1;
389
    }
390

  
391
    private Integer findNextFixedPointInNextLines(int pos, List<Integer> visitedLines) {
392
        Integer nextFixedPointPos = findNextFixedPointInLine(pos, true);
393
        if (nextFixedPointPos != null) {
394
            return nextFixedPointPos;
395
        } else {
396
            Integer lineIndex = lineIndexes.get(pos);
397
            visitedLines.add(lineIndex);
398
            int lastPositionInLine = getLastPositionInLine(pos);
399
            Point lastVertexInLine = vertices.get(lastPositionInLine);
400
            for (int i = lastPositionInLine; i < vertices.size(); i++) {
401
                if (LrsAlgorithmUtils.equalPoints(lastVertexInLine, vertices.get(i))) {
402
                    if (!visitedLines.contains(lineIndexes.get(i))) {
403
                        findNextFixedPointInNextLines(i, visitedLines);
404
                    }
405
                }
406
            }
407
        }
408
        return null;
409
    }
410

  
411
    private Integer findNextFixedPointInLine(int vertexPos, boolean selfInclude) {
412
        int lineIndex = lineIndexes.get(vertexPos);
413
        if (!selfInclude) {
414
            vertexPos += 1;
415
        }
416
        for (int i = vertexPos; i < vertices.size(); i++) {
417
            if (!lineIndexes.get(i).equals(lineIndex)) {
418
                return null;
419
            }
420
            if (isFixedPoint.get(i)) {
421
                return i;
422
            }
423
        }
424
        return null;
425
    }
426

  
427
    private Integer findNearestNextPoint(int vertexPos) throws GeometryOperationNotSupportedException, GeometryOperationException {
428
        Point vertex = vertices.get(vertexPos);
429
        double nearestDistance = Double.POSITIVE_INFINITY;
430
        Integer nearestPointPos = null;
431
        for (int i = vertexPos + 1; i < vertices.size(); i++) {
432
            Double distance = vertex.distance(vertices.get(i));
433
            if (nearestDistance > distance && distance > Double.valueOf(0)) {
434
                nearestDistance = distance;
435
                nearestPointPos = i;
436
            }
437
        }
438
        return nearestPointPos;
439
    }
440

  
441
    private Integer getPreviousPosFixedPoint(int pos) {
442
        Integer prevFixedPointPos = findPrevFixedPointInLine(pos);
443
        if (prevFixedPointPos != null) {
444
            return prevFixedPointPos;
445
        } else {
446
            Integer lineVisited = lineIndexes.get(pos);
447
            for (int i = pos - 1; i >= 0; i--) {
448
                if (!lineVisited.equals(lineIndexes.get(i))) {//Line has changed
449
                    int lastPositionInLine = i + 1;
450
                    for (int j = lastPositionInLine; j >= 0; j--) {
451
                        if (LrsAlgorithmUtils.equalPoints(vertices.get(lastPositionInLine), vertices.get(j))) {
452
                            if (isFixedPoint.get(j) && j < pos) {
453
                                return j;
454
                            }
455
                        }
456
                    }
457
                    try {
458
                        return findNearestPrevFixedPoint(lastPositionInLine);
459
                    } catch (Exception e) {
460
                        return null;
461
                    }
462
                }
463
            }
464
        }
465
        return null;
466
    }
467

  
468
    private Integer findPrevFixedPointInLine(int vertexPos) {
469
        int lineIndex = lineIndexes.get(vertexPos);
470
        for (int i = vertexPos - 1; i >= 0; i--) {
471
            if (!lineIndexes.get(i).equals(lineIndex)) {
472
                return null;
473
            }
474
            if (isFixedPoint.get(i)) {
475
                return i;
476
            }
477
        }
478
        return null;
479
    }
480

  
481
    private Integer findNearestPrevFixedPoint(int vertexPos) throws GeometryOperationNotSupportedException, GeometryOperationException {
482
        Point vertex = vertices.get(vertexPos);
483
        double nearestDistance = Double.POSITIVE_INFINITY;
484
        Integer nearestPointPos = null;
485
        for (int i = vertexPos - 1; i >= 0; i--) {
486
            if (isFixedPoint.get(i)) {
487
                Double distance = vertex.distance(vertices.get(i));
488
                if (nearestDistance > distance) {
489
                    nearestDistance = distance;
490
                    nearestPointPos = i;
491
                }
492
            }
493
        }
494
        return nearestPointPos;
495
    }
496

  
497
    private void refreshMValueGeometryVertex(int i, Double mValue, boolean fixed) {
498
        List<Point> points = LrsAlgorithmUtils.extractPoints(route);
499
        vertices.get(i).setCoordinateAt(MDIMENSION, mValue);
500
        points.get(i).setCoordinateAt(MDIMENSION, mValue);
501
        isFixedPoint.set(i, fixed);
502
    }
503

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

  
25
package org.gvsig.lrs.lib.impl;
26

  
27
import org.gvsig.lrs.lib.api.LrsAlgorithmsLibrary;
28
import org.gvsig.lrs.lib.api.LrsAlgorithmsLocator;
29
import org.gvsig.tools.library.AbstractLibrary;
30
import org.gvsig.tools.library.LibraryException;
31

  
32
/**
33
 * @author fdiaz</a>
34
 *
35
 */
36
public class DefaultLrsAlgorithmsLibrary extends AbstractLibrary {
37

  
38
    @Override
39
    public void doRegistration() {
40
        registerAsImplementationOf(LrsAlgorithmsLibrary.class);
41
    }
42

  
43
    @Override
44
    protected void doInitialize() throws LibraryException {
45
        LrsAlgorithmsLocator.registerLrsAlgorithmsManager(DefaultLrsAlgorithmsManager.class);
46
    }
47

  
48
    @Override
49
    protected void doPostInitialize() throws LibraryException {
50
    }
51

  
52
}
org.gvsig.lrs/tags/org.gvsig.lrs-1.0.24/org.gvsig.lrs.lib/org.gvsig.lrs.lib.impl/src/main/java/org/gvsig/lrs/lib/impl/LrsGenerateDynamicSegmentationAlgorithm.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.io.File;
26
import java.util.Iterator;
27
import java.util.List;
28

  
29
import org.apache.commons.lang3.StringUtils;
30
import org.slf4j.Logger;
31
import org.slf4j.LoggerFactory;
32

  
33
import org.gvsig.fmap.dal.DALLocator;
34
import org.gvsig.fmap.dal.DataManager;
35
import org.gvsig.fmap.dal.DataServerExplorer;
36
import org.gvsig.fmap.dal.DataServerExplorerParameters;
37
import org.gvsig.fmap.dal.DataStore;
38
import org.gvsig.fmap.dal.DataTypes;
39
import org.gvsig.fmap.dal.feature.EditableFeature;
40
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
41
import org.gvsig.fmap.dal.feature.EditableFeatureType;
42
import org.gvsig.fmap.dal.feature.Feature;
43
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
44
import org.gvsig.fmap.dal.feature.FeatureQuery;
45
import org.gvsig.fmap.dal.feature.FeatureSet;
46
import org.gvsig.fmap.dal.feature.FeatureStore;
47
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
48
import org.gvsig.fmap.dal.store.shp.SHPNewStoreParameters;
49
import org.gvsig.fmap.dal.store.shp.SHPStoreProvider;
50
import org.gvsig.fmap.geom.Geometry;
51
import org.gvsig.fmap.geom.GeometryLocator;
52
import org.gvsig.fmap.geom.aggregate.MultiLine;
53
import org.gvsig.fmap.geom.primitive.Point;
54
import org.gvsig.fmap.geom.type.GeometryType;
55
import org.gvsig.lrs.lib.api.LrsAlgorithm;
56
import org.gvsig.lrs.lib.api.LrsAlgorithmParams;
57
import org.gvsig.lrs.lib.api.LrsCreateRouteAlgorithmParams;
58
import org.gvsig.lrs.lib.api.LrsGenerateDynamicSegmentationAlgorithmParams;
59
import org.gvsig.lrs.lib.api.exceptions.LrsCreateRouteException;
60
import org.gvsig.lrs.lib.api.exceptions.LrsException;
61
import org.gvsig.tools.ToolsLocator;
62
import org.gvsig.tools.dataTypes.DataType;
63
import org.gvsig.tools.exception.BaseException;
64
import org.gvsig.tools.i18n.I18nManager;
65
import org.gvsig.tools.locator.LocatorException;
66
import org.gvsig.tools.service.Manager;
67
import org.gvsig.tools.task.SimpleTaskStatus;
68
import org.gvsig.tools.visitor.VisitCanceledException;
69
import org.gvsig.tools.visitor.Visitor;
70

  
71
/**
72
 * @author fdiaz
73
 *
74
 */
75
public class LrsGenerateDynamicSegmentationAlgorithm implements LrsAlgorithm {
76

  
77
    private static final Logger logger = LoggerFactory.getLogger(LrsGenerateDynamicSegmentationAlgorithm.class);
78

  
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff