| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
package org.locationtech.jts.operation.linemerge; |
| 14 |
|
| 15 |
|
| 16 |
import java.util.ArrayList; |
| 17 |
import java.util.Iterator; |
| 18 |
import java.util.List; |
| 19 |
|
| 20 |
import org.locationtech.jts.geom.Coordinate; |
| 21 |
import org.locationtech.jts.geom.CoordinateArrays; |
| 22 |
import org.locationtech.jts.geom.CoordinateList; |
| 23 |
import org.locationtech.jts.geom.GeometryFactory; |
| 24 |
import org.locationtech.jts.geom.LineString; |
| 25 |
|
| 26 |
/** |
| 27 |
* A sequence of {@link LineMergeDirectedEdge}s forming one of the lines that will |
| 28 |
* be output by the line-merging process. |
| 29 |
* |
| 30 |
* @version 1.7 |
| 31 |
*/ |
| 32 |
public class EdgeString { |
| 33 |
private GeometryFactory factory; |
| 34 |
private List directedEdges = new ArrayList(); |
| 35 |
private Coordinate[] coordinates = null; |
| 36 |
/** |
| 37 |
* Constructs an EdgeString with the given factory used to convert this EdgeString |
| 38 |
* to a LineString |
| 39 |
*/ |
| 40 |
public EdgeString(GeometryFactory factory) { |
| 41 |
this.factory = factory; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Adds a directed edge which is known to form part of this line. |
| 46 |
*/ |
| 47 |
public void add(LineMergeDirectedEdge directedEdge) { |
| 48 |
directedEdges.add(directedEdge); |
| 49 |
} |
| 50 |
|
| 51 |
private Coordinate[] getCoordinates() { |
| 52 |
if (coordinates == null) { |
| 53 |
int forwardDirectedEdges = 0; |
| 54 |
int reverseDirectedEdges = 0; |
| 55 |
CoordinateList coordinateList = new CoordinateList(); |
| 56 |
for (Iterator i = directedEdges.iterator(); i.hasNext();) { |
| 57 |
LineMergeDirectedEdge directedEdge = (LineMergeDirectedEdge) i.next(); |
| 58 |
if (directedEdge.getEdgeDirection()) { |
| 59 |
forwardDirectedEdges++; |
| 60 |
} |
| 61 |
else { |
| 62 |
reverseDirectedEdges++; |
| 63 |
} |
| 64 |
coordinateList.add(((LineMergeEdge) directedEdge.getEdge()).getLine() |
| 65 |
.getCoordinates(), false, |
| 66 |
directedEdge.getEdgeDirection()); |
| 67 |
} |
| 68 |
coordinates = coordinateList.toCoordinateArray(); |
| 69 |
if (reverseDirectedEdges > forwardDirectedEdges) { |
| 70 |
CoordinateArrays.reverse(coordinates); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
return coordinates; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Converts this EdgeString into a LineString. |
| 79 |
*/ |
| 80 |
public LineString toLineString() { |
| 81 |
return factory.createLineString(getCoordinates()); |
| 82 |
} |
| 83 |
} |
| 84 |
|