Revision 66

View differences:

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

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

  
32

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

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

  
52

  
53

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

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

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

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

  
84

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

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

  
101

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  
25
import java.io.File;
26
import java.util.Iterator;
27
import java.util.List;
28

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

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

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

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

  
79
    private LrsGenerateDynamicSegmentationAlgorithmParams parameters;
80

  
81

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

  
88
    }
89

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

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

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

  
122
        logger.info(parameters.toString());
123

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

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

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

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

  
171
            tableFeatures.accept(new Visitor() {
172

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

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

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

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

  
208
                    FeatureSet features = sourceFeatureStore.getFeatureSet(query);
209
                    features.accept(new Visitor() {
210

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

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

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

  
238
                    FeatureSet features = sourceFeatureStore.getFeatureSet(query);
239
                    features.accept(new Visitor() {
240

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

  
259
            newFeatureStore.finishEditing();
260

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

  
266

  
267
        taskStatus.terminate();
268

  
269
    }
270

  
271

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

  
281
    }
282

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

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

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

  
313
            featureType.setDefaultGeometryAttributeName("Geometry");
314

  
315
            shapeStoreParams.setDefaultFeatureType(featureType);
316
            serverExplorer.add("Shape", shapeStoreParams, true);
317

  
318
            DataStore store = dataManager.openStore(SHPStoreProvider.NAME, shapeStoreParams);
319

  
320
            return (FeatureStore)store;
321

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

  
327

  
328
}
329

  
330

  
331

  
332

  
org.gvsig.lrs/tags/org.gvsig.lrs-1.0.1/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.1/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

  
40

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

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

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

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

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

  
70
}
org.gvsig.lrs/tags/org.gvsig.lrs-1.0.1/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.Iterator;
12
import java.util.List;
13

  
14
import javax.swing.text.html.HTMLDocument.HTMLReader.IsindexAction;
15

  
16
import org.gvsig.fmap.dal.DALLocator;
17
import org.gvsig.fmap.dal.DataManager;
18
import org.gvsig.fmap.dal.DataServerExplorer;
19
import org.gvsig.fmap.dal.DataServerExplorerParameters;
20
import org.gvsig.fmap.dal.DataStore;
21
import org.gvsig.fmap.dal.DataTypes;
22
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
23
import org.gvsig.fmap.dal.feature.EditableFeatureType;
24
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
25
import org.gvsig.fmap.dal.feature.FeatureStore;
26
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
27
import org.gvsig.fmap.dal.store.shp.SHPNewStoreParameters;
28
import org.gvsig.fmap.geom.Geometry;
29
import org.gvsig.fmap.geom.GeometryLocator;
30
import org.gvsig.fmap.geom.GeometryManager;
31
import org.gvsig.fmap.geom.aggregate.MultiLine;
32
import org.gvsig.fmap.geom.exception.CreateGeometryException;
33
import org.gvsig.fmap.geom.operation.GeometryOperationException;
34
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
35
import org.gvsig.fmap.geom.primitive.Line;
36
import org.gvsig.fmap.geom.primitive.Point;
37
import org.gvsig.fmap.geom.type.GeometryType;
38
import org.gvsig.lrs.lib.api.exceptions.LrsCreateRouteException;
39
import org.gvsig.tools.dataTypes.DataType;
40
import org.gvsig.tools.locator.LocatorException;
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
            SHPNewStoreParameters shapeStoreParams = (SHPNewStoreParameters) newFeatureStoreParameters;
53
            File file = shapeStoreParams.getFile();
54
            String filePath = file.getPath().substring(0, file.getPath().lastIndexOf(File.separator));
55

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

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

  
70
            featureType.setDefaultGeometryAttributeName("Geometry");
71

  
72
            shapeStoreParams.setDefaultFeatureType(featureType);
73
            serverExplorer.add("Shape", shapeStoreParams, true);
74

  
75
            DataStore store = dataManager.createStore(shapeStoreParams);
76

  
77
            return (FeatureStore) store;
78

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

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

  
97
    static List<Point> getPointsWithM(Geometry geom, double m) throws CreateGeometryException, LocatorException {
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 point = getPointWithMBetweenTwoMPoints(m, line.getVertex(i), line.getVertex(i + 1));
104
                if (point != null) {
105
                    points.add(point);
106
                }
107
            }
108
        } else if (geom instanceof MultiLine) {
109
            MultiLine multiLine = (MultiLine) geom;
110
            for (int p = 0; p < multiLine.getPrimitivesNumber(); p++) {
111
                Line line = (Line) multiLine.getPrimitiveAt(p);
112
                for (int i = 0; i < line.getNumVertices() - 1; i++) {
113
                    Point point = getPointWithMBetweenTwoMPoints(m, line.getVertex(i), line.getVertex(i + 1));
114
                    if (point != null) {
115
                        points.add(point);
116
                    }
117
                }
118
            }
119
        }
120
        return points;
121
    }
122

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

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

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

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

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

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

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

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

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

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

  
310

  
311

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

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

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

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

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

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

  
425

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

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

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

  
457
            }
458
        }
459
        return geometry;
460
    }
461

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

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

  
492
}
org.gvsig.lrs/tags/org.gvsig.lrs-1.0.1/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
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff