Revision 985

View differences:

org.gvsig.lrs/tags/org.gvsig.lrs-1.0.180/org.gvsig.lrs.lib/org.gvsig.lrs.lib.impl/src/test/java/org/gvsig/lrs/lib/impl/TestLineInterpolatePoint.java
1
package org.gvsig.lrs.lib.impl;
2

  
3
import junit.framework.TestCase;
4
import org.gvsig.fmap.geom.Geometry;
5
import org.gvsig.fmap.geom.GeometryLocator;
6
import org.gvsig.fmap.geom.GeometryManager;
7
import org.gvsig.fmap.geom.primitive.Point;
8
import org.gvsig.lrs.lib.impl.expressionevaluator.function.lrs.STLineInterpolatePointFunction;
9
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
10

  
11
public class TestLineInterpolatePoint extends TestCase {
12
    
13
    public TestLineInterpolatePoint(String testName) {
14
        super(testName);
15
    }
16
    
17
    @Override
18
    protected void setUp() throws Exception {
19
        super.setUp();
20
        new DefaultLibrariesInitializer().fullInitialize();
21
    }
22
    
23
    @Override
24
    protected void tearDown() throws Exception {
25
        super.tearDown();
26
    }
27

  
28
    // TODO add test methods here. The name must begin with 'test'. For example:
29
    // public void testHello() {}
30
    
31
    public void testLine1() throws Exception{
32
        
33
        GeometryManager manager = GeometryLocator.getGeometryManager();
34
        Geometry line = manager.createFrom("LINESTRING (100 400, 400 400, 605 464, 800 460)");
35
        
36
        double fraction = 150/line.perimeter();
37
        
38
        Point point = STLineInterpolatePointFunction.lineInterpolatePoint(line, fraction);
39
        
40
        assertEquals("POINT (250 400)", point.convertToWKT());
41
    }
42

  
43
    public void testMultiLine1() throws Exception{
44
        
45
        GeometryManager manager = GeometryLocator.getGeometryManager();
46
        Geometry line = manager.createFrom("MULTILINESTRING ((100 400, 400 400),  (400 500, 600 500),  (600 500, 800 460))");
47

  
48
        double fraction = 400/line.perimeter();
49
        
50
        Point point = STLineInterpolatePointFunction.lineInterpolatePoint(line, fraction);
51
        
52
        assertEquals("POINT (500 500)", point.convertToWKT());
53
    }
54
    
55
    public void testLineM1() throws Exception{
56
        
57
        GeometryManager manager = GeometryLocator.getGeometryManager();
58
        Geometry line = manager.createFrom("LINESTRING M(100 400 0, 400 400 300, 605 464 505, 800 460 700)");
59
        
60
        double fraction = 150/line.perimeter();
61
        
62
        Point point = STLineInterpolatePointFunction.lineInterpolatePoint(line, fraction);
63
        
64
        assertEquals("POINT M (250 400 150)", point.convertToWKT());
65
    }
66

  
67
    public void testMultiLineM1() throws Exception{
68
        
69
        GeometryManager manager = GeometryLocator.getGeometryManager();
70
        Geometry line = manager.createFrom("MULTILINESTRING M((100 400 0, 400 400 300),  (400 500 300, 600 500 500),  (600 500 500, 800 460 700))");
71

  
72
        double fraction = 400/line.perimeter();
73
        
74
        Point point = STLineInterpolatePointFunction.lineInterpolatePoint(line, fraction);
75
        
76
        assertEquals("POINT M (500 500 400)", point.convertToWKT());
77
    }
78
    
79
}
org.gvsig.lrs/tags/org.gvsig.lrs-1.0.180/org.gvsig.lrs.lib/org.gvsig.lrs.lib.impl/src/test/java/org/gvsig/lrs/lib/impl/TestLineLocatePoint.java
1
package org.gvsig.lrs.lib.impl;
2

  
3
import junit.framework.TestCase;
4
import org.gvsig.fmap.geom.Geometry;
5
import org.gvsig.fmap.geom.GeometryLocator;
6
import org.gvsig.fmap.geom.GeometryManager;
7
import org.gvsig.fmap.geom.aggregate.MultiLine;
8
import org.gvsig.fmap.geom.primitive.Line;
9
import org.gvsig.fmap.geom.primitive.Point;
10
import org.gvsig.lrs.lib.impl.expressionevaluator.function.lrs.STLineLocatePointFunction;
11
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
12

  
13
public class TestLineLocatePoint extends TestCase {
14
    
15
    public TestLineLocatePoint(String testName) {
16
        super(testName);
17
    }
18
    
19
    @Override
20
    protected void setUp() throws Exception {
21
        super.setUp();
22
        new DefaultLibrariesInitializer().fullInitialize();
23
    }
24
    
25
    @Override
26
    protected void tearDown() throws Exception {
27
        super.tearDown();
28
    }
29

  
30
    // TODO add test methods here. The name must begin with 'test'. For example:
31
    // public void testHello() {}
32
    
33
    public void testLine1() throws Exception{
34
        
35
        GeometryManager manager = GeometryLocator.getGeometryManager();
36
        Geometry line = manager.createFrom("LINESTRING (100 400, 400 400, 605 464, 800 460)");
37
        
38
        Geometry point = manager.createFrom("POINT (250 350)");
39
        
40
        double location = STLineLocatePointFunction.lineLocatePoint((Line)line, (Point)point);
41
        assertEquals(150/line.perimeter(), location);
42
    }
43

  
44
    public void testMultiLine1() throws Exception{
45
        
46
        GeometryManager manager = GeometryLocator.getGeometryManager();
47
        Geometry line = manager.createFrom("MULTILINESTRING ((100 400, 400 400),  (400 500, 600 500),  (600 500, 800 460))");
48
        
49
        Geometry point = manager.createFrom("POINT (500 450)");
50
        
51
        double location = STLineLocatePointFunction.lineLocatePoint((MultiLine)line, (Point)point);
52
        assertEquals(400/line.perimeter(), location);
53
    }
54
    
55
}
0 56

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

  
org.gvsig.lrs/tags/org.gvsig.lrs-1.0.180/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 static org.gvsig.fmap.dal.DataStore.SHAPE_PROVIDER_NAME;
39
import org.gvsig.fmap.dal.DataTypes;
40
import org.gvsig.fmap.dal.feature.EditableFeature;
41
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
42
import org.gvsig.fmap.dal.feature.EditableFeatureType;
43
import org.gvsig.fmap.dal.feature.Feature;
44
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
45
import org.gvsig.fmap.dal.feature.FeatureQuery;
46
import org.gvsig.fmap.dal.feature.FeatureSet;
47
import org.gvsig.fmap.dal.feature.FeatureStore;
48
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
49
//import org.gvsig.fmap.dal.store.shp.SHPNewStoreParameters;
50
//import org.gvsig.fmap.dal.store.shp.SHPStoreProvider;
51
import org.gvsig.fmap.geom.Geometry;
52
import org.gvsig.fmap.geom.GeometryLocator;
53
import org.gvsig.fmap.geom.aggregate.MultiLine;
54
import org.gvsig.fmap.geom.primitive.Point;
55
import org.gvsig.fmap.geom.type.GeometryType;
56
import org.gvsig.lrs.lib.api.LrsAlgorithm;
57
import org.gvsig.lrs.lib.api.LrsAlgorithmParams;
58
import org.gvsig.lrs.lib.api.LrsCreateRouteAlgorithmParams;
59
import org.gvsig.lrs.lib.api.LrsGenerateDynamicSegmentationAlgorithmParams;
60
import org.gvsig.lrs.lib.api.exceptions.LrsCreateRouteException;
61
import org.gvsig.lrs.lib.api.exceptions.LrsException;
62
import org.gvsig.tools.ToolsLocator;
63
import org.gvsig.tools.dataTypes.DataType;
64
import org.gvsig.tools.exception.BaseException;
65
import org.gvsig.tools.i18n.I18nManager;
66
import org.gvsig.tools.locator.LocatorException;
67
import org.gvsig.tools.service.Manager;
68
import org.gvsig.tools.task.SimpleTaskStatus;
69
import org.gvsig.tools.util.HasAFile;
70
import org.gvsig.tools.visitor.VisitCanceledException;
71
import org.gvsig.tools.visitor.Visitor;
72

  
73
/**
74
 * @author fdiaz
75
 *
76
 */
77
public class LrsGenerateDynamicSegmentationAlgorithm implements LrsAlgorithm {
78

  
79
    private static final Logger logger = LoggerFactory.getLogger(LrsGenerateDynamicSegmentationAlgorithm.class);
80

  
81
    private LrsGenerateDynamicSegmentationAlgorithmParams parameters;
82

  
83

  
84
    /**
85
     *
86
     */
87
    public LrsGenerateDynamicSegmentationAlgorithm(LrsGenerateDynamicSegmentationAlgorithmParams parameters) {
88
        this.parameters = parameters;
89

  
90
    }
91

  
92
    /*
93
     * (non-Javadoc)
94
     *
95
     * @see org.gvsig.tools.service.Service#getManager()
96
     */
97
    public Manager getManager() {
98
        return null;
99
    }
100

  
101
    /*
102
     * (non-Javadoc)
103
     *
104
     * @see org.gvsig.lrs.lib.api.LrsAlgorithm#getParams()
105
     */
106
    public  LrsAlgorithmParams getParams() {
107
        return this.parameters;
108
    }
109

  
110
    /*
111
     * (non-Javadoc)
112
     * @see org.gvsig.lrs.lib.api.LrsAlgorithm#execute(org.gvsig.tools.task.SimpleTaskStatus)
113
     */
114
    public void execute(final SimpleTaskStatus taskStatus) throws LrsException {
115
        NewFeatureStoreParameters newFeatureStoreParameters = parameters.getNewFeatureStoreParameters();
116
        final FeatureStore sourceFeatureStore = parameters.getSourceFeatureStore();
117
        final FeatureAttributeDescriptor idRouteField = parameters.getIdRouteField();
118
        final FeatureAttributeDescriptor idTableRouteField = parameters.getValueTableIdRouteField();
119
        FeatureStore tableFeatureStore = parameters.getValueTableFeatureStore();
120
        final FeatureAttributeDescriptor landmarkField = parameters.getLandmarkField();
121
        final FeatureAttributeDescriptor finalLandmarkField = parameters.getFinalLandmarkField();
122
        final FeatureAttributeDescriptor valueField = parameters.getValueField();
123

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

  
126
        taskStatus.setTitle(parameters.getName());
127
        I18nManager i18nManager = ToolsLocator.getI18nManager();
128
        taskStatus.message(i18nManager.getTranslation("processing"));
129

  
130
        try {
131
            final String tableRouteFieldName=idTableRouteField.getName();
132
            final String fromFieldName;
133
            final DataType fromDataType;
134
            final String toFieldName;
135
            final DataType toDataType;
136
            final int geomType;
137
            if (landmarkField!=null){
138
                fromDataType=landmarkField.getDataType();
139
                if (finalLandmarkField!=null){
140
                    fromFieldName="PK INICIAL";
141
                    toFieldName="PK FINAL";
142
                    toDataType=finalLandmarkField.getDataType();
143
                    geomType = Geometry.TYPES.MULTILINE;
144
                }else{
145
                    fromFieldName="PK";
146
                    toFieldName=null;
147
                    toDataType=null;
148
                    geomType = Geometry.TYPES.POINT;
149
                }
150
            }else{
151
                //TODO: Si no hay fromFieldName ?salir por patas?
152
                fromFieldName=null;
153
                fromDataType=null;
154
                toFieldName=null;
155
                toDataType=null;
156
                geomType = Geometry.TYPES.POINT;
157
            }
158

  
159
            final FeatureStore newFeatureStore = createNewDataStore(
160
                newFeatureStoreParameters,
161
                idRouteField,
162
                fromFieldName,
163
                toFieldName,
164
                valueField,
165
                geomType
166
             );
167

  
168
            FeatureSet tableFeatures;
169
            tableFeatures=tableFeatureStore.getFeatureSet();
170
            taskStatus.setRangeOfValues(0, tableFeatures.getSize()-1);
171
            newFeatureStore.edit(FeatureStore.MODE_FULLEDIT);
172

  
173
            tableFeatures.accept(new Visitor() {
174

  
175
                int taskCount=0;
176
                public void visit(Object obj) throws VisitCanceledException, BaseException {
177
                    Feature feature = (Feature) obj;
178
                    String routeName = (String) feature.get(tableRouteFieldName);
179
                    Object objFrom = null;
180
                    Object objTo = null;
181
                    if (fromFieldName != null) {
182
                        objFrom = feature.get(landmarkField.getName());
183
                        double fromValue = LrsAlgorithmUtils.getAsDouble(objFrom, fromDataType);
184
                        Object value = feature.get(valueField.getName());
185
                        if (toFieldName == null || toFieldName.isEmpty()) {
186
                            addSegmentatePointsFeatures(newFeatureStore, sourceFeatureStore, idRouteField, routeName, fromFieldName, fromValue, valueField, value);
187
                        } else {
188
                            objTo = feature.get(finalLandmarkField.getName());
189
                            double toValue = LrsAlgorithmUtils.getAsDouble(objTo, toDataType);
190
                            addSegmentateLinesFeatures(newFeatureStore, sourceFeatureStore, idRouteField, routeName,
191
                                fromFieldName, fromValue, toFieldName, toValue, valueField, value);
192
                        }
193

  
194
                    } else {
195
                        //TODO: ?Exception?
196
                    }
197
                    taskStatus.setCurValue(taskCount++);
198
                }
199

  
200
                private void addSegmentatePointsFeatures(final FeatureStore featureStore,
201
                    FeatureStore sourceFeatureStore, final FeatureAttributeDescriptor idRouteField,
202
                    final String routeName, final String fromFieldName, final double fromValue, final FeatureAttributeDescriptor valueField, final Object value)
203
                    throws LocatorException, BaseException {
204

  
205
                    DataManager dataManager = DALLocator.getDataManager();
206
                    FeatureQuery query = sourceFeatureStore.createFeatureQuery();
207
                    String expression = StringUtils.replace(StringUtils.replace("NOMBRE = 'a%'", "NOMBRE", idRouteField.getName()), "a%", routeName);
208
                    query.setFilter( dataManager.createExpresion(expression) );
209

  
210
                    FeatureSet features = sourceFeatureStore.getFeatureSet(query);
211
                    features.accept(new Visitor() {
212

  
213
                        public void visit(Object obj) throws VisitCanceledException, BaseException {
214
                            Feature feature = (Feature)obj;
215
                            Geometry geom = feature.getDefaultGeometry();
216
                            List<Point>points = LrsAlgorithmUtils.getPointsWithM(geom, fromValue);
217
                            for (Iterator<Point> iterator = points.iterator(); iterator.hasNext();) {
218
                                Point point = (Point) iterator.next();
219
                                EditableFeature newFeature = featureStore.createNewFeature(true);
220
                                newFeature.set(idRouteField.getName(), routeName);
221
                                newFeature.set(fromFieldName, fromValue);
222
                                newFeature.set(valueField.getName(), value);
223
                                newFeature.setDefaultGeometry(point);
224
                                featureStore.insert(newFeature);
225
                            }
226
                        }
227
                    });
228
                }
229

  
230
                private void addSegmentateLinesFeatures(final FeatureStore featureStore,
231
                    FeatureStore sourceFeatureStore, final FeatureAttributeDescriptor idRouteField,
232
                    final String routeName, final String fromFieldName, final double fromValue,
233
                    final String toFieldName, final double toValue, final FeatureAttributeDescriptor valueField, final Object value) throws LocatorException, BaseException {
234

  
235
                    DataManager dataManager = DALLocator.getDataManager();
236
                    FeatureQuery query = sourceFeatureStore.createFeatureQuery();
237
                    String expression = StringUtils.replace(StringUtils.replace("NOMBRE = 'a%'", "NOMBRE", idRouteField.getName()), "a%", routeName);
238
                    query.setFilter( dataManager.createExpresion(expression) );
239

  
240
                    FeatureSet features = sourceFeatureStore.getFeatureSet(query);
241
                    features.accept(new Visitor() {
242

  
243
                        public void visit(Object obj) throws VisitCanceledException, BaseException {
244
                            Feature feature = (Feature) obj;
245
                            Geometry geom = feature.getDefaultGeometry();
246
                            MultiLine lines = LrsAlgorithmUtils.getLinesBetweenTwoM(geom, fromValue, toValue);
247
                            if (lines != null && lines.getPrimitivesNumber()>0) {
248
                                EditableFeature newFeature = featureStore.createNewFeature(true);
249
                                newFeature.set(idRouteField.getName(), routeName);
250
                                newFeature.set(fromFieldName, fromValue);
251
                                newFeature.set(toFieldName, toValue);
252
                                newFeature.set(valueField.getName(), value);
253
                                newFeature.setDefaultGeometry(lines);
254
                                featureStore.insert(newFeature);
255
                            }
256
                        }
257
                    });
258
                }
259
            });
260

  
261
            newFeatureStore.finishEditing();
262

  
263
        } catch (Exception e1) {
264
            taskStatus.abort();
265
            throw new LrsCreateRouteException("Error creating routes", e1);
266
        }
267

  
268

  
269
        taskStatus.terminate();
270

  
271
    }
272

  
273

  
274
    /* (non-Javadoc)
275
     * @see org.gvsig.lrs.lib.api.LrsAlgorithm#setParams(org.gvsig.lrs.lib.api.LrsAlgorithmParams)
276
     */
277
    public void setParams(LrsAlgorithmParams params) throws IllegalArgumentException {
278
        if(!(params instanceof LrsCreateRouteAlgorithmParams)){
279
            throw new IllegalArgumentException("params should be LrsCreateRouteAlgorithmParams type.");
280
        }
281
        this.parameters = (LrsGenerateDynamicSegmentationAlgorithmParams) params;
282

  
283
    }
284

  
285
    private FeatureStore createNewDataStore(
286
        NewFeatureStoreParameters newFeatureStoreParameters,
287
        FeatureAttributeDescriptor idRouteField,
288
        String fromFieldName,
289
        String toFieldName,
290
        FeatureAttributeDescriptor valueField,
291
        int type) throws Exception{
292
        try {
293
            File file=((HasAFile)newFeatureStoreParameters).getFile();
294
            String filePath=file.getPath().substring(0, file.getPath().lastIndexOf(File.separator));
295

  
296
            DataManager dataManager = DALLocator.getDataManager();
297
            DataServerExplorerParameters serverParams =
298
                dataManager.createServerExplorerParameters("FilesystemExplorer");
299
            serverParams.setDynValue("initialpath", filePath);
300
            DataServerExplorer serverExplorer =
301
                dataManager.openServerExplorer(serverParams.getExplorerName(), serverParams);
302

  
303
            EditableFeatureType featureType = (EditableFeatureType)newFeatureStoreParameters.getDefaultFeatureType();
304
            featureType.add(idRouteField.getName(), idRouteField.getType(), idRouteField.getSize());
305
            featureType.add(fromFieldName, DataTypes.DOUBLE);
306
            if(toFieldName!=null){
307
                featureType.add(toFieldName, DataTypes.DOUBLE);
308
            }
309
            featureType.add(valueField.getName(), valueField.getType(), valueField.getSize());
310
            EditableFeatureAttributeDescriptor geometryField = featureType.add("Geometry", DataTypes.GEOMETRY);
311
            GeometryType geometryType = GeometryLocator.getGeometryManager().getGeometryType(type, Geometry.SUBTYPES.GEOM2DM);
312
            geometryField.setGeometryType(geometryType);
313

  
314
            featureType.setDefaultGeometryAttributeName("Geometry");
315

  
316
            newFeatureStoreParameters.setDefaultFeatureType(featureType);
317
            serverExplorer.add("Shape", newFeatureStoreParameters, true);
318

  
319
            DataStore store = dataManager.openStore(SHAPE_PROVIDER_NAME, newFeatureStoreParameters);
320

  
321
            return (FeatureStore)store;
322

  
323
        } catch (Exception e) {
324
            throw new LrsCreateRouteException("Error creating new dataStore", e);
325
        }
326
    }
327

  
328

  
329
}
330

  
331

  
332

  
333

  
org.gvsig.lrs/tags/org.gvsig.lrs-1.0.180/org.gvsig.lrs.lib/org.gvsig.lrs.lib.impl/src/main/java/org/gvsig/lrs/lib/impl/DefaultLrsCalibrateRouteAlgorithmParams.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.LrsMeasureCalculationMethods;
31

  
32

  
33
/**
34
 * @author dmartinez
35
 *
36
 */
37
public class DefaultLrsCalibrateRouteAlgorithmParams implements LrsCalibrateRouteAlgorithmParams {
38

  
39
    private FeatureStore sourceFeatureStore;
40
    private FeatureAttributeDescriptor idRouteField;
41
    private NewFeatureStoreParameters newFeatureStoreParameters;
42
    private FeatureStore calibratePointFeatureStore;
43
    private FeatureAttributeDescriptor calibratePointIdRouteField;
44
    private FeatureAttributeDescriptor fromMeasureField;
45
    private LrsMeasureCalculationMethods measureCalculationMethods;
46
    private DistanceUnits measureUnits;
47
    private double searchRadius = 0.0;
48
    private boolean interpolateBetweenCalibrationPoints=false;
49
    private boolean extrapolateBeforeCalibrationPoints=false;
50
    private boolean extrapolateAfterCalibrationPoints=false;
51
    private boolean ignoreSpatialGaps=false;
52
    private boolean includeAll=false;
53
    private final String NAME = "LrsCalibrateRouteAlgorithm";
54
    private final String DESCRIPTION = "Algorithm to calibrate routes with linear reference system.";
55

  
56

  
57

  
58
    /**
59
     *
60
     */
61
    public DefaultLrsCalibrateRouteAlgorithmParams() {
62
        searchRadius = 0.0;
63
    }
64

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

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

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

  
87

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

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

  
104

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

  
113
    /*
114
     * (non-Javadoc)
115
     * @see org.gvsig.lrs.lib.api.LrsCreateRouteAlgorithmParams#getFromMeasureField()
116
     */
117
    public FeatureAttributeDescriptor getFromMeasureField() {
118
        return fromMeasureField;
119
    }
120

  
121
    /*
122
     * (non-Javadoc)
123
     * @see org.gvsig.lrs.lib.api.LrsCreateRouteAlgorithmParams#setFromMeasureField(org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor)
124
     */
125
    public void setFromMeasureField(FeatureAttributeDescriptor fromMeasureField) {
126
        this.fromMeasureField = fromMeasureField;
127
    }
128

  
129
    /*
130
     * (non-Javadoc)
131
     * @see org.gvsig.lrs.lib.api.LrsCreateRouteAlgorithmParams#isIgnoreSpatialGaps()
132
     */
133
    public boolean ignoreSpatialGaps() {
134
        return ignoreSpatialGaps;
135
    }
136

  
137
    /*
138
     * (non-Javadoc)
139
     * @see org.gvsig.lrs.lib.api.LrsCreateRouteAlgorithmParams#setIgnoreSpatialGaps(boolean)
140
     */
141
    public void setIgnoreSpatialGaps(boolean ignoreSpatialGaps) {
142
        this.ignoreSpatialGaps = ignoreSpatialGaps;
143
    }
144

  
145
    public NewFeatureStoreParameters getNewFeatureStoreParameters() {
146
        return this.newFeatureStoreParameters;
147
    }
148

  
149
    public void setNewFeatureStoreParameters(
150
        NewFeatureStoreParameters newFeatureStoreParameters) {
151
        this.newFeatureStoreParameters = newFeatureStoreParameters;
152
    }
153

  
154

  
155
    public FeatureStore getCalibratePointFeatureStore() {
156
        return calibratePointFeatureStore;
157
    }
158

  
159

  
160
    public void setCalibratePointFeatureStore(
161
        FeatureStore calibratePointFeatureStore) {
162
        this.calibratePointFeatureStore = calibratePointFeatureStore;
163
    }
164

  
165

  
166
    public FeatureAttributeDescriptor getCalibratePointIdRouteField() {
167
        return calibratePointIdRouteField;
168
    }
169

  
170

  
171
    public void setCalibratePointIdRouteField(
172
        FeatureAttributeDescriptor calibratePointIdRouteField) {
173
        this.calibratePointIdRouteField = calibratePointIdRouteField;
174
    }
175

  
176

  
177
    public LrsMeasureCalculationMethods getMeasureCalculationMethods() {
178
        return measureCalculationMethods;
179
    }
180

  
181

  
182
    public void setMeasureCalculationMethods(
183
        LrsMeasureCalculationMethods measureCalculationMethods) {
184
        this.measureCalculationMethods = measureCalculationMethods;
185
    }
186

  
187

  
188
    public DistanceUnits getMeasureUnits() {
189
        return measureUnits;
190
    }
191

  
192

  
193
    public void setMeasureUnits(DistanceUnits measureUnits) {
194
        this.measureUnits = measureUnits;
195
    }
196

  
197

  
198
    public double getSearchRadius() {
199
        return searchRadius;
200
    }
201

  
202

  
203
    public void setSearchRadius(double searchRadius) {
204
        this.searchRadius = searchRadius;
205
    }
206

  
207

  
208
    public boolean interpolateBetweenCalibrationPoints() {
209
        return interpolateBetweenCalibrationPoints;
210
    }
211

  
212

  
213
    public void setInterpolateBetweenCalibrationPoints(
214
        boolean interpolateBetweenCalibrationPoints) {
215
        this.interpolateBetweenCalibrationPoints =
216
            interpolateBetweenCalibrationPoints;
217
    }
218

  
219

  
220
    public boolean extrapolateBeforeCalibrationPoints() {
221
        return extrapolateBeforeCalibrationPoints;
222
    }
223

  
224

  
225
    public void setExtrapolateBeforeCalibrationPoints(
226
        boolean extrapolateBeforeCalibrationPoints) {
227
        this.extrapolateBeforeCalibrationPoints =
228
            extrapolateBeforeCalibrationPoints;
229
    }
230

  
231

  
232
    public boolean extrapolateAfterCalibrationPoints() {
233
        return extrapolateAfterCalibrationPoints;
234
    }
235

  
236

  
237
    public void setExtrapolateAfterCalibrationPoints(
238
        boolean extrapolateAfterCalibrationPoints) {
239
        this.extrapolateAfterCalibrationPoints = extrapolateAfterCalibrationPoints;
240
    }
241

  
242

  
243
    public boolean includeAll() {
244
        return includeAll;
245
    }
246

  
247

  
248
    public void setIncludeAll(boolean includeAll) {
249
        this.includeAll = includeAll;
250
    }
251

  
252

  
253
}
org.gvsig.lrs/tags/org.gvsig.lrs-1.0.180/org.gvsig.lrs.lib/org.gvsig.lrs.lib.impl/src/main/java/org/gvsig/lrs/lib/impl/DefaultLrsShowMeasuresAlgorithmParams.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.lrs.lib.api.LrsShowMeasuresAlgorithmParams;
26

  
27

  
28

  
29
/**
30
 * @author dmartinez
31
 *
32
 */
33
public class DefaultLrsShowMeasuresAlgorithmParams implements LrsShowMeasuresAlgorithmParams {
34

  
35

  
36
    private double distance = 0.0;
37
    private final String NAME = "LrsShowMeasuresAlgorithm";
38
    private final String DESCRIPTION = "Algorithm to show measures with linear reference system.";
39
    private int units = LRS_SHOWMEASURES_UNKNOWN_OTHER;
40

  
41

  
42
    /**
43
     *
44
     */
45
    public DefaultLrsShowMeasuresAlgorithmParams() {
46
        distance = 0.0;
47
    }
48

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

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

  
63
    public double getDistance() {
64
        return distance;
65
    }
66

  
67
    public void setDistance(double distance) {
68
        this.distance = distance;
69
    }
70

  
71
    public int getUnits() {
72
        return units;
73
    }
74

  
75
    public void setUnits(int units) {
76
        this.units = units;
77
    }
78

  
79
}
org.gvsig.lrs/tags/org.gvsig.lrs-1.0.180/org.gvsig.lrs.lib/org.gvsig.lrs.lib.impl/src/main/java/org/gvsig/lrs/lib/impl/LrsAlgorithmUtils.java
1
/*
2
 * Copyright 2015 DiSiD Technologies S.L.L. All rights reserved.
3
 *
4
 * Project  : DiSiD org.gvsig.lrs.lib.impl
5
 * SVN Id   : $Id$
6
 */
7
package org.gvsig.lrs.lib.impl;
8

  
9
import java.io.File;
10
import java.util.ArrayList;
11
import java.util.List;
12
import org.gvsig.fmap.dal.DALLocator;
13
import org.gvsig.fmap.dal.DataManager;
14
import org.gvsig.fmap.dal.DataServerExplorer;
15
import org.gvsig.fmap.dal.DataServerExplorerParameters;
16
import org.gvsig.fmap.dal.DataStore;
17
import org.gvsig.fmap.dal.DataTypes;
18
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
19
import org.gvsig.fmap.dal.feature.EditableFeatureType;
20
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
21
import org.gvsig.fmap.dal.feature.FeatureStore;
22
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
23
import org.gvsig.fmap.geom.Geometry;
24
import org.gvsig.fmap.geom.GeometryLocator;
25
import org.gvsig.fmap.geom.GeometryManager;
26
import org.gvsig.fmap.geom.aggregate.MultiLine;
27
import org.gvsig.fmap.geom.exception.CreateGeometryException;
28
import org.gvsig.fmap.geom.operation.GeometryOperationException;
29
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
30
import org.gvsig.fmap.geom.primitive.Line;
31
import org.gvsig.fmap.geom.primitive.Point;
32
import org.gvsig.fmap.geom.type.GeometryType;
33
import org.gvsig.lrs.lib.api.exceptions.LrsCreateRouteException;
34
import org.gvsig.tools.dataTypes.DataType;
35
import org.gvsig.tools.locator.LocatorException;
36
import org.gvsig.tools.util.HasAFile;
37

  
38
/**
39
 * @author fdiaz
40
 *
41
 */
