Revision 17 org.gvsig.lrs/trunk/org.gvsig.lrs/org.gvsig.lrs.lib/org.gvsig.lrs.lib.impl/src/main/java/org/gvsig/lrs/lib/impl/LrsCreateRouteAlgorithm.java
LrsCreateRouteAlgorithm.java | ||
---|---|---|
22 | 22 |
*/ |
23 | 23 |
package org.gvsig.lrs.lib.impl; |
24 | 24 |
|
25 |
import java.awt.Dimension; |
|
26 |
import java.awt.geom.Point2D; |
|
25 | 27 |
import java.io.File; |
28 |
import java.sql.Array; |
|
26 | 29 |
import java.util.ArrayList; |
30 |
import java.util.Arrays; |
|
31 |
import java.util.Comparator; |
|
27 | 32 |
import java.util.HashMap; |
28 | 33 |
import java.util.Iterator; |
29 | 34 |
import java.util.List; |
30 | 35 |
import java.util.Map; |
31 | 36 |
import java.util.Map.Entry; |
32 | 37 |
|
38 |
import javax.xml.transform.Source; |
|
39 |
|
|
40 |
import org.apache.commons.lang3.mutable.MutableDouble; |
|
33 | 41 |
import org.gvsig.fmap.dal.DALLocator; |
34 | 42 |
import org.gvsig.fmap.dal.DataManager; |
35 | 43 |
import org.gvsig.fmap.dal.DataServerExplorer; |
... | ... | |
49 | 57 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
50 | 58 |
import org.gvsig.fmap.dal.store.shp.SHPNewStoreParameters; |
51 | 59 |
import org.gvsig.fmap.geom.Geometry; |
60 |
import org.gvsig.fmap.geom.Geometry.DIMENSIONS; |
|
61 |
import org.gvsig.fmap.geom.GeometryException; |
|
52 | 62 |
import org.gvsig.fmap.geom.GeometryLocator; |
53 | 63 |
import org.gvsig.fmap.geom.GeometryManager; |
54 | 64 |
import org.gvsig.fmap.geom.aggregate.MultiLine; |
... | ... | |
123 | 133 |
NewFeatureStoreParameters newFeatureStoreParameters = parameters.getNewFeatureStoreParameters(); |
124 | 134 |
FeatureStore sourceFeatureStore = parameters.getSourceFeatureStore(); |
125 | 135 |
FeatureAttributeDescriptor idRouteField = parameters.getIdRouteField(); |
126 |
LrsCoordinatesPriority coordinatePriority = parameters.getCoordinatePriority(); |
|
127 |
LrsSourceOfMeasures sourceOfMeasures = parameters.getSourceOfMeasures(); |
|
136 |
//LrsCoordinatesPriority coordinatePriority = parameters.getCoordinatePriority();
|
|
137 |
//LrsSourceOfMeasures sourceOfMeasures = parameters.getSourceOfMeasures();
|
|
128 | 138 |
FeatureAttributeDescriptor fromMeasureField = parameters.getFromMeasureField(); |
129 | 139 |
FeatureAttributeDescriptor toMeasureField = parameters.getToMeasureField(); |
130 |
double measureFactor = parameters.getMeasureFactor(); |
|
131 |
double measureOffset = parameters.getMeasureOffset(); |
|
132 |
boolean ignoreSpatialGaps = parameters.ignoreSpatialGaps(); |
|
140 |
//double measureFactor = parameters.getMeasureFactor();
|
|
141 |
//double measureOffset = parameters.getMeasureOffset();
|
|
142 |
//boolean ignoreSpatialGaps = parameters.ignoreSpatialGaps();
|
|
133 | 143 |
|
134 | 144 |
logger.info(parameters.toString()); |
135 | 145 |
|
... | ... | |
195 | 205 |
} |
196 | 206 |
}); |
197 | 207 |
|
198 |
taskStatus.setRangeOfValues(0, featuresMap.size()-1);
|
|
208 |
taskStatus.setRangeOfValues(0, featuresMap.size()); |
|
199 | 209 |
int taskCount=0; |
200 | 210 |
|
201 | 211 |
newFeatureStore.edit(FeatureStore.MODE_FULLEDIT); |
212 |
|
|
213 |
//TODO Added to facilitate Tests. Remove afterwards |
|
214 |
StringBuffer resultDebug=new StringBuffer(); |
|
215 |
LrsSourceOfMeasures sourceOfMeasures = parameters.getSourceOfMeasures(); |
|
216 |
boolean ignoreSpatialGaps = parameters.ignoreSpatialGaps(); |
|
217 |
LrsCoordinatesPriority coordinatePriority = parameters.getCoordinatePriority(); |
|
218 |
|
|
219 |
resultDebug.append (sourceOfMeasures.getName()); |
|
220 |
if (!sourceOfMeasures.equals(LrsSourceOfMeasures.TWO_FIELDS)){ |
|
221 |
if (ignoreSpatialGaps)resultDebug.append (" WITHOUT GAPS "); |
|
222 |
else resultDebug.append (" WITH GAPS "); |
|
223 |
resultDebug.append(coordinatePriority.getName()); |
|
224 |
} |
|
225 |
//END of Test addition |
|
226 |
|
|
202 | 227 |
for(Entry<String, List<MStructure>> entry : featuresMap.entrySet()) { |
203 | 228 |
String routeName = entry.getKey(); |
204 | 229 |
List<MStructure> mList = entry.getValue(); |
... | ... | |
207 | 232 |
newFeature.set(routeFieldName, routeName); |
208 | 233 |
Geometry route = createGeometryRoute(mList); |
209 | 234 |
newFeature.setDefaultGeometry(route); |
235 |
|
|
236 |
|
|
237 |
//TODO Added to facilitate Tests. Remove afterwards |
|
238 |
String lineSeparator=System.getProperty("line.separator"); |
|
239 |
resultDebug.append(lineSeparator); |
|
240 |
resultDebug.append("Route: "+routeName); |
|
241 |
resultDebug.append(lineSeparator); |
|
242 |
if (route instanceof MultiLine){ |
|
243 |
MultiLine multiLine=(MultiLine)route; |
|
244 |
for (int j=0;j<multiLine.getPrimitivesNumber();j++){ |
|
245 |
Line line=(Line)multiLine.getPrimitiveAt(j); |
|
246 |
resultDebug.append("Line "+j+":["); |
|
247 |
for (int i=0;i<line.getNumVertices();i++){ |
|
248 |
Point point=line.getVertex(i); |
|
249 |
Double x=point.getX(); |
|
250 |
Double y=point.getY(); |
|
251 |
Double m=point.getCoordinateAt(point.getDimension()-1); |
|
252 |
resultDebug.append("(x="+x.toString()+",y="+y.toString()+",m="+m.toString()+")"); |
|
253 |
} |
|
254 |
resultDebug.append("]"); |
|
255 |
resultDebug.append(lineSeparator); |
|
256 |
} |
|
257 |
} |
|
258 |
|
|
259 |
//END of Test addition |
|
260 |
|
|
261 |
|
|
210 | 262 |
newFeatureStore.update(newFeature); |
211 | 263 |
|
264 |
taskCount++; |
|
212 | 265 |
taskStatus.setCurValue(taskCount); |
213 |
taskCount++; |
|
266 |
|
|
214 | 267 |
} |
268 |
logger.debug(resultDebug.toString()); |
|
215 | 269 |
newFeatureStore.finishEditing(); |
216 | 270 |
|
217 | 271 |
} catch (Exception e1) { |
... | ... | |
219 | 273 |
throw new LrsCreateRouteException("Error creating routes", e1); |
220 | 274 |
} |
221 | 275 |
|
276 |
|
|
222 | 277 |
taskStatus.terminate(); |
223 | 278 |
|
224 | 279 |
|
... | ... | |
262 | 317 |
// } |
263 | 318 |
} |
264 | 319 |
|
265 |
private Geometry createGeometryRoute(List<MStructure> mList) throws CreateGeometryException{ |
|
320 |
private Geometry createGeometryRoute(List<MStructure> mList) throws CreateGeometryException, GeometryOperationNotSupportedException, GeometryOperationException { |
|
321 |
LrsSourceOfMeasures sourceOfMeasures =parameters.getSourceOfMeasures(); |
|
322 |
Geometry geometryResult=null; |
|
323 |
switch (sourceOfMeasures) { |
|
324 |
case ONE_FIELD: |
|
325 |
geometryResult= calculateGeometryByOneField(mList); |
|
326 |
break; |
|
327 |
case TWO_FIELDS: |
|
328 |
geometryResult= calculateGeometryByTwoField(mList); |
|
329 |
break; |
|
330 |
case LENGTH: |
|
331 |
default: |
|
332 |
geometryResult= calculateGeometryByLength(mList); |
|
333 |
break; |
|
334 |
} |
|
335 |
geometryResult=applyOffset(geometryResult); |
|
336 |
return geometryResult; |
|
337 |
} |
|
338 |
|
|
339 |
private Geometry calculateGeometryByLength(List<MStructure> mList) throws CreateGeometryException, GeometryOperationNotSupportedException, GeometryOperationException{ |
|
340 |
return null; |
|
341 |
} |
|
342 |
|
|
343 |
private Geometry calculateGeometryByOneField(List<MStructure> mList) throws CreateGeometryException, GeometryOperationNotSupportedException, GeometryOperationException { |
|
344 |
boolean ignoreSpatialGaps = parameters.ignoreSpatialGaps(); |
|
266 | 345 |
GeometryManager geomanager= GeometryLocator.getGeometryManager(); |
267 |
//TODO Must be changed to Multiline and GEOM2DM |
|
268 |
MultiLine routeGeometry= (MultiLine)geomanager.create(Geometry.TYPES.MULTICURVE, Geometry.SUBTYPES.GEOM2D); |
|
346 |
try { |
|
347 |
sortMList(mList); |
|
348 |
} catch (Exception e) { |
|
349 |
logger.warn("Geometries couldn't be ordered"); |
|
350 |
} |
|
351 |
MultiLine mGeometry= (MultiLine)geomanager.create(Geometry.TYPES.MULTILINE, Geometry.SUBTYPES.GEOM2DM); |
|
352 |
MutableDouble lastMValue=new MutableDouble(0); |
|
353 |
Double gap=Double.valueOf(0); |
|
354 |
Point lastPoint=null; |
|
269 | 355 |
for (MStructure mStructure:mList){ |
270 | 356 |
Geometry geometry=mStructure.geometry; |
357 |
if(!ignoreSpatialGaps){ |
|
358 |
gap=calculateGap(lastPoint, geometry); |
|
359 |
lastMValue.add(gap); |
|
360 |
} |
|
361 |
Double fromDistance=lastMValue.getValue(); |
|
362 |
lastMValue.add(mStructure.fromField); |
|
363 |
Double toDistance=lastMValue.getValue(); |
|
364 |
|
|
271 | 365 |
if (geometry instanceof Line){ |
272 |
routeGeometry.addPrimitive((Line)geometry); |
|
366 |
Line line=(Line) geometry; |
|
367 |
Line lineM=lineToMLine(line, fromDistance, toDistance); |
|
368 |
mGeometry.addPrimitive(lineM); |
|
369 |
lastPoint=lineM.getVertex(lineM.getNumVertices()-1); |
|
273 | 370 |
} |
274 | 371 |
if (geometry instanceof MultiLine){ |
275 | 372 |
MultiLine multiLine =(MultiLine) geometry; |
276 |
for (int i=0;i<multiLine.getPrimitivesNumber();i++){ |
|
277 |
routeGeometry.addPrimitive(multiLine.getPrimitiveAt(i)); |
|
373 |
Line[] lines=multilineToMlines(multiLine, fromDistance, toDistance,ignoreSpatialGaps); |
|
374 |
for (Line lineM:lines){ |
|
375 |
mGeometry.addPrimitive(lineM); |
|
376 |
lastPoint=lineM.getVertex(lineM.getNumVertices()-1); |
|
278 | 377 |
} |
279 | 378 |
} |
280 | 379 |
} |
281 |
return routeGeometry;
|
|
380 |
return mGeometry;
|
|
282 | 381 |
} |
283 | 382 |
|
383 |
private Double calculateGap(Point lastPoint, Geometry geometry) throws GeometryOperationNotSupportedException, GeometryOperationException{ |
|
384 |
Double gap=new Double(0); |
|
385 |
if (geometry instanceof Line){ |
|
386 |
Line line=(Line) geometry; |
|
387 |
if (lastPoint!=null){ |
|
388 |
Point firstPoint=line.getVertex(0); |
|
389 |
gap=firstPoint.distance(lastPoint); |
|
390 |
} |
|
391 |
} |
|
392 |
if (geometry instanceof MultiLine){ |
|
393 |
MultiLine multiLine =(MultiLine) geometry; |
|
394 |
if (lastPoint!=null){ |
|
395 |
Line line=(Line) multiLine.getPrimitiveAt(0); |
|
396 |
Point firstPoint=line.getVertex(0); |
|
397 |
gap=firstPoint.distance(lastPoint); |
|
398 |
} |
|
399 |
} |
|
400 |
return gap; |
|
401 |
} |
|
284 | 402 |
|
403 |
private void sortMList(List<MStructure> mList) throws CreateEnvelopeException, GeometryOperationNotSupportedException, GeometryOperationException { |
|
404 |
LrsCoordinatesPriority coordinatePriority = parameters.getCoordinatePriority(); |
|
405 |
GeometryManager geomanager= GeometryLocator.getGeometryManager(); |
|
406 |
Envelope envelope = geomanager.createEnvelope(Geometry.SUBTYPES.GEOM2D); |
|
407 |
for (MStructure mStructure:mList){ |
|
408 |
envelope.add(mStructure.geometry.getEnvelope()); |
|
409 |
} |
|
410 |
Point origin=envelope.getLowerCorner(); |
|
411 |
switch (coordinatePriority) { |
|
412 |
case DOWN_LEFT: |
|
413 |
break; |
|
414 |
case DOWN_RIGHT: |
|
415 |
Double maxX =envelope.getMaximum(DIMENSIONS.X); |
|
416 |
origin.setX(maxX); |
|
417 |
break; |
|
418 |
case UP_LEFT: |
|
419 |
Double maxY =envelope.getMaximum(DIMENSIONS.Y); |
|
420 |
origin.setY(maxY); |
|
421 |
break; |
|
422 |
case UP_RIGHT: |
|
423 |
origin=envelope.getUpperCorner(); |
|
424 |
break; |
|
425 |
default: |
|
426 |
break; |
|
427 |
} |
|
428 |
GeometryDistanceToPointComparator comaparatorMList=new GeometryDistanceToPointComparator(origin); |
|
429 |
MStructure[] orderedMStruct=(MStructure[])mList.toArray(); |
|
430 |
Arrays.sort(orderedMStruct, comaparatorMList); |
|
431 |
mList=new ArrayList<MStructure>(Arrays.asList(orderedMStruct)); |
|
432 |
|
|
433 |
for (MStructure mStructure:mList){ |
|
434 |
Geometry geometry=mStructure.geometry; |
|
435 |
if (geometry instanceof Line){ |
|
436 |
Line line=(Line)geometry; |
|
437 |
Double firstPointDistance=origin.distance(line.getVertex(0)); |
|
438 |
Double lastPointDistance=origin.distance(line.getVertex(line.getNumVertices()-1)); |
|
439 |
if (lastPointDistance<firstPointDistance){ |
|
440 |
geometry.flip(); |
|
441 |
} |
|
442 |
} |
|
443 |
if (geometry instanceof MultiLine){ |
|
444 |
MultiLine multiLine=(MultiLine)geometry; |
|
445 |
Line firstLine=(Line)multiLine.getPrimitiveAt(0); |
|
446 |
Line lastLine=(Line)multiLine.getPrimitiveAt(multiLine.getPrimitivesNumber()-1); |
|
447 |
Double firstPointDistance=origin.distance(firstLine.getVertex(0)); |
|
448 |
Double lastPointDistance=origin.distance(lastLine.getVertex(lastLine.getNumVertices()-1)); |
|
449 |
if (lastPointDistance<firstPointDistance){ |
|
450 |
geometry.flip(); |
|
451 |
} |
|
452 |
} |
|
453 |
mStructure.geometry=geometry; |
|
454 |
} |
|
455 |
} |
|
456 |
|
|
457 |
private Geometry applyOffset(Geometry geometry){ |
|
458 |
double measureFactor = parameters.getMeasureFactor(); |
|
459 |
double measureOffset = parameters.getMeasureOffset(); |
|
460 |
if (geometry instanceof Line){ |
|
461 |
Line line=(Line) geometry; |
|
462 |
for (int i=0;i<line.getNumVertices();i++){ |
|
463 |
Point mVertex=line.getVertex(i); |
|
464 |
Double mValue=mVertex.getCoordinateAt(mVertex.getDimension()-1); |
|
465 |
Double newMValue=(mValue*measureFactor)+measureOffset; |
|
466 |
mVertex.setCoordinateAt(mVertex.getDimension()-1, newMValue); |
|
467 |
line.setVertex(i, mVertex); |
|
468 |
} |
|
469 |
} |
|
470 |
if (geometry instanceof MultiLine){ |
|
471 |
MultiLine multiLine =(MultiLine) geometry; |
|
472 |
for (int i=0;i<multiLine.getPrimitivesNumber();i++){ |
|
473 |
Line line=(Line) multiLine.getPrimitiveAt(i); |
|
474 |
for (int j=0;j<line.getNumVertices();j++){ |
|
475 |
Point mVertex=line.getVertex(j); |
|
476 |
Double mValue=mVertex.getCoordinateAt(mVertex.getDimension()-1); |
|
477 |
Double newMValue=(mValue*measureFactor)+measureOffset; |
|
478 |
mVertex.setCoordinateAt(mVertex.getDimension()-1, newMValue); |
|
479 |
line.setVertex(j, mVertex); |
|
480 |
} |
|
481 |
} |
|
482 |
} |
|
483 |
return geometry; |
|
484 |
} |
|
485 |
|
|
486 |
private Geometry calculateGeometryByTwoField(List<MStructure> mList) throws CreateGeometryException, GeometryOperationNotSupportedException, GeometryOperationException{ |
|
487 |
GeometryManager geomanager= GeometryLocator.getGeometryManager(); |
|
488 |
MultiLine mGeometry= (MultiLine)geomanager.create(Geometry.TYPES.MULTILINE, Geometry.SUBTYPES.GEOM2DM); |
|
489 |
for (MStructure mStructure:mList){ |
|
490 |
Geometry geometry=mStructure.geometry; |
|
491 |
Double fromDistance=mStructure.fromField; |
|
492 |
Double toDistance=mStructure.toField; |
|
493 |
if (geometry instanceof Line){ |
|
494 |
Line line=(Line) geometry; |
|
495 |
Line lineM=lineToMLine(line, fromDistance, toDistance); |
|
496 |
mGeometry.addPrimitive(lineM); |
|
497 |
} |
|
498 |
if (geometry instanceof MultiLine){ |
|
499 |
MultiLine multiLine =(MultiLine) geometry; |
|
500 |
Line[] lines=multilineToMlines(multiLine, fromDistance, toDistance); |
|
501 |
for (Line lineM:lines){ |
|
502 |
mGeometry.addPrimitive(lineM); |
|
503 |
} |
|
504 |
} |
|
505 |
} |
|
506 |
|
|
507 |
return mGeometry; |
|
508 |
} |
|
509 |
private Line[] multilineToMlines(MultiLine multiLine,Double fromDistance, Double toDistance) throws GeometryOperationNotSupportedException, GeometryOperationException, CreateGeometryException{ |
|
510 |
return multilineToMlines(multiLine,fromDistance, toDistance,true); |
|
511 |
} |
|
512 |
private Line[] multilineToMlines(MultiLine multiLine,Double fromDistance, Double toDistance,boolean ignoreSpatialGaps) throws GeometryOperationNotSupportedException, GeometryOperationException, CreateGeometryException{ |
|
513 |
|
|
514 |
Line[] lines=new Line[multiLine.getPrimitivesNumber()]; |
|
515 |
Double multiLineLength=getMultiLineLength(multiLine,ignoreSpatialGaps); |
|
516 |
Double mFirstPoint=fromDistance; |
|
517 |
Double mLastPoint=null; |
|
518 |
Double distance=Double.valueOf(0); |
|
519 |
Point lastPoint=null; |
|
520 |
for (int i=0;i<multiLine.getPrimitivesNumber();i++){ |
|
521 |
|
|
522 |
Line line=(Line) multiLine.getPrimitiveAt(i); |
|
523 |
Double lineLength=getLineLength(line); |
|
524 |
Double gap=Double.valueOf(0); |
|
525 |
|
|
526 |
if (!ignoreSpatialGaps&&lastPoint!=null){ |
|
527 |
Point firstPoint=line.getVertex(0); |
|
528 |
gap=firstPoint.distance(lastPoint); |
|
529 |
distance+=gap; |
|
530 |
} |
|
531 |
mFirstPoint=calculateM(toDistance,fromDistance, distance, multiLineLength); |
|
532 |
distance+=lineLength; |
|
533 |
mLastPoint=calculateM(toDistance,fromDistance, distance, multiLineLength); |
|
534 |
lines[i]=lineToMLine(line, mFirstPoint, mLastPoint); |
|
535 |
lastPoint=lines[i].getVertex(line.getNumVertices()-1); |
|
536 |
} |
|
537 |
return lines; |
|
538 |
} |
|
539 |
|
|
540 |
private Line lineToMLine(Line line,Double minMValue, Double maxMValue) throws CreateGeometryException, GeometryOperationNotSupportedException, GeometryOperationException{ |
|
541 |
GeometryManager geomanager= GeometryLocator.getGeometryManager(); |
|
542 |
Double lineLength=getLineLength(line); |
|
543 |
Line lineM=(Line)geomanager.create(Geometry.TYPES.LINE, Geometry.SUBTYPES.GEOM2DM); |
|
544 |
Double inLineDistance=Double.valueOf(0); |
|
545 |
for (int i=0;i<line.getNumVertices();i++){ |
|
546 |
Point vertex=line.getVertex(i); |
|
547 |
Point mVertex=(Point)geomanager.create(Geometry.TYPES.POINT, Geometry.SUBTYPES.GEOM2DM); |
|
548 |
mVertex.setX(vertex.getX()); |
|
549 |
mVertex.setY(vertex.getY()); |
|
550 |
|
|
551 |
Double mValue; |
|
552 |
if (i==0)mValue=minMValue; |
|
553 |
else if (i==line.getNumVertices()-1) mValue=maxMValue; |
|
554 |
else{ |
|
555 |
Point previousVertex=line.getVertex(i-1); |
|
556 |
inLineDistance+=vertex.distance(previousVertex); |
|
557 |
mValue=calculateM(maxMValue,minMValue, inLineDistance, lineLength); |
|
558 |
} |
|
559 |
|
|
560 |
mVertex.setCoordinateAt(mVertex.getDimension()-1, mValue); |
|
561 |
lineM.addVertex(mVertex); |
|
562 |
} |
|
563 |
return lineM; |
|
564 |
} |
|
565 |
|
|
566 |
private Double calculateM(Double maxValue, Double minValue, Double relativeDistance, Double totalLength){ |
|
567 |
//mValue=((newMax-newMin)*(mCoordinate-oldMin)/(oldMax-oldMin))+newMin; |
|
568 |
if (totalLength.equals(Double.valueOf(0) )) return Double.POSITIVE_INFINITY; |
|
569 |
return ((maxValue-minValue)*(relativeDistance)/(totalLength))+minValue; |
|
570 |
} |
|
571 |
|
|
572 |
// private Line lineToMLine(Line line,Point previousVertex, MutableDouble distance) throws CreateGeometryException{ |
|
573 |
// GeometryManager geomanager= GeometryLocator.getGeometryManager(); |
|
574 |
// Line lineM=(Line)geomanager.create(Geometry.TYPES.LINE, Geometry.SUBTYPES.GEOM2DM); |
|
575 |
// for (int i=0;i<line.getNumVertices();i++){ |
|
576 |
// Point vertex=line.getVertex(i); |
|
577 |
// Point mVertex=(Point)geomanager.create(Geometry.TYPES.POINT, Geometry.SUBTYPES.GEOM2DM); |
|
578 |
// mVertex.setX(vertex.getX()); |
|
579 |
// mVertex.setY(vertex.getY()); |
|
580 |
// |
|
581 |
// if (previousVertex==null){ |
|
582 |
// previousVertex=vertex; |
|
583 |
// }else{ |
|
584 |
// try { |
|
585 |
// distance.setValue(distance.getValue()+vertex.distance(previousVertex)); |
|
586 |
// previousVertex=vertex; |
|
587 |
// } catch (Exception e) { |
|
588 |
// previousVertex=vertex; |
|
589 |
// distance.setValue(Double.NaN); |
|
590 |
// } |
|
591 |
// } |
|
592 |
// mVertex.setCoordinateAt(mVertex.getDimension()-1, distance.getValue()); |
|
593 |
// lineM.addVertex(mVertex); |
|
594 |
// } |
|
595 |
// return lineM; |
|
596 |
// } |
|
597 |
|
|
598 |
|
|
285 | 599 |
/** |
286 | 600 |
* @param route |
287 | 601 |
* @return |
... | ... | |
310 | 624 |
return refs; |
311 | 625 |
} |
312 | 626 |
|
313 |
private MultiLine createRoute(List<FeatureReference> references, LrsCoordinatesPriority coordinatePriority, LrsSourceOfMeasures sourceOfMeasures, FeatureAttributeDescriptor fromMeasureField,FeatureAttributeDescriptor toMeasureField, double measureFactor, double measureOffset, boolean ignoreSpatialGaps) throws CreateGeometryException, DataException, CreateEnvelopeException, GeometryOperationNotSupportedException, GeometryOperationException{ |
|
627 |
// private MultiLine createRoute(List<FeatureReference> references, LrsCoordinatesPriority coordinatePriority, LrsSourceOfMeasures sourceOfMeasures, FeatureAttributeDescriptor fromMeasureField,FeatureAttributeDescriptor toMeasureField, double measureFactor, double measureOffset, boolean ignoreSpatialGaps) throws CreateGeometryException, DataException, CreateEnvelopeException, GeometryOperationNotSupportedException, GeometryOperationException{ |
|
628 |
// |
|
629 |
// Envelope envelope = getEnvelope(references); |
|
630 |
// Point p = getOriginPointAccordingPriorityCoordinates(envelope, coordinatePriority); |
|
631 |
// GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
|
632 |
// |
|
633 |
// //FIXME: cuando est? esto tendr? que ser GEOM2DM o incluso GEOM3DM |
|
634 |
// MultiLine route = geometryManager.createMultiLine(Geometry.SUBTYPES.GEOM2D); |
|
635 |
// double m = 0; |
|
636 |
// Point previousVertex = null; |
|
637 |
// List<FeatureReference> orderedReferences = getOrderedReferences(references, (Point)p.cloneGeometry()); |
|
638 |
// for (Iterator iterator = orderedReferences.iterator(); iterator.hasNext();) { |
|
639 |
// FeatureReference featureReference = (FeatureReference) iterator.next(); |
|
640 |
// Feature feature = featureReference.getFeature(); |
|
641 |
// Geometry geometry = feature.getDefaultGeometry(); |
|
642 |
// if (geometry instanceof Line) { |
|
643 |
// Line line = (Line) geometry; |
|
644 |
// Point firstVertex = line.getVertex(0); |
|
645 |
// Point lastVertex = line.getVertex(line.getNumVertices() - 1); |
|
646 |
// boolean ascendantOrder = isAscendantOrder(p, firstVertex, lastVertex); |
|
647 |
// //FIXME: cuando est? disponible esto deber? ser GEOM2DM (?o incluso GEOM3DM?) |
|
648 |
// Line tramo = geometryManager.createLine(Geometry.SUBTYPES.GEOM3D); |
|
649 |
// double lineLength = getLineLength(line); |
|
650 |
// for(int i=0; i<line.getNumVertices(); i++){ |
|
651 |
// Point vertex = getNextVertex(line, ascendantOrder, i); |
|
652 |
// |
|
653 |
// switch (sourceOfMeasures) { |
|
654 |
// case ONE_FIELD: |
|
655 |
// m = calculateMByOneField(m, previousVertex, i, vertex, feature.getDouble(fromMeasureField.getName()), lineLength, measureFactor, measureOffset, ignoreSpatialGaps); |
|
656 |
// break; |
|
657 |
// case TWO_FIELDS: |
|
658 |
// m = calculateMByTwoField(m, previousVertex, i, vertex, feature.getDouble(fromMeasureField.getName()), feature.getDouble(toMeasureField.getName()), lineLength, measureFactor, measureOffset); |
|
659 |
// break; |
|
660 |
// case LENGTH: |
|
661 |
// default: |
|
662 |
// m = calculateMByLength(m, previousVertex, i, vertex, ignoreSpatialGaps, measureFactor, measureOffset); |
|
663 |
// break; |
|
664 |
// } |
|
665 |
// // FIXME: cuando est? disponible esto deber? ser GEOM2DM (?o incluso GEOM3DM?) |
|
666 |
// Point mvertex = geometryManager.createPoint(vertex.getX(), vertex.getY(), Geometry.SUBTYPES.GEOM3D); |
|
667 |
// mvertex.setCoordinateAt(2, m); |
|
668 |
// tramo.addVertex(mvertex); |
|
669 |
// previousVertex = vertex; |
|
670 |
// p = vertex; |
|
671 |
// } |
|
672 |
// route.addPrimitive(tramo); |
|
673 |
// } else if (geometry instanceof MultiLine) { |
|
674 |
// MultiLine multiLine = (MultiLine)geometry; |
|
675 |
// double multiLineLength = getMultiLineLength(multiLine); |
|
676 |
// MultiLine orderedMultiLine = getOrderedMultiLine(multiLine, previousVertex, coordinatePriority); |
|
677 |
// for (int j = 0; j < orderedMultiLine.getPrimitivesNumber(); j++) { |
|
678 |
// Line line = (Line) orderedMultiLine.getPrimitiveAt(j); |
|
679 |
// Point firstVertex = line.getVertex(0); |
|
680 |
// Point lastVertex = line.getVertex(line.getNumVertices() - 1); |
|
681 |
// boolean ascendantOrder = isAscendantOrder(p, firstVertex, lastVertex); |
|
682 |
// //FIXME: cuando est? disponible esto deber? ser GEOM2DM (?o incluso GEOM3DM?) |
|
683 |
// Line tramo = geometryManager.createLine(Geometry.SUBTYPES.GEOM3D); |
|
684 |
// for(int i=0; i<line.getNumVertices(); i++){ |
|
685 |
// Point vertex = getNextVertex(line, ascendantOrder, i); |
|
686 |
// switch (sourceOfMeasures) { |
|
687 |
// case ONE_FIELD: |
|
688 |
// m = calculateMByOneField(m, previousVertex, i, vertex, feature.getDouble(toMeasureField.getName()), multiLineLength, multiLineLength, multiLineLength, ascendantOrder); |
|
689 |
// break; |
|
690 |
// case TWO_FIELDS: |
|
691 |
// //Este caso tal vez no se d? nunca, pero por si acaso |
|
692 |
// m = calculateMByTwoField(m, previousVertex, i, vertex, feature.getDouble(fromMeasureField.getName()), feature.getDouble(toMeasureField.getName()), multiLineLength, measureFactor, measureOffset); |
|
693 |
// break; |
|
694 |
// case LENGTH: |
|
695 |
// default: |
|
696 |
// m = calculateMByLength(m, previousVertex, i, vertex, ignoreSpatialGaps, measureFactor, measureOffset); |
|
697 |
// break; |
|
698 |
// } |
|
699 |
// |
|
700 |
// // FIXME: cuando est? disponible esto deber? ser GEOM2DM (?o incluso GEOM3DM?) |
|
701 |
// Point mvertex = geometryManager.createPoint(vertex.getX(), vertex.getY(), Geometry.SUBTYPES.GEOM3D); |
|
702 |
// mvertex.setCoordinateAt(2, m); |
|
703 |
// tramo.addVertex(mvertex); |
|
704 |
// previousVertex = vertex; |
|
705 |
// p = vertex; |
|
706 |
// } |
|
707 |
// route.addPrimitive(tramo); |
|
708 |
// } |
|
709 |
// } |
|
710 |
// } |
|
711 |
// |
|
712 |
// return route; |
|
713 |
// } |
|
314 | 714 |
|
315 |
Envelope envelope = getEnvelope(references); |
|
316 |
Point p = getOriginPointAccordingPriorityCoordinates(envelope, coordinatePriority); |
|
317 |
GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
|
318 |
|
|
319 |
//FIXME: cuando est? esto tendr? que ser GEOM2DM o incluso GEOM3DM |
|
320 |
MultiLine route = geometryManager.createMultiLine(Geometry.SUBTYPES.GEOM2D); |
|
321 |
double m = 0; |
|
322 |
Point previousVertex = null; |
|
323 |
List<FeatureReference> orderedReferences = getOrderedReferences(references, (Point)p.cloneGeometry()); |
|
324 |
for (Iterator iterator = orderedReferences.iterator(); iterator.hasNext();) { |
|
325 |
FeatureReference featureReference = (FeatureReference) iterator.next(); |
|
326 |
Feature feature = featureReference.getFeature(); |
|
327 |
Geometry geometry = feature.getDefaultGeometry(); |
|
328 |
if (geometry instanceof Line) { |
|
329 |
Line line = (Line) geometry; |
|
330 |
Point firstVertex = line.getVertex(0); |
|
331 |
Point lastVertex = line.getVertex(line.getNumVertices() - 1); |
|
332 |
boolean ascendantOrder = isAscendantOrder(p, firstVertex, lastVertex); |
|
333 |
//FIXME: cuando est? disponible esto deber? ser GEOM2DM (?o incluso GEOM3DM?) |
|
334 |
Line tramo = geometryManager.createLine(Geometry.SUBTYPES.GEOM3D); |
|
335 |
double lineLength = getLineLength(line); |
|
336 |
for(int i=0; i<line.getNumVertices(); i++){ |
|
337 |
Point vertex = getNextVertex(line, ascendantOrder, i); |
|
338 |
|
|
339 |
switch (sourceOfMeasures) { |
|
340 |
case ONE_FIELD: |
|
341 |
m = calculateMByOneField(m, previousVertex, i, vertex, feature.getDouble(fromMeasureField.getName()), lineLength, measureFactor, measureOffset, ignoreSpatialGaps); |
|
342 |
break; |
|
343 |
case TWO_FIELDS: |
|
344 |
m = calculateMByTwoField(m, previousVertex, i, vertex, feature.getDouble(fromMeasureField.getName()), feature.getDouble(toMeasureField.getName()), lineLength, measureFactor, measureOffset); |
|
345 |
break; |
|
346 |
case LENGTH: |
|
347 |
default: |
|
348 |
m = calculateMByLength(m, previousVertex, i, vertex, ignoreSpatialGaps, measureFactor, measureOffset); |
|
349 |
break; |
|
350 |
} |
|
351 |
// FIXME: cuando est? disponible esto deber? ser GEOM2DM (?o incluso GEOM3DM?) |
|
352 |
Point mvertex = geometryManager.createPoint(vertex.getX(), vertex.getY(), Geometry.SUBTYPES.GEOM3D); |
|
353 |
mvertex.setCoordinateAt(2, m); |
|
354 |
tramo.addVertex(mvertex); |
|
355 |
previousVertex = vertex; |
|
356 |
p = vertex; |
|
357 |
} |
|
358 |
route.addPrimitive(tramo); |
|
359 |
} else if (geometry instanceof MultiLine) { |
|
360 |
MultiLine multiLine = (MultiLine)geometry; |
|
361 |
double multiLineLength = getMultiLineLength(multiLine); |
|
362 |
MultiLine orderedMultiLine = getOrderedMultiLine(multiLine, previousVertex, coordinatePriority); |
|
363 |
for (int j = 0; j < orderedMultiLine.getPrimitivesNumber(); j++) { |
|
364 |
Line line = (Line) orderedMultiLine.getPrimitiveAt(j); |
|
365 |
Point firstVertex = line.getVertex(0); |
|
366 |
Point lastVertex = line.getVertex(line.getNumVertices() - 1); |
|
367 |
boolean ascendantOrder = isAscendantOrder(p, firstVertex, lastVertex); |
|
368 |
//FIXME: cuando est? disponible esto deber? ser GEOM2DM (?o incluso GEOM3DM?) |
|
369 |
Line tramo = geometryManager.createLine(Geometry.SUBTYPES.GEOM3D); |
|
370 |
for(int i=0; i<line.getNumVertices(); i++){ |
|
371 |
Point vertex = getNextVertex(line, ascendantOrder, i); |
|
372 |
switch (sourceOfMeasures) { |
|
373 |
case ONE_FIELD: |
|
374 |
m = calculateMByOneField(m, previousVertex, i, vertex, feature.getDouble(toMeasureField.getName()), multiLineLength, multiLineLength, multiLineLength, ascendantOrder); |
|
375 |
break; |
|
376 |
case TWO_FIELDS: |
|
377 |
//Este caso tal vez no se d? nunca, pero por si acaso |
|
378 |
m = calculateMByTwoField(m, previousVertex, i, vertex, feature.getDouble(fromMeasureField.getName()), feature.getDouble(toMeasureField.getName()), multiLineLength, measureFactor, measureOffset); |
|
379 |
break; |
|
380 |
case LENGTH: |
|
381 |
default: |
|
382 |
m = calculateMByLength(m, previousVertex, i, vertex, ignoreSpatialGaps, measureFactor, measureOffset); |
|
383 |
break; |
|
384 |
} |
|
385 |
|
|
386 |
// FIXME: cuando est? disponible esto deber? ser GEOM2DM (?o incluso GEOM3DM?) |
|
387 |
Point mvertex = geometryManager.createPoint(vertex.getX(), vertex.getY(), Geometry.SUBTYPES.GEOM3D); |
|
388 |
mvertex.setCoordinateAt(2, m); |
|
389 |
tramo.addVertex(mvertex); |
|
390 |
previousVertex = vertex; |
|
391 |
p = vertex; |
|
392 |
} |
|
393 |
route.addPrimitive(tramo); |
|
394 |
} |
|
395 |
} |
|
396 |
} |
|
397 |
|
|
398 |
return route; |
|
399 |
} |
|
400 |
|
|
401 | 715 |
/** |
402 | 716 |
* @param multiLine |
403 | 717 |
* @param previousVertex |
... | ... | |
517 | 831 |
* @throws GeometryOperationException |
518 | 832 |
* @throws GeometryOperationNotSupportedException |
519 | 833 |
*/ |
520 |
private double getMultiLineLength(MultiLine multiLine) throws GeometryOperationNotSupportedException, GeometryOperationException{ |
|
834 |
private double getMultiLineLength(MultiLine multiLine,boolean ignoreSpatialGaps) throws GeometryOperationNotSupportedException, GeometryOperationException{
|
|
521 | 835 |
double lenght = 0; |
836 |
Point previousVertex = null; |
|
522 | 837 |
for (int j = 0; j < multiLine.getPrimitivesNumber(); j++) { |
523 | 838 |
Line line = (Line) multiLine.getPrimitiveAt(j); |
524 |
Point previousVertex = null; |
|
839 |
if (ignoreSpatialGaps){ |
|
840 |
previousVertex = null; |
|
841 |
} |
|
525 | 842 |
for(int i=0; i<line.getNumVertices(); i++){ |
526 | 843 |
Point vertex = line.getVertex(i); |
527 | 844 |
if(previousVertex!=null){ |
... | ... | |
691 | 1008 |
|
692 | 1009 |
} |
693 | 1010 |
|
694 |
class MStructure{ |
|
1011 |
class MStructure implements Comparable<MStructure>{
|
|
695 | 1012 |
Geometry geometry; |
696 | 1013 |
Double fromField; |
697 | 1014 |
Double toField; |
1015 |
|
|
1016 |
public int compareTo(MStructure o) { |
|
1017 |
// TODO Auto-generated method stub |
|
1018 |
return 0; |
|
1019 |
} |
|
698 | 1020 |
} |
699 | 1021 |
|
1022 |
|
|
1023 |
class GeometryDistanceToPointComparator implements Comparator<MStructure> { |
|
1024 |
|
|
1025 |
private Point referencePoint; |
|
1026 |
|
|
1027 |
public GeometryDistanceToPointComparator(Point referencePoint) { |
|
1028 |
this.referencePoint = referencePoint; |
|
1029 |
} |
|
1030 |
|
|
1031 |
public int compare(MStructure mStruct1, MStructure mStruct2) { |
|
1032 |
Double distance1=nearestPointDistance(mStruct1.geometry); |
|
1033 |
Double distance2=nearestPointDistance(mStruct2.geometry); |
|
1034 |
return distance1.compareTo(distance2); |
|
1035 |
} |
|
1036 |
|
|
1037 |
private Double nearestPointDistance(Geometry geometry){ |
|
1038 |
if (geometry instanceof Line){ |
|
1039 |
return nearestPointDistanceToLine((Line)geometry); |
|
1040 |
} |
|
1041 |
if (geometry instanceof MultiLine){ |
|
1042 |
MultiLine multiLine=(MultiLine)geometry; |
|
1043 |
Line firstLine=(Line)multiLine.getPrimitiveAt(0); |
|
1044 |
Line lastLine=(Line)multiLine.getPrimitiveAt(multiLine.getPrimitivesNumber()-1); |
|
1045 |
|
|
1046 |
Double distanceFirstPoint; |
|
1047 |
Double distanceLastPoint; |
|
1048 |
try { |
|
1049 |
distanceFirstPoint = referencePoint.distance(firstLine.getVertex(0)); |
|
1050 |
} catch (Exception e) { |
|
1051 |
distanceFirstPoint=Double.MAX_VALUE; |
|
1052 |
} |
|
1053 |
try { |
|
1054 |
distanceLastPoint=referencePoint.distance(lastLine.getVertex(lastLine.getNumVertices()-1)); |
|
1055 |
} catch (Exception e) { |
|
1056 |
distanceLastPoint=Double.MAX_VALUE; |
|
1057 |
} |
|
1058 |
|
|
1059 |
if (distanceFirstPoint<distanceLastPoint) return distanceFirstPoint; |
|
1060 |
else return distanceLastPoint; |
|
1061 |
} |
|
1062 |
return Double.MAX_VALUE; |
|
1063 |
} |
|
1064 |
|
|
1065 |
private Double nearestPointDistanceToLine(Line line){ |
|
1066 |
Double distanceFirstPoint; |
|
1067 |
Double distanceLastPoint; |
|
1068 |
try { |
|
1069 |
distanceFirstPoint = referencePoint.distance(line.getVertex(0)); |
|
1070 |
} catch (Exception e) { |
|
1071 |
distanceFirstPoint=Double.MAX_VALUE; |
|
1072 |
} |
|
1073 |
try { |
|
1074 |
distanceLastPoint=referencePoint.distance(line.getVertex(line.getNumVertices()-1)); |
|
1075 |
} catch (Exception e) { |
|
1076 |
distanceLastPoint=Double.MAX_VALUE; |
|
1077 |
} |
|
1078 |
|
|
1079 |
if (distanceFirstPoint<distanceLastPoint) return distanceFirstPoint; |
|
1080 |
else return distanceLastPoint; |
|
1081 |
} |
|
1082 |
|
|
1083 |
} |
|
1084 |
|
|
700 | 1085 |
} |
701 | 1086 |
|
702 | 1087 |
|
Also available in: Unified diff