Revision 35

View differences:

org.gvsig.lrs/trunk/org.gvsig.lrs/org.gvsig.lrs.app/org.gvsig.lrs.app.mainplugin/src/main/java/org/gvsig/lrs/app/calibrateroute/CalibrateRouteExtension.java
43 43
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
44 44
import org.gvsig.fmap.dal.feature.FeatureStore;
45 45
import org.gvsig.fmap.dal.feature.FeatureType;
46
import org.gvsig.fmap.dal.store.shp.SHPNewStoreParameters;
46 47
import org.gvsig.fmap.geom.DataTypes;
47 48
import org.gvsig.fmap.geom.Geometry;
48 49
import org.gvsig.fmap.geom.type.GeometryType;
50
import org.gvsig.fmap.mapcontext.MapContextLocator;
51
import org.gvsig.fmap.mapcontext.MapContextManager;
52
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
49 53
import org.gvsig.fmap.mapcontext.layers.FLayer;
50 54
import org.gvsig.fmap.mapcontext.layers.FLayers;
51 55
import org.gvsig.fmap.mapcontext.layers.operations.LayerCollection;
......
135 139
                    }
136 140
                    LrsAlgorithmsManager algorithmsManager = LrsAlgorithmsLocator.getLrsAlgorithmsManager();
137 141
                    final LrsAlgorithm algorithm = algorithmsManager.createLrsAlgorithm(params);
142
                    final SHPNewStoreParameters shpParams=(SHPNewStoreParameters)params.getNewFeatureStoreParameters();
143

  
138 144
                    final SimpleTaskStatus taskStatus =
139 145
                        ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus(i18nManager.getTranslation("progress"));
140 146

  
......
148 154
                        public void run() {
149 155
                            try {
150 156
                                algorithm.execute(taskStatus);
157
                                int resultLoadLayer =JOptionPane.showConfirmDialog(
158
                                    null,
159
                                    i18nManager.getTranslation("load_layer_question"),
160
                                    i18nManager.getTranslation("load_layer_question_title"), JOptionPane.YES_NO_OPTION);
161
                                if (resultLoadLayer==JOptionPane.YES_OPTION){
162
                                    loadLayer(shpParams);
163
                                }
151 164
                            } catch (Exception e) {
152 165
                                logger.error("Error calibrating route", e);
153 166
                                String message = e.getMessage();
......
214 227
        return view;
215 228
    }
216 229

  
230
    private void loadLayer(SHPNewStoreParameters shpParams){
231
        if (shpParams!=null ){
232
            String name=shpParams.getFile().getName();
233
            if (name.contains(".shp")){
234
                name=name.substring(0,name.lastIndexOf(".shp"));
235
            }
236
            MapContextManager mapContextManager =MapContextLocator.getMapContextManager();
237

  
238
            FLyrVect newLayer=null;
239
            try {
240
                newLayer = (FLyrVect) mapContextManager.createLayer(name,shpParams);
241
            } catch (LoadLayerException e) {
242
                logger.warn("Couldn't load layer",e);
243
            }
244
            getActiveView().getMapControl().getMapContext().getLayers().addLayer(newLayer);
245
        }
246
    }
247

  
217 248
}
org.gvsig.lrs/trunk/org.gvsig.lrs/org.gvsig.lrs.app/org.gvsig.lrs.app.mainplugin/src/main/resources-plugin/i18n/text.properties
49 49
measure_calc_method=M\u00E9todo de c\u00e1lculo de mediciones
50 50
search_radius=Radio de b\u00FAsqueda
51 51
interpolate_between_calibration_points=Interpolar entre puntos de calibraci\u00f3n
52
measure_units=Unidades
52
units=Unidades
53 53
extrapolate_before_calibration_points=Extrapolar antes de puntos de calibraci\u00f3n
54 54
extrapolate_after_calibration_points=Extrapolar despu\u00E9s de puntos de calibraci\u00f3n
55 55
include_all=Incluir todos los elementos en la capa de salida
......
68 68
error_getting_selected_item_from_cmbFromMeasureField=Error obteniendo el valor seleccionado del campo desde
69 69
need_to_select_a_from_Measure_field=Es necesario seleccionar un valor desde
70 70
calibrating_features=Calibrando elementos
71
kilometer=kil\u00f3metro
72
meter=metro
73
centimeter=cent\u00edmetro
74
millimeter=mil\u00edmetro
75
mile=milla
76
yards=yardas
77
feet=pies
78
inch=pulgada
71 79

  
72 80
#EDIT ROUTE
73 81
a_layer_is_needed=Es necesaria una capa
org.gvsig.lrs/trunk/org.gvsig.lrs/org.gvsig.lrs.app/org.gvsig.lrs.app.mainplugin/src/main/resources-plugin/i18n/text_en.properties
49 49
measure_calc_method=Measure calculations method
50 50
search_radius=Search radius
51 51
interpolate_between_calibration_points=Interpolate between calibration points
52
measure_units=Units
52
units=Units
53 53
extrapolate_before_calibration_points=Extrapolate before calibration points
54 54
extrapolate_after_calibration_points=Extrapolate after calibration points
55 55
include_all=Include all elements in output layer
......
68 68
error_getting_selected_item_from_cmbFromMeasureField=Error getting selected item from From-measure field
69 69
need_to_select_a_from_Measure_field=You need to select a From-measure field
70 70
calibrating_features=Calibrating features
71
kilometer=kilometer
72
meter=meter
73
centimeter=centimeter
74
millimeter=millimeter
75
mile=mile
76
yards=yards
77
feet=feet
78
inch=inch
71 79

  
72 80
#EDIT ROUTE
73 81
a_layer_is_needed=A layer is needed
org.gvsig.lrs/trunk/org.gvsig.lrs/org.gvsig.lrs.lib/org.gvsig.lrs.lib.api/src/main/java/org/gvsig/lrs/lib/api/exceptions/LrsCalibrateRouteException.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright ? 2007-2014 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.api.exceptions;
26

  
27
public class LrsCalibrateRouteException extends LrsException {
28

  
29
    /**
30
     *
31
     */
32
    private static final long serialVersionUID = -7593469257155358436L;
33

  
34
    private static final String MESSAGE =
35
        "Error calibrating routes.";
36

  
37
    private static final String KEY = "_LrsCalibrateRouteException";
38

  
39
    public LrsCalibrateRouteException(Throwable ex) {
40
        super(MESSAGE, ex, KEY, serialVersionUID);
41
    }
42

  
43
    public LrsCalibrateRouteException(String message, Throwable ex) {
44
        super(message, ex, KEY, serialVersionUID);
45
    }
46

  
47
}
org.gvsig.lrs/trunk/org.gvsig.lrs/org.gvsig.lrs.lib/org.gvsig.lrs.lib.impl/src/main/java/org/gvsig/lrs/lib/impl/LrsAlgorithmUtils.java
447 447
        if (geometry instanceof MultiLine){
448 448
            List<Line> lines=extractLines(geometry);
449 449
            for(Line line:lines){
450
                if (index>line.getNumVertices()){
450
                if (index<=line.getNumVertices()){
451 451
                    line.insertVertex(index, vertex);
452
                    return;
452 453
                }else{
453 454
                    index-=line.getNumVertices();
454 455
                }
......
457 458
        }
458 459
    }
459 460

  
461
    static protected Double calculateM(Double maxValue, Double minValue,
462
        Double relativeDistance, Double totalLength) {
463
        // mValue=((newMax-newMin)*(mCoordinate-oldMin)/(oldMax-oldMin))+newMin;
464
        if (totalLength.equals(Double.valueOf(0)))
465
            return Double.POSITIVE_INFINITY;
466
        return ((maxValue - minValue) * (relativeDistance) / (totalLength))
467
            + minValue;
468
    }
460 469

  
470
    static protected double calculateNewM(double oldMax, double oldMin, double newMax, double newMin, double mCoordinate){
471
        double result;
472
        if (oldMax==oldMin){
473
            return Double.POSITIVE_INFINITY;
474
        }
475
        result=((newMax-newMin)*(mCoordinate-oldMin)/(oldMax-oldMin))+newMin;
476

  
477
        return result;
478
    }
479

  
461 480
}
org.gvsig.lrs/trunk/org.gvsig.lrs/org.gvsig.lrs.lib/org.gvsig.lrs.lib.impl/src/main/java/org/gvsig/lrs/lib/impl/LrsCalibrateRouteAlgorithm.java
22 22
 */
23 23
package org.gvsig.lrs.lib.impl;
24 24

  
25
import java.io.File;
26 25
import java.util.ArrayList;
27 26
import java.util.HashMap;
28
import java.util.Iterator;
29 27
import java.util.List;
30 28
import java.util.Map;
31 29
import java.util.Map.Entry;
32 30

  
33
import org.apache.commons.lang3.mutable.Mutable;
34
import org.apache.commons.lang3.mutable.MutableInt;
35
import org.gvsig.fmap.dal.DALLocator;
36
import org.gvsig.fmap.dal.DataManager;
37
import org.gvsig.fmap.dal.DataServerExplorer;
38
import org.gvsig.fmap.dal.DataServerExplorerParameters;
39
import org.gvsig.fmap.dal.DataStore;
40
import org.gvsig.fmap.dal.DataStoreParameters;
41
import org.gvsig.fmap.dal.DataTypes;
42
import org.gvsig.fmap.dal.exception.DataException;
31
import org.cresques.cts.IProjection;
43 32
import org.gvsig.fmap.dal.feature.EditableFeature;
44
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
45
import org.gvsig.fmap.dal.feature.EditableFeatureType;
46 33
import org.gvsig.fmap.dal.feature.Feature;
47 34
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
48
import org.gvsig.fmap.dal.feature.FeatureReference;
49
import org.gvsig.fmap.dal.feature.FeatureSelection;
50 35
import org.gvsig.fmap.dal.feature.FeatureSet;
51 36
import org.gvsig.fmap.dal.feature.FeatureStore;
52 37
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
53
import org.gvsig.fmap.dal.store.shp.SHPNewStoreParameters;
54 38
import org.gvsig.fmap.geom.Geometry;
55 39
import org.gvsig.fmap.geom.GeometryLocator;
56 40
import org.gvsig.fmap.geom.GeometryManager;
57
import org.gvsig.fmap.geom.aggregate.MultiLine;
58
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
59 41
import org.gvsig.fmap.geom.exception.CreateGeometryException;
60 42
import org.gvsig.fmap.geom.operation.GeometryOperationException;
61 43
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
62
import org.gvsig.fmap.geom.primitive.Envelope;
63 44
import org.gvsig.fmap.geom.primitive.Line;
64 45
import org.gvsig.fmap.geom.primitive.Point;
65
import org.gvsig.fmap.geom.type.GeometryType;
66 46
import org.gvsig.lrs.lib.api.DistanceUnits;
67 47
import org.gvsig.lrs.lib.api.LrsAlgorithm;
68 48
import org.gvsig.lrs.lib.api.LrsAlgorithmParams;
69 49
import org.gvsig.lrs.lib.api.LrsCalibrateRouteAlgorithmParams;
70
import org.gvsig.lrs.lib.api.LrsCoordinatesPriority;
71 50
import org.gvsig.lrs.lib.api.LrsCreateRouteAlgorithmParams;
72 51
import org.gvsig.lrs.lib.api.LrsMeasureCalculationMethods;
73
import org.gvsig.lrs.lib.api.LrsSourceOfMeasures;
74
import org.gvsig.lrs.lib.api.exceptions.LrsCreateRouteException;
52
import org.gvsig.lrs.lib.api.exceptions.LrsCalibrateRouteException;
75 53
import org.gvsig.lrs.lib.api.exceptions.LrsException;
76 54
import org.gvsig.tools.ToolsLocator;
77
import org.gvsig.tools.dataTypes.DataType;
78 55
import org.gvsig.tools.exception.BaseException;
79 56
import org.gvsig.tools.i18n.I18nManager;
80 57
import org.gvsig.tools.service.Manager;
......
135 112
     * @see org.gvsig.lrs.lib.api.LrsAlgorithm#execute(org.gvsig.tools.task.SimpleTaskStatus)
