Revision 35 org.gvsig.lrs/trunk/org.gvsig.lrs/org.gvsig.lrs.lib/org.gvsig.lrs.lib.impl/src/main/java/org/gvsig/lrs/lib/impl/LrsCalibrateRouteAlgorithm.java
LrsCalibrateRouteAlgorithm.java | ||
---|---|---|
22 | 22 |
*/ |
23 | 23 |
package org.gvsig.lrs.lib.impl; |
24 | 24 |
|
25 |
import java.io.File; |
|
26 | 25 |
import java.util.ArrayList; |
27 | 26 |
import java.util.HashMap; |
28 |
import java.util.Iterator; |
|
29 | 27 |
import java.util.List; |
30 | 28 |
import java.util.Map; |
31 | 29 |
import java.util.Map.Entry; |
32 | 30 |
|
33 |
import org.apache.commons.lang3.mutable.Mutable; |
|
34 |
import org.apache.commons.lang3.mutable.MutableInt; |
|
35 |
import org.gvsig.fmap.dal.DALLocator; |
|
36 |
import org.gvsig.fmap.dal.DataManager; |
|
37 |
import org.gvsig.fmap.dal.DataServerExplorer; |
|
38 |
import org.gvsig.fmap.dal.DataServerExplorerParameters; |
|
39 |
import org.gvsig.fmap.dal.DataStore; |
|
40 |
import org.gvsig.fmap.dal.DataStoreParameters; |
|
41 |
import org.gvsig.fmap.dal.DataTypes; |
|
42 |
import org.gvsig.fmap.dal.exception.DataException; |
|
31 |
import org.cresques.cts.IProjection; |
|
43 | 32 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
44 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
|
45 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
|
46 | 33 |
import org.gvsig.fmap.dal.feature.Feature; |
47 | 34 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
48 |
import org.gvsig.fmap.dal.feature.FeatureReference; |
|
49 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
|
50 | 35 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
51 | 36 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
52 | 37 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
53 |
import org.gvsig.fmap.dal.store.shp.SHPNewStoreParameters; |
|
54 | 38 |
import org.gvsig.fmap.geom.Geometry; |
55 | 39 |
import org.gvsig.fmap.geom.GeometryLocator; |
56 | 40 |
import org.gvsig.fmap.geom.GeometryManager; |
57 |
import org.gvsig.fmap.geom.aggregate.MultiLine; |
|
58 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
|
59 | 41 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
60 | 42 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
61 | 43 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
62 |
import org.gvsig.fmap.geom.primitive.Envelope; |
|
63 | 44 |
import org.gvsig.fmap.geom.primitive.Line; |
64 | 45 |
import org.gvsig.fmap.geom.primitive.Point; |
65 |
import org.gvsig.fmap.geom.type.GeometryType; |
|
66 | 46 |
import org.gvsig.lrs.lib.api.DistanceUnits; |
67 | 47 |
import org.gvsig.lrs.lib.api.LrsAlgorithm; |
68 | 48 |
import org.gvsig.lrs.lib.api.LrsAlgorithmParams; |
69 | 49 |
import org.gvsig.lrs.lib.api.LrsCalibrateRouteAlgorithmParams; |
70 |
import org.gvsig.lrs.lib.api.LrsCoordinatesPriority; |
|
71 | 50 |
import org.gvsig.lrs.lib.api.LrsCreateRouteAlgorithmParams; |
72 | 51 |
import org.gvsig.lrs.lib.api.LrsMeasureCalculationMethods; |
73 |
import org.gvsig.lrs.lib.api.LrsSourceOfMeasures; |
|
74 |
import org.gvsig.lrs.lib.api.exceptions.LrsCreateRouteException; |
|
52 |
import org.gvsig.lrs.lib.api.exceptions.LrsCalibrateRouteException; |
|
75 | 53 |
import org.gvsig.lrs.lib.api.exceptions.LrsException; |
76 | 54 |
import org.gvsig.tools.ToolsLocator; |
77 |
import org.gvsig.tools.dataTypes.DataType; |
|
78 | 55 |
import org.gvsig.tools.exception.BaseException; |
79 | 56 |
import org.gvsig.tools.i18n.I18nManager; |
80 | 57 |
import org.gvsig.tools.service.Manager; |
... | ... | |
135 | 112 |
* @see org.gvsig.lrs.lib.api.LrsAlgorithm#execute(org.gvsig.tools.task.SimpleTaskStatus) |
136 | 113 |
*/ |
137 | 114 |
public void execute(final SimpleTaskStatus taskStatus) throws LrsException { |
138 |
NewFeatureStoreParameters newFeatureStoreParameters = parameters.getNewFeatureStoreParameters();
|
|
139 |
|
|
115 |
NewFeatureStoreParameters newFeatureStoreParameters = |
|
116 |
parameters.getNewFeatureStoreParameters(); |
|
140 | 117 |
FeatureStore sourceFeatureStore = parameters.getSourceFeatureStore(); |
141 | 118 |
FeatureAttributeDescriptor idRouteField = parameters.getIdRouteField(); |
119 |
FeatureStore calibratePointFeatureStore= parameters.getCalibratePointFeatureStore(); |
|
120 |
FeatureAttributeDescriptor idRouteCalibratePointField =parameters.getCalibratePointIdRouteField(); |
|
121 |
FeatureAttributeDescriptor fromMeasureField = parameters.getFromMeasureField(); |
|
122 |
final boolean includeAll=parameters.includeAll(); |
|
142 | 123 |
|
143 | 124 |
logger.info(parameters.toString()); |
144 | 125 |
|
145 | 126 |
taskStatus.setTitle(parameters.getName()); |
146 | 127 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
147 |
taskStatus.message(i18nManager.getTranslation("calibrating_features"));
|
|
128 |
taskStatus.message(i18nManager.getTranslation("grouping_features"));
|
|
148 | 129 |
|
149 | 130 |
try { |
150 |
final FeatureStore newFeatureStore=LrsAlgorithmUtils.createNewDataStore(newFeatureStoreParameters,idRouteField); |
|
151 |
newFeatureStore.edit(FeatureStore.MODE_FULLEDIT); |
|
131 |
final String routeFieldName = idRouteField.getName(); |
|
132 |
final String routeCalibratePointFieldName=idRouteCalibratePointField.getName(); |
|
133 |
final String measureFieldName=fromMeasureField.getName(); |
|
134 |
final Double convertedSearchRadius=calculateConvertedSearchRadius(); |
|
152 | 135 |
|
136 |
FeatureStore newFeatureStore = LrsAlgorithmUtils |
|
137 |
.createNewDataStore(newFeatureStoreParameters, idRouteField); |
|
138 |
|
|
153 | 139 |
FeatureSet sourceFeatures; |
154 |
if (sourceFeatureStore.getFeatureSelection().getSize()>0){
|
|
155 |
sourceFeatures=sourceFeatureStore.getFeatureSelection();
|
|
156 |
}else{
|
|
157 |
sourceFeatures=sourceFeatureStore.getFeatureSet();
|
|
140 |
if (sourceFeatureStore.getFeatureSelection().getSize() > 0) {
|
|
141 |
sourceFeatures = sourceFeatureStore.getFeatureSelection();
|
|
142 |
} else {
|
|
143 |
sourceFeatures = sourceFeatureStore.getFeatureSet();
|
|
158 | 144 |
} |
159 | 145 |
|
160 |
taskStatus.setRangeOfValues(0, sourceFeatures.getSize()-1); |
|
161 |
final MutableInt taskCount=new MutableInt(); |
|
162 |
taskCount.setValue(0); |
|
163 |
|
|
146 |
final Map<String, Object[]> featuresMap = |
|
147 |
new HashMap<String, Object[]>(); |
|
164 | 148 |
sourceFeatures.accept(new Visitor() { |
165 | 149 |
|
166 |
public void visit(Object obj) throws VisitCanceledException, BaseException { |
|
167 |
Feature feature=(Feature)obj; |
|
150 |
public void visit(Object obj) |
|
151 |
throws VisitCanceledException, BaseException { |
|
152 |
Feature feature = (Feature) obj; |
|
153 |
String routeName = (String) feature.get(routeFieldName); |
|
154 |
List<Point> points = new ArrayList<Point>(); |
|
155 |
Object[] valueObject=new Object[2]; |
|
156 |
valueObject[0]=feature.getDefaultGeometry(); |
|
157 |
valueObject[1]=points; |
|
168 | 158 |
|
169 |
EditableFeature calibratedFeature=calibrateRoute(feature); |
|
159 |
featuresMap.put(routeName, valueObject); |
|
160 |
} |
|
161 |
}); |
|
170 | 162 |
|
171 |
newFeatureStore.update(calibratedFeature); |
|
163 |
FeatureSet pointFeatures=calibratePointFeatureStore.getFeatureSet(); |
|
164 |
pointFeatures.accept(new Visitor() { |
|
172 | 165 |
|
173 |
taskStatus.setCurValue(taskCount.getValue()); |
|
174 |
taskCount.increment(); |
|
166 |
public void visit(Object obj) throws VisitCanceledException, BaseException { |
|
167 |
Feature feature = (Feature) obj; |
|
168 |
String routeName = (String) feature.get(routeCalibratePointFieldName); |
|
169 |
Double measure= (Double) feature.get(measureFieldName); |
|
170 |
Geometry geomPoint=(Point)feature.getDefaultGeometry(); |
|
171 |
|
|
172 |
if (geomPoint instanceof Point){ |
|
173 |
Point point=(Point) geomPoint; |
|
174 |
Object valueObject=featuresMap.get(routeName); |
|
175 |
if (valueObject!=null && valueObject instanceof Object[]){ |
|
176 |
Object[] objArray=(Object[])valueObject; |
|
177 |
if (objArray[1] instanceof List){ |
|
178 |
List<Point> points=(List<Point>)objArray[1]; |
|
179 |
if (feature.getDefaultGeometry() instanceof Point){ |
|
180 |
Geometry geometry=(Geometry)objArray[0]; |
|
181 |
if (isValidPoint(point,geometry, convertedSearchRadius)){ |
|
182 |
try { |
|
183 |
Point mPoint= pointToMPoint(geometry,point, measure); |
|
184 |
points.add(mPoint); |
|
185 |
} catch (Exception e) { |
|
186 |
logger.debug("Error adding point",e); |
|
187 |
} |
|
188 |
} |
|
189 |
} |
|
190 |
} |
|
191 |
} |
|
192 |
} |
|
175 | 193 |
} |
176 | 194 |
}); |
177 |
newFeatureStore.finishEditing(); |
|
178 | 195 |
|
196 |
taskStatus.setRangeOfValues(0, featuresMap.size()); |
|
197 |
int taskCount = 0; |
|
179 | 198 |
|
199 |
newFeatureStore.edit(FeatureStore.MODE_FULLEDIT); |
|
200 |
|
|
201 |
for (Entry<String, Object[]> entry : featuresMap |
|
202 |
.entrySet()) { |
|
203 |
String routeName = entry.getKey(); |
|
204 |
Object valueObject = entry.getValue(); |
|
205 |
List<Point> points=null; |
|
206 |
Geometry geometry=null; |
|
207 |
|
|
208 |
if (valueObject!=null && valueObject instanceof Object[]){ |
|
209 |
Object[] objArray=(Object[])valueObject; |
|
210 |
if (objArray[0] instanceof Geometry){ |
|
211 |
geometry=(Geometry)objArray[0]; |
|
212 |
} |
|
213 |
if (objArray[1] instanceof List){ |
|
214 |
points=(List<Point>)objArray[1]; |
|
215 |
} |
|
216 |
} |
|
217 |
if (!points.isEmpty()){ |
|
218 |
EditableFeature newFeature = |
|
219 |
newFeatureStore.createNewFeature(true); |
|
220 |
newFeature.set(routeFieldName, routeName); |
|
221 |
Geometry calibratedRoute = calibrateRoute(geometry,points); |
|
222 |
newFeature.setDefaultGeometry(calibratedRoute); |
|
223 |
newFeatureStore.update(newFeature); |
|
224 |
}else{ |
|
225 |
if (includeAll){ |
|
226 |
EditableFeature newFeature = |
|
227 |
newFeatureStore.createNewFeature(true); |
|
228 |
newFeature.set(routeFieldName, routeName); |
|
229 |
newFeature.setDefaultGeometry(geometry); |
|
230 |
newFeatureStore.update(newFeature); |
|
231 |
} |
|
232 |
} |
|
233 |
taskCount++; |
|
234 |
taskStatus.setCurValue(taskCount); |
|
235 |
} |
|
236 |
newFeatureStore.finishEditing(); |
|
180 | 237 |
} catch (Exception e1) { |
181 | 238 |
taskStatus.abort(); |
182 |
throw new LrsCreateRouteException("Error calibrating routes", e1);
|
|
239 |
throw new LrsCalibrateRouteException("Error calibrating routes", e1);
|
|
183 | 240 |
} |
184 | 241 |
|
185 | 242 |
taskStatus.terminate(); |
186 |
taskStatus.terminate(); |
|
187 | 243 |
} |
188 | 244 |
|
189 |
private EditableFeature calibrateRoute(Feature feature) { |
|
190 |
FeatureStore calibratePointFeatureStore= parameters.getCalibratePointFeatureStore(); |
|
191 |
FeatureAttributeDescriptor idRouteCalibratePointField =parameters.getCalibratePointIdRouteField(); |
|
192 |
FeatureAttributeDescriptor fromMeasureField = parameters.getFromMeasureField(); |
|
193 | 245 |
|
194 |
LrsMeasureCalculationMethods calculationMethod= parameters.getMeasureCalculationMethods(); |
|
246 |
private Point pointToMPoint(Geometry geometry,Point point, Double measure) throws CreateGeometryException, GeometryOperationNotSupportedException, GeometryOperationException{ |
|
247 |
GeometryManager geomanager = GeometryLocator.getGeometryManager(); |
|
248 |
|
|
249 |
Point[] pointsInLine=(Point[])geometry.closestPoints(point); |
|
250 |
Point pointInLine=pointsInLine[0]; |
|
251 |
|
|
252 |
Point mPoint = (Point) geomanager |
|
253 |
.create(Geometry.TYPES.POINT, Geometry.SUBTYPES.GEOM2DM); |
|
254 |
mPoint.setX(pointInLine.getX()); |
|
255 |
mPoint.setY(pointInLine.getY()); |
|
256 |
int mDimension=mPoint.getDimension()-1; |
|
257 |
mPoint.setCoordinateAt(mDimension, measure); |
|
258 |
|
|
259 |
return mPoint; |
|
260 |
} |
|
261 |
|
|
262 |
private Double calculateConvertedSearchRadius(){ |
|
195 | 263 |
Double searchRadius=parameters.getSearchRadius(); |
196 | 264 |
DistanceUnits measureUnits = parameters.getMeasureUnits(); |
265 |
FeatureStore sourceFeatureStore = parameters.getSourceFeatureStore(); |
|
266 |
|
|
267 |
IProjection projection; |
|
268 |
try { |
|
269 |
projection = sourceFeatureStore.getDefaultFeatureType().getDefaultSRS(); |
|
270 |
|
|
271 |
Double searchRadiusInMeters=searchRadius*measureUnits.getTransToMeter(); |
|
272 |
//TODO |
|
273 |
//DistanceUnits projectionDistanceUnits=getProjectionDistanceUnits(projection); |
|
274 |
//return searchRadiusInMeters/projectionDistanceUnits.getTransToMeter(); |
|
275 |
return searchRadiusInMeters; |
|
276 |
} catch (Exception e) { |
|
277 |
return new Double(0); |
|
278 |
} |
|
279 |
} |
|
280 |
|
|
281 |
private boolean isValidPoint(Point point,Geometry geometry, Double convertedSearchRadius){ |
|
282 |
try { |
|
283 |
return geometry.isWithinDistance(point, convertedSearchRadius); |
|
284 |
} catch (Exception e) { |
|
285 |
return false; |
|
286 |
} |
|
287 |
} |
|
288 |
|
|
289 |
private Geometry calibrateRoute(Geometry geometry, List<Point> points) { |
|
290 |
LrsMeasureCalculationMethods calculationMethod= parameters.getMeasureCalculationMethods(); |
|
291 |
boolean ignoreSpatialGaps = parameters.ignoreSpatialGaps(); |
|
197 | 292 |
boolean interpolateBetweenCalibrationPoints=parameters.interpolateBetweenCalibrationPoints(); |
198 | 293 |
boolean extrapolateBeforeCalibrationPoints=parameters.extrapolateBeforeCalibrationPoints(); |
199 | 294 |
boolean extrapolateAfterCalibrationPoints=parameters.extrapolateAfterCalibrationPoints(); |
200 |
boolean ignoreSpatialGaps = parameters.ignoreSpatialGaps(); |
|
201 |
boolean includeAll=parameters.includeAll(); |
|
202 | 295 |
|
203 |
// TODO Must calculate the calibrated Feature |
|
296 |
if (points.isEmpty()){ |
|
297 |
return geometry; |
|
298 |
} |
|
299 |
ExpandedRoute expandedRoute=new ExpandedRoute(geometry, ignoreSpatialGaps); |
|
204 | 300 |
|
205 |
return null; |
|
301 |
|
|
302 |
switch (calculationMethod) { |
|
303 |
case DISTANCE: |
|
304 |
expandedRoute.resetMeasuresToDistances(); |
|
305 |
case MEASURES: |
|
306 |
default: |
|
307 |
expandedRoute.addFixedPoints(points); |
|
308 |
if (interpolateBetweenCalibrationPoints){ |
|
309 |
expandedRoute.interpolateBetweenCalibrationPoints(); |
|
310 |
} |
|
311 |
if (extrapolateBeforeCalibrationPoints){ |
|
312 |
expandedRoute.extrapolateBeforeCalibrationPoints(); |
|
313 |
} |
|
314 |
if (extrapolateAfterCalibrationPoints){ |
|
315 |
expandedRoute.extrapolateAfterCalibrationPoints(); |
|
316 |
} |
|
317 |
return expandedRoute.getRoute(); |
|
318 |
} |
|
319 |
|
|
206 | 320 |
} |
207 | 321 |
|
208 | 322 |
|
209 |
class MStructure{ |
|
210 |
Geometry geometry; |
|
211 |
Double fromField; |
|
212 |
Double toField; |
|
323 |
|
|
324 |
|
|
325 |
|
|
326 |
|
|
327 |
class ExpandedRoute{ |
|
328 |
private Geometry route; |
|
329 |
private List<Boolean> isFixedPoint; |
|
330 |
private List<Double> distances; |
|
331 |
private List<Double>oldMValues; |
|
332 |
private List<Point> vertices; |
|
333 |
private int MDIMENSION; |
|
334 |
private int maxMFixedPointPos; |
|
335 |
private int minMFixedPointPos; |
|
336 |
private final Double PRECISION=new Double(1.0e-10); |
|
337 |
|
|
338 |
public Geometry getRoute(){ |
|
339 |
return route; |
|
340 |
} |
|
341 |
|
|
342 |
public ExpandedRoute(Geometry geometry, boolean ignoreSpatialGaps){ |
|
343 |
isFixedPoint=new ArrayList<Boolean>(); |
|
344 |
distances=new ArrayList<Double>(); |
|
345 |
oldMValues=new ArrayList<Double>(); |
|
346 |
vertices=new ArrayList<Point>(); |
|
347 |
route=geometry.cloneGeometry(); |
|
348 |
|
|
349 |
double distance = 0; |
|
350 |
List<Line> lines=LrsAlgorithmUtils.extractLines(geometry); |
|
351 |
Point previousVertex=null; |
|
352 |
for (int i=0; i<lines.size();i++){ |
|
353 |
Line line=lines.get(i); |
|
354 |
for (int j=0; j<line.getNumVertices();j++){ |
|
355 |
Point vertex=line.getVertex(j); |
|
356 |
if(j==0){ |
|
357 |
if (previousVertex==null){ |
|
358 |
//First point in geometry |
|
359 |
MDIMENSION=vertex.getDimension()-1; |
|
360 |
distance=0; |
|
361 |
} |
|
362 |
Integer visitedIndex = getIndexVisitedVertex(vertex); |
|
363 |
if (visitedIndex!=null){ |
|
364 |
previousVertex=vertices.get(visitedIndex); |
|
365 |
distance=distances.get(visitedIndex); |
|
366 |
}else{ |
|
367 |
if (previousVertex!=null){ |
|
368 |
try { |
|
369 |
if (!ignoreSpatialGaps){ |
|
370 |
distance+=previousVertex.distance(vertex); |
|
371 |
} |
|
372 |
} catch (Exception e) { |
|
373 |
distance=Double.NaN; |
|
374 |
} |
|
375 |
} |
|
376 |
} |
|
377 |
}else{ |
|
378 |
try { |
|
379 |
distance+=vertex.distance(previousVertex); |
|
380 |
} catch (Exception e) { |
|
381 |
distance=Double.NaN; |
|
382 |
} |
|
383 |
} |
|
384 |
Double mValue=vertex.getCoordinateAt(MDIMENSION); |
|
385 |
|
|
386 |
isFixedPoint.add(false); |
|
387 |
vertices.add(vertex); |
|
388 |
distances.add(distance); |
|
389 |
oldMValues.add(mValue); |
|
390 |
|
|
391 |
previousVertex = vertex; |
|
392 |
} |
|
393 |
} |
|
394 |
} |
|
395 |
|
|
396 |
private Integer getIndexVisitedVertex(Point point){ |
|
397 |
double distance = Double.NEGATIVE_INFINITY; |
|
398 |
Integer index = null; |
|
399 |
for(int i =0; i<vertices.size(); i++){ |
|
400 |
if (LrsAlgorithmUtils.equalPoints(point, vertices.get(i))){ |
|
401 |
if(this.distances.get(i)>distance){ |
|
402 |
distance = this.distances.get(i); |
|
403 |
index = i; |
|
404 |
} |
|
405 |
} |
|
406 |
} |
|
407 |
return index; |
|
408 |
} |
|
409 |
|
|
410 |
private void addFixedPoints(List<Point> fixedPoints){ |
|
411 |
Double minMValue=null; |
|
412 |
Double maxMValue=null; |
|
413 |
for (Point fixedPoint:fixedPoints){ |
|
414 |
Double length=Double.valueOf(0); |
|
415 |
int index = 0; |
|
416 |
List<Line> lines=LrsAlgorithmUtils.extractLines(route); |
|
417 |
List<Integer>insertAtList=new ArrayList<Integer>(); |
|
418 |
List<Double>lengths=new ArrayList<Double>(); |
|
419 |
List<Double>oldValues=new ArrayList<Double>(); |
|
420 |
for (int i=0; i<lines.size();i++){ |
|
421 |
Line line=lines.get(i); |
|
422 |
try{ |
|
423 |
//FIXME |
|
424 |
//if (line.intersects(fixedPoint)){ |
|
425 |
if (line.isWithinDistance(fixedPoint, PRECISION)){ |
|
426 |
length=distances.get(index); |
|
427 |
for (int j=0;j<line.getNumVertices();j++){ |
|
428 |
Point vertex=line.getVertex(j); |
|
429 |
Point nextVertex; |
|
430 |
if (j+1<line.getNumVertices()){ |
|
431 |
nextVertex=line.getVertex(j+1); |
|
432 |
}else{ |
|
433 |
nextVertex=null; |
|
434 |
} |
|
435 |
if(LrsAlgorithmUtils.equalPoints(vertex, fixedPoint)){ |
|
436 |
oldMValues.add(index, vertex.getCoordinateAt(MDIMENSION)); |
|
437 |
Double mValue=fixedPoint.getCoordinateAt(MDIMENSION); |
|
438 |
vertex.setCoordinateAt(MDIMENSION, mValue); |
|
439 |
isFixedPoint.set(index, true); |
|
440 |
vertices.set(index, vertex); |
|
441 |
|
|
442 |
if (minMValue==null||mValue.compareTo(minMValue)<0){ |
|
443 |
minMValue=mValue; |
|
444 |
minMFixedPointPos=index; |
|
445 |
} |
|
446 |
if (maxMValue==null||mValue.compareTo(maxMValue)>=0){ |
|
447 |
maxMValue=mValue; |
|
448 |
maxMFixedPointPos=index; |
|
449 |
} |
|
450 |
}else{ |
|
451 |
if (nextVertex!=null && !LrsAlgorithmUtils.equalPoints(nextVertex, fixedPoint)){ |
|
452 |
GeometryManager geomanager = GeometryLocator.getGeometryManager(); |
|
453 |
Line segment= (Line) geomanager |
|
454 |
.create(Geometry.TYPES.LINE, Geometry.SUBTYPES.GEOM2DM); |
|
455 |
segment.addVertex(vertex); |
|
456 |
segment.addVertex(nextVertex); |
|
457 |
//FIXME |
|
458 |
//if (line.intersects(fixedPoint)){ |
|
459 |
if (segment.isWithinDistance(fixedPoint, PRECISION)){ |
|
460 |
length+=vertex.distance(fixedPoint); |
|
461 |
Double oldMValue=LrsAlgorithmUtils.calculateNewM( |
|
462 |
nextVertex.getCoordinateAt(MDIMENSION), |
|
463 |
vertex.getCoordinateAt(MDIMENSION), |
|
464 |
distances.get(index+1), |
|
465 |
distances.get(index), |
|
466 |
length); |
|
467 |
insertAtList.add(index+1); |
|
468 |
lengths.add(length); |
|
469 |
oldValues.add(oldMValue); |
|
470 |
} |
|
471 |
} |
|
472 |
} |
|
473 |
index++; |
|
474 |
} |
|
475 |
}else{ |
|
476 |
index+=line.getNumVertices(); |
|
477 |
} |
|
478 |
}catch(Exception e){ |
|
479 |
logger.warn("Error inserting point "+fixedPoint.toString(),e); |
|
480 |
} |
|
481 |
} |
|
482 |
int pos=0; |
|
483 |
for (Integer insertAt:insertAtList){ |
|
484 |
Double distance=lengths.get(pos); |
|
485 |
Double oldMValue=oldValues.get(pos); |
|
486 |
int correctedPosition=insertAt+pos; |
|
487 |
LrsAlgorithmUtils.insertVertex(route, fixedPoint, correctedPosition); |
|
488 |
isFixedPoint.add(correctedPosition, true); |
|
489 |
distances.add(correctedPosition, distance); |
|
490 |
oldMValues.add(correctedPosition, oldMValue); |
|
491 |
|
|
492 |
vertices.add(correctedPosition, fixedPoint); |
|
493 |
correctPosteriorPositions(correctedPosition); |
|
494 |
|
|
495 |
Double mValue=fixedPoint.getCoordinateAt(MDIMENSION); |
|
496 |
if (minMValue==null||mValue.compareTo(minMValue)<0){ |
|
497 |
minMValue=mValue; |
|
498 |
minMFixedPointPos=correctedPosition; |
|
499 |
} |
|
500 |
if (maxMValue==null||mValue.compareTo(maxMValue)>=0){ |
|
501 |
maxMValue=mValue; |
|
502 |
maxMFixedPointPos=correctedPosition; |
|
503 |
} |
|
504 |
pos++; |
|
505 |
} |
|
506 |
} |
|
507 |
} |
|
508 |
|
|
509 |
private void correctPosteriorPositions(int pos){ |
|
510 |
if (minMFixedPointPos>=pos){ |
|
511 |
minMFixedPointPos++; |
|
512 |
} |
|
513 |
if (maxMFixedPointPos>=pos){ |
|
514 |
maxMFixedPointPos++; |
|
515 |
} |
|
516 |
|
|
517 |
} |
|
518 |
|
|
519 |
public void resetMeasuresToDistances(){ |
|
520 |
List<Point> points=LrsAlgorithmUtils.extractPoints(route); |
|
521 |
for (int i=0;i<points.size();i++){ |
|
522 |
if (!isFixedPoint.get(i)){ |
|
523 |
points.get(i).setCoordinateAt(MDIMENSION, distances.get(i)); |
|
524 |
vertices.get(i).setCoordinateAt(MDIMENSION, distances.get(i)); |
|
525 |
} |
|
526 |
} |
|
527 |
} |
|
528 |
|
|
529 |
public void interpolateBetweenCalibrationPoints(){ |
|
530 |
Double newMValueBeforePoint=Double.NaN; |
|
531 |
Double oldMValueBeforePoint=Double.NaN; |
|
532 |
Double calibrationRatio=Double.NaN; |
|
533 |
for (int i=minMFixedPointPos; i<=maxMFixedPointPos-1;i++){ |
|
534 |
if (isFixedPoint.get(i)){ |
|
535 |
int pointBeforePos=i; |
|
536 |
int pointAfterPos=getNextPosFixedPoint(i); |
|
537 |
|
|
538 |
newMValueBeforePoint=vertices.get(pointBeforePos).getCoordinateAt(MDIMENSION); |
|
539 |
Double newMValueAfterPoint=vertices.get(pointAfterPos).getCoordinateAt(MDIMENSION); |
|
540 |
|
|
541 |
oldMValueBeforePoint=oldMValues.get(pointBeforePos); |
|
542 |
Double oldMValueAfterPoint=oldMValues.get(pointAfterPos); |
|
543 |
|
|
544 |
Double distanceBetweenFixedPoints=newMValueAfterPoint-newMValueBeforePoint; |
|
545 |
Double distanceBetweenOldFixedPoints=oldMValueAfterPoint-oldMValueBeforePoint; |
|
546 |
|
|
547 |
calibrationRatio=distanceBetweenOldFixedPoints/distanceBetweenFixedPoints; |
|
548 |
}else{ |
|
549 |
Double valueToRecalculate=vertices.get(i).getCoordinateAt(MDIMENSION); |
|
550 |
Double distanceBetween=valueToRecalculate-oldMValueBeforePoint; |
|
551 |
Double mValue=newMValueBeforePoint+(distanceBetween/calibrationRatio); |
|
552 |
refreshMValueGeometryVertex(i,mValue); |
|
553 |
} |
|
554 |
} |
|
555 |
} |
|
556 |
|
|
557 |
|
|
558 |
public void extrapolateBeforeCalibrationPoints(){ |
|
559 |
int nextFixedPointPos=getNextPosFixedPoint(minMFixedPointPos); |
|
560 |
Double newMValueFirstPoint=vertices.get(minMFixedPointPos).getCoordinateAt(MDIMENSION); |
|
561 |
Double newMValueSecondPoint=vertices.get(nextFixedPointPos).getCoordinateAt(MDIMENSION); |
|
562 |
|
|
563 |
Double oldMValueFirstPoint=oldMValues.get(minMFixedPointPos); |
|
564 |
Double oldMValueSecondPoint=oldMValues.get(nextFixedPointPos); |
|
565 |
|
|
566 |
Double distanceBetweenFixedPoints=newMValueSecondPoint-newMValueFirstPoint; |
|
567 |
Double distanceBetweenOldFixedPoints=oldMValueSecondPoint-oldMValueFirstPoint; |
|
568 |
|
|
569 |
Double calibrationRatio=distanceBetweenOldFixedPoints/distanceBetweenFixedPoints; |
|
570 |
for (int i=0; i<minMFixedPointPos;i++){ |
|
571 |
Double valueToRecalculate=vertices.get(i).getCoordinateAt(MDIMENSION); |
|
572 |
Double distanceBetween=oldMValueFirstPoint-valueToRecalculate; |
|
573 |
Double mValue=newMValueFirstPoint-(distanceBetween/calibrationRatio); |
|
574 |
refreshMValueGeometryVertex(i,mValue); |
|
575 |
} |
|
576 |
} |
|
577 |
|
|
578 |
public void extrapolateAfterCalibrationPoints(){ |
|
579 |
int prevFixedPointPos=getPreviousPosFixedPoint(maxMFixedPointPos); |
|
580 |
Double newMValueLastPoint=vertices.get(maxMFixedPointPos).getCoordinateAt(MDIMENSION); |
|
581 |
Double newMValuePrevPoint=vertices.get(prevFixedPointPos).getCoordinateAt(MDIMENSION); |
|
582 |
|
|
583 |
Double oldMValueLastPoint=oldMValues.get(maxMFixedPointPos); |
|
584 |
Double oldMValuePrevPoint=oldMValues.get(prevFixedPointPos); |
|
585 |
|
|
586 |
Double distanceBetweenFixedPoints=newMValueLastPoint-newMValuePrevPoint; |
|
587 |
Double distanceBetweenOldFixedPoints=oldMValueLastPoint-oldMValuePrevPoint; |
|
588 |
|
|
589 |
Double calibrationRatio=distanceBetweenOldFixedPoints/distanceBetweenFixedPoints; |
|
590 |
for (int i=maxMFixedPointPos+1; i<vertices.size();i++){ |
|
591 |
Double valueToRecalculate=vertices.get(i).getCoordinateAt(MDIMENSION); |
|
592 |
Double distanceBetween=valueToRecalculate-oldMValueLastPoint; |
|
593 |
Double mValue=newMValueLastPoint+(distanceBetween/calibrationRatio); |
|
594 |
refreshMValueGeometryVertex(i,mValue); |
|
595 |
} |
|
596 |
} |
|
597 |
|
|
598 |
private Integer getNextPosFixedPoint(int pos){ |
|
599 |
for (int i=pos+1;i<isFixedPoint.size();i++){ |
|
600 |
if (isFixedPoint.get(i)){ |
|
601 |
if( !distances.get(pos).equals(distances.get(i)) ){ |
|
602 |
return i; |
|
603 |
} |
|
604 |
} |
|
605 |
} |
|
606 |
return null; |
|
607 |
} |
|
608 |
|
|
609 |
private Integer getPreviousPosFixedPoint(int pos){ |
|
610 |
for (int i=pos-1;i>=0;i--){ |
|
611 |
if (isFixedPoint.get(i)){ |
|
612 |
if( !distances.get(pos).equals(distances.get(i)) ){ |
|
613 |
return i; |
|
614 |
} |
|
615 |
} |
|
616 |
} |
|
617 |
return null; |
|
618 |
} |
|
619 |
|
|
620 |
private void refreshMValueGeometryVertex(int i, Double mValue){ |
|
621 |
List<Point> points=LrsAlgorithmUtils.extractPoints(route); |
|
622 |
vertices.get(i).setCoordinateAt(MDIMENSION, mValue); |
|
623 |
points.get(i).setCoordinateAt(MDIMENSION,mValue); |
|
624 |
} |
|
625 |
|
|
213 | 626 |
} |
214 | 627 |
|
628 |
|
|
215 | 629 |
} |
216 | 630 |
|
217 | 631 |
|
Also available in: Unified diff