42
public class LrsAlgorithmUtils {
43

  
44
    static protected FeatureStore createNewDataStore(NewFeatureStoreParameters newFeatureStoreParameters,
45
        FeatureAttributeDescriptor idRouteField) throws Exception {
46
        return createNewDataStore(newFeatureStoreParameters, idRouteField, Geometry.TYPES.MULTILINE);
47
    }
48

  
49
    static protected FeatureStore createNewDataStore(NewFeatureStoreParameters newFeatureStoreParameters,
50
        FeatureAttributeDescriptor idRouteField, int type) throws Exception {
51
        try {
52
            File file = ((HasAFile)newFeatureStoreParameters).getFile();
53
            String filePath = file.getPath().substring(0, file.getPath().lastIndexOf(File.separator));
54

  
55
            DataManager dataManager = DALLocator.getDataManager();
56
            DataServerExplorerParameters serverParams =
57
                dataManager.createServerExplorerParameters("FilesystemExplorer");
58
            serverParams.setDynValue("initialpath", filePath);
59
            DataServerExplorer serverExplorer =
60
                dataManager.openServerExplorer(serverParams.getExplorerName(), serverParams);
61

  
62
            EditableFeatureType featureType = (EditableFeatureType) newFeatureStoreParameters.getDefaultFeatureType();
63
            featureType.add(idRouteField.getName(), idRouteField.getType(), idRouteField.getSize());
64
            EditableFeatureAttributeDescriptor geometryField = featureType.add("Geometry", DataTypes.GEOMETRY);
65
            GeometryType geometryType =
66
                GeometryLocator.getGeometryManager().getGeometryType(type, Geometry.SUBTYPES.GEOM2DM);
67
            geometryField.setGeometryType(geometryType);
68

  
69
            featureType.setDefaultGeometryAttributeName("Geometry");
70

  
71
            newFeatureStoreParameters.setDefaultFeatureType(featureType);
72
            serverExplorer.add("Shape", newFeatureStoreParameters, true);
73

  
74
            DataStore store = dataManager.createStore(newFeatureStoreParameters);
75

  
76
            return (FeatureStore) store;
77

  
78
        } catch (Exception e) {
79
            throw new LrsCreateRouteException("Error creating new dataStore", e);
80
        }
81
    }
82

  
83
    static protected Double getAsDouble(Object obj, DataType dataType) {
84
        if (obj == null || dataType == null) {
85
            return null;
86
        }
87
        Double result = Double.NaN;
88
        if (dataType.equals(DataTypes.DOUBLE)) {
89
            result = (Double) obj;
90
        } else if (dataType.isNumeric()) {
91
            result = Double.valueOf(String.valueOf(obj));
92
        }
93
        return result;
94
    }
95

  
96
    static List<Point> getPointsWithM(Geometry geom, double m) throws CreateGeometryException, LocatorException {
97

  
98
        List<Point> points = new ArrayList<Point>();
99

  
100
        if (geom instanceof Line) {
101
            Line line = (Line) geom;
102
            for (int i = 0; i < line.getNumVertices() - 1; i++) {
103
                Point currentVertex = line.getVertex(i);
104
                Point nextVertex = line.getVertex(i + 1);
105
                Point point = getPointWithMBetweenTwoMPoints(m, currentVertex, nextVertex);
106
                if (point != null && !points.contains(point)) {
107
                    points.add(point);
108
                }
109
            }
110
        } else if (geom instanceof MultiLine) {
111
            MultiLine multiLine = (MultiLine) geom;
112
            for (int p = 0; p < multiLine.getPrimitivesNumber(); p++) {
113
                Line line = (Line) multiLine.getPrimitiveAt(p);
114
                for (int i = 0; i < line.getNumVertices() - 1; i++) {
115
                    Point point = getPointWithMBetweenTwoMPoints(m, line.getVertex(i), line.getVertex(i + 1));
116
                    if (point != null && !points.contains(point)) {
117
                        points.add(point);
118
                    }
119
                }
120
            }
121
        }
122
        return points;
123
    }
124

  
125
    private static boolean isInRange(double v, double r1, double r2) {
126
        if (r1 < r2) {
127
            return v >= r1 && v <= r2;
128
        } else if (r1 > r2) {
129
            return v >= r2 && v <= r1;
130
        } else {
131
            return v == r1;
132
        }
133
    }
134

  
135
    static MultiLine getLinesBetweenTwoM(Geometry geom, double m1, double m2) throws CreateGeometryException,
136
        LocatorException {
137
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
138

  
139
        MultiLine lines = null;
140
        if (geom instanceof Line) {
141
            lines = getLinesBetweenTwoM((Line) geom, m1, m2);
142
        } else if (geom instanceof MultiLine) {
143
            lines = (MultiLine) geomManager.create(Geometry.TYPES.MULTILINE, geom.getGeometryType().getSubType());
144
            MultiLine multiLine = (MultiLine) geom;
145
            for (int i = 0; i < multiLine.getPrimitivesNumber(); i++) {
146
                MultiLine auxMultiLine = getLinesBetweenTwoM(multiLine.getPrimitiveAt(i), m1, m2);
147
                for (int j = 0; j < auxMultiLine.getPrimitivesNumber(); j++) {
148
                    lines.addPrimitive(auxMultiLine.getPrimitiveAt(j));
149
                }
150
            }
151
        }
152
        return lines;
153
    }
154

  
155
    static private MultiLine getLinesBetweenTwoM(Line line, double m1, double m2) throws CreateGeometryException {
156
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
157
        MultiLine lines = (MultiLine) geomManager.create(Geometry.TYPES.MULTILINE, line.getGeometryType().getSubType());
158
        Line auxLine = (Line) geomManager.create(Geometry.TYPES.LINE, line.getGeometryType().getSubType());
159

  
160
        boolean inSegment = false;
161
        if (line.getNumVertices() > 0) {
162
            int ivertex = 0;
163
            Point previousVertex = null;
164
            while (ivertex < line.getNumVertices()) {
165
                if (!inSegment) { // NO ESTAMOS EN UN SEGMENTO VALIDO
166
                    Point vertex = line.getVertex(ivertex);
167
                    double m = vertex.getCoordinateAt(vertex.getDimension() - 1);
168
                    if (isInRange(m, m1, m2)) {
169
                        // Si la m del vertice actual entra en el rango:
170
                        if (previousVertex == null) {
171
                            // Si no hay previousVertex, este debe ser el primer
172
                            // vertice, entonces
173
                            // - a?adimos el v?rtice a la linea
174
                            // - marcamos que estamos en un segmento valido
175
                            // - nos guardamos el vertice como previousVertex
176
                            // - aumentamos el indice para coger el siguiente
177
                            // v?rtice en la pr?xima iteraci?n
178
                            auxLine.addVertex(vertex);
179
                            previousVertex = vertex;
180
                            inSegment = true;
181
                            ivertex++;
182
                        } else {
183
                            // - Calculamos qu? punto habr?a iniciado el
184
                            // segmento v?lido
185
                            double previousM = previousVertex.getCoordinateAt(previousVertex.getDimension() - 1);
186
                            // FIXME: Esta comprobaci?n se podr?a quitar
187
                            if (isInRange(m1, previousM, m) || isInRange(m2, previousM, m)) {
188
                                Point point = getPointWithMBetweenTwoMPoints(m1, previousVertex, vertex);
189
                                if (point == null) {
190
                                    point = getPointWithMBetweenTwoMPoints(m2, previousVertex, vertex);
191
                                }
192
                                if (point != null) {
193
                                    // - A?adimos el punto a la linea auxiliar
194
                                    // - Marcamos que estamos en un segmento v?lido
195
                                    // - nos guardamos el punto como previousVertex
196
                                    // - y NO aumentamos el indice
197
                                    auxLine.addVertex(point);
198
                                    inSegment = true;
199
                                    previousVertex = point;
200
                                } else {
201
                                    // Nunca deber?a pasar por aqu?
202
                                }
203
                            }
204
                        }
205
                    } else {
206
                        // Cabe la posibilidad de que entre un vertice y otro se
207
                        // entre y se salga del rango
208
                        if (previousVertex != null) {
209
                            double previousM = previousVertex.getCoordinateAt(previousVertex.getDimension() - 1);
210
                            if (isInRange(m1, previousM, m) && isInRange(m2, previousM, m)) {
211
                                Point point1 = getPointWithMBetweenTwoMPoints(m1, previousVertex, vertex);
212
                                Point point2 = getPointWithMBetweenTwoMPoints(m2, previousVertex, vertex);
213
                                auxLine.addVertex(point1);
214
                                auxLine.addVertex(point2);
215
                                lines.addPrimitive(auxLine);
216
                            }
217
                        }
218

  
219
                        // Si la m del v?rtice actual no entra en el rango:
220
                        // - nos guardamos el vertice como previousVertex
221
                        // - aumentamos el indice para coger el siguiente
222
                        // v?rtice en la pr?xima iteraci?n
223
                        previousVertex = vertex;
224
                        ivertex++;
225
                    }
226
                } else { // ESTAMOS EN UN SEGMENTO VALIDO
227
                    Point vertex = line.getVertex(ivertex);
228
                    double m = vertex.getCoordinateAt(previousVertex.getDimension() - 1);
229
                    if (isInRange(m, m1, m2)) {
230
                        // Si la m del vertice actual entra en el rango:
231
                        // - a?adimos el v?rtice a la linea
232
                        // - aumentamos el indice para coger el siguiente
233
                        // v?rtice en la pr?xima iteraci?n
234
                        auxLine.addVertex(vertex);
235
                        previousVertex = vertex;
236
                        ivertex++;
237
                    } else {
238
                        // Si la m del v?rtice actual no entra en el rango:
239
                        // - Calculamos el punto que habr?a finalizado el
240
                        // segmento v?lido entre el vertice anterior y el actual
241
                        Point point = getPointWithMBetweenTwoMPoints(m1, previousVertex, vertex);
242
                        if (point == null) {
243
                            point = getPointWithMBetweenTwoMPoints(m2, previousVertex, vertex);
244
                        }
245
                        if (point != null) {
246
                            // - A?adimos el punto a la linea auxiliar
247
                            // - a?adimos la linea y creamos una nueva
248
                            // - Marcamos que NO estamos en un segmento v?lido
249
                            // - nos guardamos el punto como previousVertex
250
                            // - y NO aumentamos el indice
251
                            auxLine.addVertex(point);
252
                            lines.addPrimitive(auxLine);
253
                            auxLine =
254
                                (Line) geomManager.create(Geometry.TYPES.LINE, line.getGeometryType().getSubType());
255
                            inSegment = false;
256
                            previousVertex = point;
257
                        } else {
258
                            // Nunca deber?a pasar por aqu?
259
                        }
260
                    }
261
                }
262
            }
263
        }
264
        if (inSegment && auxLine.getNumVertices() > 0) {
265
            lines.addPrimitive(auxLine);
266
        }
267
        return lines;
268
    }
269

  
270
    /*
271
     *
272
     */
273
    private static Point getPointWithMBetweenTwoMPoints(double m, Point p1, Point p2) throws CreateGeometryException,
274
        LocatorException {
275
        double x;
276
        double y;
277
        double m1 = p1.getCoordinateAt(p1.getDimension() - 1);
278
        double m2 = p2.getCoordinateAt(p2.getDimension() - 1);
279

  
280
        if (m1 <= m2) {
281
            if (m < m1 || m > m2) {
282
                return null;
283
            }
284
        } else {
285
            if (m < m2 || m > m1) {
286
                return null;
287
            }
288
        }
289

  
290
        double x1 = p1.getX();
291
        double x2 = p2.getX();
292
        double y1 = p1.getY();
293
        double y2 = p2.getY();
294
        if (x1 == x2) {
295
            x = x1;
296
        } else {
297
            x = ((x2 - x1) * (m - m1) / (m2 - m1)) + x1;
298
        }
299
        if (y1 == y2) {
300
            y = y1;
301
        } else {
302
            y = ((y2 - y1) * (m - m1) / (m2 - m1)) + y1;
303
        }
304

  
305
        Point point = (Point) GeometryLocator.getGeometryManager().create(p1.getGeometryType());
306
        point.setX(x);
307
        point.setY(y);
308
        point.setCoordinateAt(point.getDimension() - 1, m);
309
        return point;
310
    }
311

  
312

  
313

  
314
    /**
315
     * Returns geometry length without gaps
316
     * @param geometry
317
     * @return
318
     * @throws GeometryOperationNotSupportedException
319
     * @throws GeometryOperationException
320
     */
321
    static protected Double getGeometryLength(Geometry geometry) throws GeometryOperationNotSupportedException, GeometryOperationException{
322
        return getGeometryLength(geometry, true);
323
    }
324

  
325
    /**
326
     * Returns geometry length without gaps with the option to not ignore spatial gaps.
327
     * @param geometry
328
     * @param ignoreSpatialGaps
329
     * @return
330
     * @throws GeometryOperationNotSupportedException
331
     * @throws GeometryOperationException
332
     */
333
    static protected Double getGeometryLength(Geometry geometry,
334
        boolean ignoreSpatialGaps)
335
            throws GeometryOperationNotSupportedException,
336
            GeometryOperationException {
337
        if (geometry instanceof Line) {
338
            Line line = (Line) geometry;
339
            return getLineLength(line);
340
        }
341
        if (geometry instanceof MultiLine) {
342
            MultiLine multiLine = (MultiLine) geometry;
343
            return getMultiLineLength(multiLine, ignoreSpatialGaps);
344
        }
345
        return Double.NaN;
346
    }
347

  
348
    /**
349
     * @param line
350
     * @return lenght
351
     * @throws GeometryOperationException
352
     * @throws GeometryOperationNotSupportedException
353
     */
354
    static protected double getLineLength(Line line)
355
        throws GeometryOperationNotSupportedException,
356
        GeometryOperationException {
357
        double lenght = 0;
358
        Point previousVertex = null;
359
        for (int i = 0; i < line.getNumVertices(); i++) {
360
            Point vertex = line.getVertex(i);
361
            if (previousVertex != null) {
362
                lenght += previousVertex.distance(vertex);
363
            }
364
            previousVertex = vertex;
365
        }
366
        return lenght;
367
    }
368

  
369
    /**
370
     * @param multiLine
371
     * @return lenght
372
     * @throws GeometryOperationException
373
     * @throws GeometryOperationNotSupportedException
374
     */
375
    static protected double getMultiLineLength(MultiLine multiLine,
376
        boolean ignoreSpatialGaps)
377
            throws GeometryOperationNotSupportedException,
378
            GeometryOperationException {
379
        double length = 0;
380
        Point previousVertex = null;
381
        for (int j = 0; j < multiLine.getPrimitivesNumber(); j++) {
382
            Line line = (Line) multiLine.getPrimitiveAt(j);
383
            if (ignoreSpatialGaps) {
384
                previousVertex = null;
385
            }
386
            for (int i = 0; i < line.getNumVertices(); i++) {
387
                Point vertex = line.getVertex(i);
388
                if (previousVertex != null) {
389
                    length += previousVertex.distance(vertex);
390
                }
391
                previousVertex = vertex;
392
            }
393
        }
394
        return length;
395
    }
396

  
397
    /**
398
     * Extracts the lines in a Line or Multiline
399
     * @param geometry
400
     * @return
401
     */
402
    static protected List<Line> extractLines(Geometry geometry){
403
        List<Line> lines=new ArrayList<Line>();
404
        if (geometry instanceof  Line){
405
            lines.add((Line) geometry);
406
        }
407
        if (geometry instanceof MultiLine){
408
            MultiLine multiline=(MultiLine)geometry;
409
            for (int i=0;i<multiline.getPrimitivesNumber();i++){
410
                lines.add((Line) multiline.getPrimitiveAt(i));
411
            }
412
        }
413
        return lines;
414
    }
415

  
416
    static protected List<Point> extractPoints(Geometry geometry){
417
        List<Point> points=new ArrayList<Point>();
418
        List<Line> lines=LrsAlgorithmUtils.extractLines(geometry);
419
        for (Line line: lines){
420
            for (int i=0;i<line.getNumVertices();i++){
421
                points.add(line.getVertex(i));
422
            }
423
        }
424
        return points;
425
    }
426

  
427

  
428
    static protected Point extractFirstPoint(Geometry geometry){
429
        Point firstPoint=null;
430
        List<Line> lines= LrsAlgorithmUtils.extractLines(geometry);
431
        if (lines!=null && !lines.isEmpty()){
432
            firstPoint=lines.get(0).getVertex(0);
433
        }
434
        return firstPoint;
435
    }
436

  
437
    static protected boolean equalPoints(Point point1, Point point2){
438
        if ( point1.getX()==point2.getX() && point1.getY()==point2.getY()){
439
            return true;
440
        }
441
        return false;
442
    }
443

  
444
    static protected Geometry insertVertex(Geometry geometry, Point vertex, int index){
445
        if (geometry instanceof Line){
446
            Line line=(Line)geometry;
447
            line.insertVertex(index, vertex);
448
        }
449
        if (geometry instanceof MultiLine){
450
            List<Line> lines=extractLines(geometry);
451
            for(Line line:lines){
452
                if (index<=line.getNumVertices()){
453
                    line.insertVertex(index, vertex);
454
                    return geometry;
455
                }else{
456
                    index-=line.getNumVertices();
457
                }
458

  
459
            }
460
        }
461
        return geometry;
462
    }
463

  
464
    /**
465
     * Reduced versi?n of straight line through two points equation to calculate M's
466
     *
467
     * @param totalLength
468
     * @param minValue
469
     * @param maxValue
470
     * @param relativeDistance
471
     * @return
472
     */
473
    static protected double calculateM(Double totalLength, Double minValue, Double maxValue, Double relativeDistance) {
474
        return LrsAlgorithmUtils.straightLineThroughTwoPointsEquation(0, totalLength, minValue, maxValue, relativeDistance);
475
    }
476

  
477
    /**
478
     * Straight line through two points equation.
479
     *
480
     * @param x1
481
     * @param x2
482
     * @param y1
483
     * @param y2
484
     * @param x
485
     * @return
486
     */
487
    static protected double straightLineThroughTwoPointsEquation(double x1, double x2, double y1, double y2, double x) {
488
        if (x2 - x1 == 0.0) {
489
            return Double.POSITIVE_INFINITY;
490
        }
491
        return ((y2 - y1) * (x - x1) / (x2 - x1)) + y1;
492
    }
493

  
494
}
org.gvsig.lrs/tags/org.gvsig.lrs-1.0.180/org.gvsig.lrs.lib/org.gvsig.lrs.lib.impl/src/main/java/org/gvsig/lrs/lib/impl/DefaultLrsEditRouteCalibrationSelectIdRouteAlgorithmParams.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.LrsEditRouteCalibrationSelectIdRouteAlgorithmParams;
31
import org.gvsig.lrs.lib.api.LrsMeasureCalculationMethods;
32

  
33

  
34
/**
35
 * @author dmartinez
36
 *
37
 */