136 113
     */
137 114
    public void execute(final SimpleTaskStatus taskStatus) throws LrsException {
138
        NewFeatureStoreParameters newFeatureStoreParameters = parameters.getNewFeatureStoreParameters();
139

  
115
        NewFeatureStoreParameters newFeatureStoreParameters =
116
            parameters.getNewFeatureStoreParameters();
140 117
        FeatureStore sourceFeatureStore = parameters.getSourceFeatureStore();
141 118
        FeatureAttributeDescriptor idRouteField = parameters.getIdRouteField();
119
        FeatureStore calibratePointFeatureStore= parameters.getCalibratePointFeatureStore();
120
        FeatureAttributeDescriptor idRouteCalibratePointField =parameters.getCalibratePointIdRouteField();
121
        FeatureAttributeDescriptor fromMeasureField = parameters.getFromMeasureField();
122
        final boolean includeAll=parameters.includeAll();
142 123

  
143 124
        logger.info(parameters.toString());
144 125

  
145 126
        taskStatus.setTitle(parameters.getName());
146 127
        I18nManager i18nManager = ToolsLocator.getI18nManager();
147
        taskStatus.message(i18nManager.getTranslation("calibrating_features"));
128
        taskStatus.message(i18nManager.getTranslation("grouping_features"));
148 129

  
149 130
        try {
150
            final FeatureStore newFeatureStore=LrsAlgorithmUtils.createNewDataStore(newFeatureStoreParameters,idRouteField);
151
            newFeatureStore.edit(FeatureStore.MODE_FULLEDIT);
131
            final String routeFieldName = idRouteField.getName();
132
            final String routeCalibratePointFieldName=idRouteCalibratePointField.getName();
133
            final String measureFieldName=fromMeasureField.getName();
134
            final Double convertedSearchRadius=calculateConvertedSearchRadius();
152 135

  
136
            FeatureStore newFeatureStore = LrsAlgorithmUtils
137
                .createNewDataStore(newFeatureStoreParameters, idRouteField);
138

  
153 139
            FeatureSet sourceFeatures;
154
            if (sourceFeatureStore.getFeatureSelection().getSize()>0){
155
                sourceFeatures=sourceFeatureStore.getFeatureSelection();
156
            }else{
157
                sourceFeatures=sourceFeatureStore.getFeatureSet();
140
            if (sourceFeatureStore.getFeatureSelection().getSize() > 0) {
141
                sourceFeatures = sourceFeatureStore.getFeatureSelection();
142
            } else {
143
                sourceFeatures = sourceFeatureStore.getFeatureSet();
158 144
            }
159 145

  
160
            taskStatus.setRangeOfValues(0, sourceFeatures.getSize()-1);
161
            final MutableInt taskCount=new MutableInt();
162
            taskCount.setValue(0);
163

  
146
            final Map<String, Object[]> featuresMap =
147
                new HashMap<String, Object[]>();
164 148
            sourceFeatures.accept(new Visitor() {
165 149

  
166
                public void visit(Object obj) throws VisitCanceledException, BaseException {
167
                    Feature feature=(Feature)obj;
150
                public void visit(Object obj)
151
                    throws VisitCanceledException, BaseException {
152
                    Feature feature = (Feature) obj;
153
                    String routeName = (String) feature.get(routeFieldName);
154
                    List<Point> points = new ArrayList<Point>();
155
                    Object[] valueObject=new Object[2];
156
                    valueObject[0]=feature.getDefaultGeometry();
157
                    valueObject[1]=points;
168 158

  
169
                    EditableFeature calibratedFeature=calibrateRoute(feature);
159
                    featuresMap.put(routeName, valueObject);
160
                }
161
            });
170 162

  
171
                    newFeatureStore.update(calibratedFeature);
163
            FeatureSet pointFeatures=calibratePointFeatureStore.getFeatureSet();
164
            pointFeatures.accept(new Visitor() {
172 165

  
173
                    taskStatus.setCurValue(taskCount.getValue());
174
                    taskCount.increment();
166
                public void visit(Object obj) throws VisitCanceledException, BaseException {
167
                    Feature feature = (Feature) obj;
168
                    String routeName = (String) feature.get(routeCalibratePointFieldName);
169
                    Double measure= (Double) feature.get(measureFieldName);
170
                    Geometry geomPoint=(Point)feature.getDefaultGeometry();
171

  
172
                    if (geomPoint instanceof Point){
173
                        Point point=(Point) geomPoint;
174
                        Object valueObject=featuresMap.get(routeName);
175
                        if (valueObject!=null && valueObject instanceof Object[]){
176
                            Object[] objArray=(Object[])valueObject;
177
                            if (objArray[1] instanceof List){
178
                                List<Point> points=(List<Point>)objArray[1];
179
                                if (feature.getDefaultGeometry() instanceof Point){
180
                                    Geometry geometry=(Geometry)objArray[0];
181
                                    if (isValidPoint(point,geometry, convertedSearchRadius)){
182
                                        try {
183
                                            Point mPoint= pointToMPoint(geometry,point, measure);
184
                                            points.add(mPoint);
185
                                        } catch (Exception e) {
186
                                            logger.debug("Error adding point",e);
187
                                        }
188
                                    }
189
                                }
190
                            }
191
                        }
192
                    }
175 193
                }
176 194
            });
177
            newFeatureStore.finishEditing();
178 195

  
196
            taskStatus.setRangeOfValues(0, featuresMap.size());
197
            int taskCount = 0;
179 198

  
199
            newFeatureStore.edit(FeatureStore.MODE_FULLEDIT);
200

  
201
            for (Entry<String, Object[]> entry : featuresMap
202
                .entrySet()) {
203
                String routeName = entry.getKey();
204
                Object valueObject = entry.getValue();
205
                List<Point> points=null;
206
                Geometry geometry=null;
207

  
208
                if (valueObject!=null && valueObject instanceof Object[]){
209
                    Object[] objArray=(Object[])valueObject;
210
                    if (objArray[0] instanceof Geometry){
211
                        geometry=(Geometry)objArray[0];
212
                    }
213
                    if (objArray[1] instanceof List){
214
                        points=(List<Point>)objArray[1];
215
                    }
216
                }
217
                if (!points.isEmpty()){
218
                    EditableFeature newFeature =
219
                        newFeatureStore.createNewFeature(true);
220
                    newFeature.set(routeFieldName, routeName);
221
                    Geometry calibratedRoute = calibrateRoute(geometry,points);
222
                    newFeature.setDefaultGeometry(calibratedRoute);
223
                    newFeatureStore.update(newFeature);
224
                }else{
225
                    if (includeAll){
226
                        EditableFeature newFeature =
227
                            newFeatureStore.createNewFeature(true);
228
                        newFeature.set(routeFieldName, routeName);
229
                        newFeature.setDefaultGeometry(geometry);
230
                        newFeatureStore.update(newFeature);
231
                    }
232
                }
233
                taskCount++;
234
                taskStatus.setCurValue(taskCount);
235
            }
236
            newFeatureStore.finishEditing();
180 237
        } catch (Exception e1) {
181 238
            taskStatus.abort();
182
            throw new LrsCreateRouteException("Error calibrating routes", e1);
239
            throw new LrsCalibrateRouteException("Error calibrating routes", e1);
183 240
        }
184 241

  
185 242
        taskStatus.terminate();
186
        taskStatus.terminate();
187 243
    }
188 244

  
189
    private EditableFeature calibrateRoute(Feature feature) {
190
        FeatureStore calibratePointFeatureStore= parameters.getCalibratePointFeatureStore();
191
        FeatureAttributeDescriptor idRouteCalibratePointField =parameters.getCalibratePointIdRouteField();
192
        FeatureAttributeDescriptor fromMeasureField = parameters.getFromMeasureField();
193 245

  
194
        LrsMeasureCalculationMethods calculationMethod= parameters.getMeasureCalculationMethods();
246
    private Point pointToMPoint(Geometry geometry,Point point, Double measure) throws CreateGeometryException, GeometryOperationNotSupportedException, GeometryOperationException{
247
        GeometryManager geomanager = GeometryLocator.getGeometryManager();
248

  
249
        Point[] pointsInLine=(Point[])geometry.closestPoints(point);
250
        Point pointInLine=pointsInLine[0];
251

  
252
        Point mPoint = (Point) geomanager
253
            .create(Geometry.TYPES.POINT, Geometry.SUBTYPES.GEOM2DM);
254
        mPoint.setX(pointInLine.getX());
255
        mPoint.setY(pointInLine.getY());
256
        int mDimension=mPoint.getDimension()-1;
257
        mPoint.setCoordinateAt(mDimension, measure);
258

  
259
        return mPoint;
260
    }
261

  
262
    private Double calculateConvertedSearchRadius(){
195 263
        Double searchRadius=parameters.getSearchRadius();
196 264
        DistanceUnits measureUnits = parameters.getMeasureUnits();
265
        FeatureStore sourceFeatureStore = parameters.getSourceFeatureStore();
266

  
267
        IProjection projection;
268
        try {
269
            projection = sourceFeatureStore.getDefaultFeatureType().getDefaultSRS();
270

  
271
            Double searchRadiusInMeters=searchRadius*measureUnits.getTransToMeter();
272
            //TODO
273
            //DistanceUnits projectionDistanceUnits=getProjectionDistanceUnits(projection);
274
            //return searchRadiusInMeters/projectionDistanceUnits.getTransToMeter();
275
            return searchRadiusInMeters;
276
        } catch (Exception e) {
277
            return new Double(0);
278
        }
279
    }
280

  
281
    private boolean isValidPoint(Point point,Geometry geometry, Double convertedSearchRadius){
282
        try {
283
            return geometry.isWithinDistance(point, convertedSearchRadius);
284
        } catch (Exception e) {
285
            return false;
286
        }
287
    }
288

  
289
    private Geometry calibrateRoute(Geometry geometry, List<Point> points) {
290
        LrsMeasureCalculationMethods calculationMethod= parameters.getMeasureCalculationMethods();
291
        boolean ignoreSpatialGaps = parameters.ignoreSpatialGaps();
197 292
        boolean interpolateBetweenCalibrationPoints=parameters.interpolateBetweenCalibrationPoints();
198 293
        boolean extrapolateBeforeCalibrationPoints=parameters.extrapolateBeforeCalibrationPoints();
199 294
        boolean extrapolateAfterCalibrationPoints=parameters.extrapolateAfterCalibrationPoints();
200
        boolean ignoreSpatialGaps = parameters.ignoreSpatialGaps();
201
        boolean includeAll=parameters.includeAll();
202 295

  
203
        // TODO Must calculate the calibrated Feature
296
        if (points.isEmpty()){
297
            return geometry;
298
        }
299
        ExpandedRoute expandedRoute=new ExpandedRoute(geometry, ignoreSpatialGaps);
204 300

  
205
        return null;
301

  
302
        switch (calculationMethod) {
303
        case DISTANCE:
304
            expandedRoute.resetMeasuresToDistances();
305
        case MEASURES:
306
        default:
307
            expandedRoute.addFixedPoints(points);
308
            if (interpolateBetweenCalibrationPoints){
309
                expandedRoute.interpolateBetweenCalibrationPoints();
310
            }
311
            if (extrapolateBeforeCalibrationPoints){
312
                expandedRoute.extrapolateBeforeCalibrationPoints();
313
            }
314
            if (extrapolateAfterCalibrationPoints){
315
                expandedRoute.extrapolateAfterCalibrationPoints();
316
            }
317
            return expandedRoute.getRoute();
318
        }
319

  
206 320
    }
207 321

  
208 322

  
209
    class MStructure{
210
        Geometry geometry;
211
        Double fromField;
212
        Double toField;
323

  
324

  
325

  
326

  
327
    class ExpandedRoute{
328
        private Geometry route;
329
        private List<Boolean> isFixedPoint;
330
        private List<Double> distances;
331
        private List<Double>oldMValues;
332
        private List<Point> vertices;
333
        private int MDIMENSION;
334
        private int maxMFixedPointPos;
335
        private int minMFixedPointPos;
336
        private final Double PRECISION=new Double(1.0e-10);
337

  
338
        public Geometry getRoute(){
339
            return route;
340
        }
341

  
342
        public ExpandedRoute(Geometry geometry, boolean ignoreSpatialGaps){
343
            isFixedPoint=new ArrayList<Boolean>();
344
            distances=new ArrayList<Double>();
345
            oldMValues=new ArrayList<Double>();
346
            vertices=new ArrayList<Point>();
347
            route=geometry.cloneGeometry();
348

  
349
            double distance = 0;
350
            List<Line> lines=LrsAlgorithmUtils.extractLines(geometry);
351
            Point previousVertex=null;
352
            for (int i=0; i<lines.size();i++){
353
                Line line=lines.get(i);
354
                for (int j=0; j<line.getNumVertices();j++){
355
                    Point vertex=line.getVertex(j);
356
                    if(j==0){
357
                       if (previousVertex==null){
358
                           //First point in geometry
359
                           MDIMENSION=vertex.getDimension()-1;
360
                           distance=0;
361
                       }
362
                       Integer visitedIndex = getIndexVisitedVertex(vertex);
363
                       if (visitedIndex!=null){
364
                           previousVertex=vertices.get(visitedIndex);
365
                           distance=distances.get(visitedIndex);
366
                       }else{
367
                           if (previousVertex!=null){
368
                               try {
369
                                   if (!ignoreSpatialGaps){
370
                                       distance+=previousVertex.distance(vertex);
371
                                   }
372
                               } catch (Exception e) {
373
                                   distance=Double.NaN;
374
                               }
375
                           }
376
                       }
377
                    }else{
378
                        try {
379
                            distance+=vertex.distance(previousVertex);
380
                        } catch (Exception e) {
381
                            distance=Double.NaN;
382
                        }
383
                    }
384
                    Double mValue=vertex.getCoordinateAt(MDIMENSION);
385

  
386
                    isFixedPoint.add(false);
387
                    vertices.add(vertex);
388
                    distances.add(distance);
389
                    oldMValues.add(mValue);
390

  
391
                    previousVertex = vertex;
392
                }
393
            }
394
        }
395

  
396
        private Integer getIndexVisitedVertex(Point point){
397
            double distance = Double.NEGATIVE_INFINITY;
398
            Integer index = null;
399
            for(int i =0; i<vertices.size(); i++){
400
                if (LrsAlgorithmUtils.equalPoints(point, vertices.get(i))){
401
                    if(this.distances.get(i)>distance){
402
                        distance = this.distances.get(i);
403
                        index = i;
404
                    }
405
                }
406
            }
407
            return index;
408
        }
409

  
410
        private void addFixedPoints(List<Point> fixedPoints){
411
            Double minMValue=null;
412
            Double maxMValue=null;
413
            for (Point fixedPoint:fixedPoints){
414
                Double length=Double.valueOf(0);
415
                int index = 0;
416
                List<Line> lines=LrsAlgorithmUtils.extractLines(route);
417
                List<Integer>insertAtList=new ArrayList<Integer>();
418
                List<Double>lengths=new ArrayList<Double>();
419
                List<Double>oldValues=new ArrayList<Double>();
420
                for (int i=0; i<lines.size();i++){
421
                    Line line=lines.get(i);
422
                    try{
423
                        //FIXME
424
                        //if (line.intersects(fixedPoint)){
425
                        if (line.isWithinDistance(fixedPoint, PRECISION)){
426
                            length=distances.get(index);
427
                            for (int j=0;j<line.getNumVertices();j++){
428
                                Point vertex=line.getVertex(j);
429
                                Point nextVertex;
430
                                if (j+1<line.getNumVertices()){
431
                                    nextVertex=line.getVertex(j+1);
432
                                }else{
433
                                    nextVertex=null;
434
                                }
435
                                if(LrsAlgorithmUtils.equalPoints(vertex, fixedPoint)){
436
                                    oldMValues.add(index, vertex.getCoordinateAt(MDIMENSION));
437
                                    Double mValue=fixedPoint.getCoordinateAt(MDIMENSION);
438
                                    vertex.setCoordinateAt(MDIMENSION, mValue);
439
                                    isFixedPoint.set(index, true);
440
                                    vertices.set(index, vertex);
441

  
442
                                    if (minMValue==null||mValue.compareTo(minMValue)<0){
443
                                        minMValue=mValue;
444
                                        minMFixedPointPos=index;
445
                                    }
446
                                    if (maxMValue==null||mValue.compareTo(maxMValue)>=0){
447
                                        maxMValue=mValue;
448
                                        maxMFixedPointPos=index;
449
                                    }
450
                                }else{
451
                                    if (nextVertex!=null && !LrsAlgorithmUtils.equalPoints(nextVertex, fixedPoint)){
452
                                        GeometryManager geomanager = GeometryLocator.getGeometryManager();
453
                                        Line segment= (Line) geomanager
454
                                            .create(Geometry.TYPES.LINE, Geometry.SUBTYPES.GEOM2DM);
455
                                        segment.addVertex(vertex);
456
                                        segment.addVertex(nextVertex);
457
                                        //FIXME
458
                                        //if (line.intersects(fixedPoint)){
459
                                        if (segment.isWithinDistance(fixedPoint, PRECISION)){
460
                                            length+=vertex.distance(fixedPoint);
461
                                            Double oldMValue=LrsAlgorithmUtils.calculateNewM(
462
                                                nextVertex.getCoordinateAt(MDIMENSION),
463
                                                vertex.getCoordinateAt(MDIMENSION),
464
                                                distances.get(index+1),
465
                                                distances.get(index),
466
                                                length);
467
                                            insertAtList.add(index+1);
468
                                            lengths.add(length);
469
                                            oldValues.add(oldMValue);
470
                                        }
471
                                    }
472
                                }
473
                                index++;
474
                            }
475
                        }else{
476
                            index+=line.getNumVertices();
477
                        }
478
                    }catch(Exception e){
479
                        logger.warn("Error inserting point "+fixedPoint.toString(),e);
480
                    }
481
                }
482
                int pos=0;
483
                for (Integer insertAt:insertAtList){
484
                    Double distance=lengths.get(pos);
485
                    Double oldMValue=oldValues.get(pos);
486
                    int correctedPosition=insertAt+pos;
487
                    LrsAlgorithmUtils.insertVertex(route, fixedPoint, correctedPosition);
488
                    isFixedPoint.add(correctedPosition, true);
489
                    distances.add(correctedPosition, distance);
490
                    oldMValues.add(correctedPosition, oldMValue);
491

  
492
                    vertices.add(correctedPosition, fixedPoint);
493
                    correctPosteriorPositions(correctedPosition);
494

  
495
                    Double mValue=fixedPoint.getCoordinateAt(MDIMENSION);
496
                    if (minMValue==null||mValue.compareTo(minMValue)<0){
497
                        minMValue=mValue;
498
                        minMFixedPointPos=correctedPosition;
499
                    }
500
                    if (maxMValue==null||mValue.compareTo(maxMValue)>=0){
501
                        maxMValue=mValue;
502
                        maxMFixedPointPos=correctedPosition;
503
                    }
504
                    pos++;
505
                }
506
            }
507
        }
508

  
509
        private void correctPosteriorPositions(int pos){
510
            if (minMFixedPointPos>=pos){
511
                minMFixedPointPos++;
512
            }
513
            if (maxMFixedPointPos>=pos){
514
                maxMFixedPointPos++;
515
            }
516

  
517
        }
518

  
519
        public void resetMeasuresToDistances(){
520
             List<Point> points=LrsAlgorithmUtils.extractPoints(route);
521
             for (int i=0;i<points.size();i++){
522
                 if (!isFixedPoint.get(i)){
523
                     points.get(i).setCoordinateAt(MDIMENSION, distances.get(i));
524
                     vertices.get(i).setCoordinateAt(MDIMENSION, distances.get(i));
525
                 }
526
             }
527
        }
528

  
529
        public void interpolateBetweenCalibrationPoints(){
530
            Double newMValueBeforePoint=Double.NaN;
531
            Double oldMValueBeforePoint=Double.NaN;
532
            Double calibrationRatio=Double.NaN;
533
            for (int i=minMFixedPointPos; i<=maxMFixedPointPos-1;i++){
534
                if (isFixedPoint.get(i)){
535
                    int pointBeforePos=i;
536
                    int pointAfterPos=getNextPosFixedPoint(i);
537

  
538
                    newMValueBeforePoint=vertices.get(pointBeforePos).getCoordinateAt(MDIMENSION);
539
                    Double newMValueAfterPoint=vertices.get(pointAfterPos).getCoordinateAt(MDIMENSION);
540

  
541
                    oldMValueBeforePoint=oldMValues.get(pointBeforePos);
542
                    Double oldMValueAfterPoint=oldMValues.get(pointAfterPos);
543

  
544
                    Double distanceBetweenFixedPoints=newMValueAfterPoint-newMValueBeforePoint;
545
                    Double distanceBetweenOldFixedPoints=oldMValueAfterPoint-oldMValueBeforePoint;
546

  
547
                    calibrationRatio=distanceBetweenOldFixedPoints/distanceBetweenFixedPoints;
548
                }else{
549
                    Double valueToRecalculate=vertices.get(i).getCoordinateAt(MDIMENSION);
550
                    Double distanceBetween=valueToRecalculate-oldMValueBeforePoint;
551
                    Double mValue=newMValueBeforePoint+(distanceBetween/calibrationRatio);
552
                    refreshMValueGeometryVertex(i,mValue);
553
                }
554
            }
555
        }
556

  
557

  
558
        public void extrapolateBeforeCalibrationPoints(){
559
            int nextFixedPointPos=getNextPosFixedPoint(minMFixedPointPos);
560
            Double newMValueFirstPoint=vertices.get(minMFixedPointPos).getCoordinateAt(MDIMENSION);
561
            Double newMValueSecondPoint=vertices.get(nextFixedPointPos).getCoordinateAt(MDIMENSION);
562

  
563
            Double oldMValueFirstPoint=oldMValues.get(minMFixedPointPos);
564
            Double oldMValueSecondPoint=oldMValues.get(nextFixedPointPos);
565

  
566
            Double distanceBetweenFixedPoints=newMValueSecondPoint-newMValueFirstPoint;
567
            Double distanceBetweenOldFixedPoints=oldMValueSecondPoint-oldMValueFirstPoint;
568

  
569
            Double calibrationRatio=distanceBetweenOldFixedPoints/distanceBetweenFixedPoints;
570
            for (int i=0; i<minMFixedPointPos;i++){
571
                Double valueToRecalculate=vertices.get(i).getCoordinateAt(MDIMENSION);
572
                Double distanceBetween=oldMValueFirstPoint-valueToRecalculate;
573
                Double mValue=newMValueFirstPoint-(distanceBetween/calibrationRatio);
574
                refreshMValueGeometryVertex(i,mValue);
575
            }
576
        }
577

  
578
        public void extrapolateAfterCalibrationPoints(){
579
            int prevFixedPointPos=getPreviousPosFixedPoint(maxMFixedPointPos);
580
            Double newMValueLastPoint=vertices.get(maxMFixedPointPos).getCoordinateAt(MDIMENSION);
581
            Double newMValuePrevPoint=vertices.get(prevFixedPointPos).getCoordinateAt(MDIMENSION);
582

  
583
            Double oldMValueLastPoint=oldMValues.get(maxMFixedPointPos);
584
            Double oldMValuePrevPoint=oldMValues.get(prevFixedPointPos);
585

  
586
            Double distanceBetweenFixedPoints=newMValueLastPoint-newMValuePrevPoint;
587
            Double distanceBetweenOldFixedPoints=oldMValueLastPoint-oldMValuePrevPoint;
588

  
589
            Double calibrationRatio=distanceBetweenOldFixedPoints/distanceBetweenFixedPoints;
590
            for (int i=maxMFixedPointPos+1; i<vertices.size();i++){
591
                Double valueToRecalculate=vertices.get(i).getCoordinateAt(MDIMENSION);
592
                Double distanceBetween=valueToRecalculate-oldMValueLastPoint;
593
                Double mValue=newMValueLastPoint+(distanceBetween/calibrationRatio);
594
                refreshMValueGeometryVertex(i,mValue);
595
            }
596
        }
597

  
598
        private Integer getNextPosFixedPoint(int pos){
599
            for (int i=pos+1;i<isFixedPoint.size();i++){
600
                if (isFixedPoint.get(i)){
601
                    if( !distances.get(pos).equals(distances.get(i)) ){
602
                        return i;
603
                    }
604
                }
605
            }
606
            return null;
607
        }
608

  
609
        private Integer getPreviousPosFixedPoint(int pos){
610
            for (int i=pos-1;i>=0;i--){
611
                if (isFixedPoint.get(i)){
612
                    if( !distances.get(pos).equals(distances.get(i)) ){
613
                        return i;
614
                    }
615
                }
616
            }
617
            return null;
618
        }
619

  
620
        private void refreshMValueGeometryVertex(int i, Double mValue){
621
            List<Point> points=LrsAlgorithmUtils.extractPoints(route);
622
            vertices.get(i).setCoordinateAt(MDIMENSION, mValue);
623
            points.get(i).setCoordinateAt(MDIMENSION,mValue);
624
        }
625

  
213 626
    }
214 627

  
628

  
215 629
}
216 630

  
217 631

  
org.gvsig.lrs/trunk/org.gvsig.lrs/org.gvsig.lrs.swing/org.gvsig.lrs.swing.impl/src/main/java/org/gvsig/lrs/swing/impl/JLrsCalibrateRouteParamsView.xml
24 24
    </at>
25 25
    <at name="componentclass">com.jeta.forms.gui.form.FormComponent</at>
26 26
   </super>
27
   <at name="id">/home/daniel/tmp/abeille/JLrsCalibrateRouteParamsView.xml</at>
27
   <at name="id">/home/daniel/projects/externals/gvSIG/org.gvsig.lrs/org.gvsig.lrs.swing/org.gvsig.lrs.swing.impl/src/main/java/org/gvsig/lrs/swing/impl/JLrsCalibrateRouteParamsView.xml</at>
28 28
   <at name="rowspecs">CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:GROW(1.0),BOTTOM:DEFAULT:NONE,CENTER:DEFAULT:NONE</at>
29 29
   <at name="colspecs">FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:4DLU:NONE,FILL:DEFAULT:GROW(1.0),FILL:DEFAULT:NONE,FILL:DEFAULT:NONE</at>
30 30
   <at name="components">
......
1160 1160
         </at>
1161 1161
         <at name="componentclass">com.jeta.forms.gui.form.FormComponent</at>
1162 1162
        </super>
1163
        <at name="id">embedded.218310665</at>
1163
        <at name="id">embedded.249315505</at>
1164 1164
        <at name="rowspecs">CENTER:DEFAULT:NONE</at>
1165 1165
        <at name="colspecs">FILL:DEFAULT:GROW(1.0),FILL:36PX:NONE</at>
1166 1166
        <at name="components">
......
1377 1377
         </at>
1378 1378
         <at name="componentclass">com.jeta.forms.gui.form.FormComponent</at>
1379 1379
        </super>
1380
        <at name="id">embedded.1415843874</at>
1380
        <at name="id">embedded.616715470</at>
1381 1381
        <at name="rowspecs">CENTER:DEFAULT:NONE</at>
1382
        <at name="colspecs">FILL:DEFAULT:GROW(1.0),FILL:DEFAULT:NONE,FILL:4DLU:NONE,FILL:DEFAULT:NONE</at>
1382
        <at name="colspecs">FILL:50DLU:GROW(1.0),FILL:DEFAULT:NONE,FILL:4DLU:NONE,FILL:DEFAULT:NONE</at>
1383 1383
        <at name="components">
1384 1384
         <object classname="java.util.LinkedList">
1385 1385
          <item >
......
1406 1406
               <at name="classname">com.jeta.forms.components.label.JETALabel</at>
1407 1407
               <at name="properties">
1408 1408
                <object classname="com.jeta.forms.store.support.PropertyMap">
1409
                 <at name="text">measure_units</at>
1409
                 <at name="text">units</at>
1410 1410
                 <at name="height">14</at>
1411
                 <at name="width">85</at>
1411
                 <at name="width">28</at>
1412 1412
                 <at name="name">lblMeasureUnits</at>
1413 1413
                 <at name="fill">
1414 1414
                  <object classname="com.jeta.forms.store.properties.effects.PaintProperty">
......
1526 1526
               <at name="properties">
1527 1527
                <object classname="com.jeta.forms.store.support.PropertyMap">
1528 1528
                 <at name="height">20</at>
1529
                 <at name="width">597</at>
1529
                 <at name="width">654</at>
1530 1530
                 <at name="name">txtSearchRadius</at>
1531 1531
                 <at name="border">
1532 1532
                  <object classname="com.jeta.forms.store.properties.CompoundBorderProperty">
......
1562 1562
          <at name="classname">com.jeta.forms.gui.form.GridView</at>
1563 1563
          <at name="properties">
1564 1564
           <object classname="com.jeta.forms.store.support.PropertyMap">
1565
            <at name="name"/>
1565
            <at name="name"></at>
1566 1566
            <at name="fill">
1567 1567
             <object classname="com.jeta.forms.store.properties.effects.PaintProperty">
1568 1568
              <at name="name">fill</at>
......
1654 1654
         </at>
1655 1655
         <at name="componentclass">com.jeta.forms.gui.form.FormComponent</at>
1656 1656
        </super>
1657
        <at name="id">embedded.768634657</at>
1657
        <at name="id">embedded.1448976003</at>
1658 1658
        <at name="rowspecs">CENTER:DEFAULT:NONE</at>
1659 1659
        <at name="colspecs">FILL:DEFAULT:NONE,FILL:4DLU:NONE,FILL:DEFAULT:NONE</at>
1660 1660
        <at name="components">
org.gvsig.lrs/trunk/org.gvsig.lrs/org.gvsig.lrs.swing/org.gvsig.lrs.swing.impl/src/main/java/org/gvsig/lrs/swing/impl/JLrsCalibrateRouteParamsView.java
248 248
   public JPanel createPanel2()
249 249
   {
250 250
      JPanel jpanel1 = new JPanel();
251
      FormLayout formlayout1 = new FormLayout("FILL:DEFAULT:GROW(1.0),FILL:DEFAULT:NONE,FILL:4DLU:NONE,FILL:DEFAULT:NONE","CENTER:DEFAULT:NONE");
251
      FormLayout formlayout1 = new FormLayout("FILL:20DLU:GROW(1.0),FILL:DEFAULT:NONE,FILL:4DLU:NONE,FILL:DEFAULT:NONE","CENTER:DEFAULT:NONE");
252 252
      CellConstraints cc = new CellConstraints();
253 253
      jpanel1.setLayout(formlayout1);
254 254

  
255 255
      lblMeasureUnits.setName("lblMeasureUnits");
256
      lblMeasureUnits.setText("measure_units");
256
      lblMeasureUnits.setText("units");
257 257
      jpanel1.add(lblMeasureUnits,cc.xy(2,1));
258 258

  
259 259
      cmbMeasureUnits.setName("cmbMeasureUnits");

Also available in: Unified diff