38
public class DefaultLrsEditRouteCalibrationSelectIdRouteAlgorithmParams implements LrsEditRouteCalibrationSelectIdRouteAlgorithmParams {
39

  
40
    private FeatureAttributeDescriptor idRouteField;
41
    private final String NAME = "LrsEditRouteCalibrationSelectIdRouteAlgorithm";
42
    private final String DESCRIPTION = "Algorithm to select id route to edit calibration with linear reference system.";
43

  
44

  
45
    /* (non-Javadoc)
46
     * @see org.gvsig.lrs.lib.api.LrsAlgorithmParams#getName()
47
     */
48
    public String getName() {
49
        return NAME;
50
    }
51

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

  
59
    /*
60
     * (non-Javadoc)
61
     * @see org.gvsig.lrs.lib.api.LrsCreateRouteAlgorithmParams#getIdRouteField()
62
     */
63
    public FeatureAttributeDescriptor getIdRouteField() {
64
        return idRouteField;
65
    }
66

  
67
    /*
68
     * (non-Javadoc)
69
     * @see org.gvsig.lrs.lib.api.LrsCreateRouteAlgorithmParams#setIdRouteField(org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor)
70
     */
71
    public void setIdRouteField(FeatureAttributeDescriptor idRouteField) {
72
        this.idRouteField = idRouteField;
73
    }
74

  
75
}
org.gvsig.lrs/tags/org.gvsig.lrs-1.0.180/org.gvsig.lrs.lib/org.gvsig.lrs.lib.impl/src/main/java/org/gvsig/lrs/lib/impl/DefaultLrsAlgorithmsManager.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.lrs.lib.api.MeasuresCalculator;
26

  
27
import java.util.List;
28

  
29
import org.apache.commons.lang3.StringUtils;
30
import org.cresques.cts.IProjection;
31

  
32
import org.gvsig.fmap.dal.DALLocator;
33
import org.gvsig.fmap.dal.DataManager;
34
import org.gvsig.fmap.dal.DataServerExplorer;
35
import org.gvsig.fmap.dal.DataServerExplorerParameters;
36
import org.gvsig.fmap.dal.DataStoreParameters;
37
import org.gvsig.fmap.dal.exception.DataException;
38
import org.gvsig.fmap.dal.exception.InitializeException;
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff