| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
package org.locationtech.jts.edgegraph; |
| 14 |
|
| 15 |
import java.util.Collection; |
| 16 |
import java.util.Iterator; |
| 17 |
|
| 18 |
import org.locationtech.jts.geom.CoordinateSequence; |
| 19 |
import org.locationtech.jts.geom.Geometry; |
| 20 |
import org.locationtech.jts.geom.GeometryComponentFilter; |
| 21 |
import org.locationtech.jts.geom.LineString; |
| 22 |
|
| 23 |
|
| 24 |
/** |
| 25 |
* Builds an edge graph from geometries containing edges. |
| 26 |
* |
| 27 |
* @author mdavis |
| 28 |
* |
| 29 |
*/ |
| 30 |
public class EdgeGraphBuilder |
| 31 |
{ |
| 32 |
public static EdgeGraph build(Collection geoms) { |
| 33 |
EdgeGraphBuilder builder = new EdgeGraphBuilder(); |
| 34 |
builder.add(geoms); |
| 35 |
return builder.getGraph(); |
| 36 |
} |
| 37 |
|
| 38 |
private EdgeGraph graph = new EdgeGraph(); |
| 39 |
|
| 40 |
public EdgeGraphBuilder() |
| 41 |
{ |
| 42 |
|
| 43 |
} |
| 44 |
|
| 45 |
public EdgeGraph getGraph() |
| 46 |
{ |
| 47 |
return graph; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Adds the edges of a Geometry to the graph. |
| 52 |
* May be called multiple times. |
| 53 |
* Any dimension of Geometry may be added; the constituent edges are |
| 54 |
* extracted. |
| 55 |
* |
| 56 |
* @param geometry geometry to be added |
| 57 |
*/ |
| 58 |
public void add(Geometry geometry) { |
| 59 |
geometry.apply(new GeometryComponentFilter() { |
| 60 |
public void filter(Geometry component) { |
| 61 |
if (component instanceof LineString) { |
| 62 |
add((LineString)component); |
| 63 |
} |
| 64 |
} |
| 65 |
}); |
| 66 |
} |
| 67 |
/** |
| 68 |
* Adds the edges in a collection of {@link Geometry}s to the graph. |
| 69 |
* May be called multiple times. |
| 70 |
* Any dimension of Geometry may be added. |
| 71 |
* |
| 72 |
* @param geometries the geometries to be added |
| 73 |
*/ |
| 74 |
public void add(Collection geometries) |
| 75 |
{ |
| 76 |
for (Iterator i = geometries.iterator(); i.hasNext(); ) { |
| 77 |
Geometry geometry = (Geometry) i.next(); |
| 78 |
add(geometry); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
private void add(LineString lineString) { |
| 83 |
CoordinateSequence seq = lineString.getCoordinateSequence(); |
| 84 |
for (int i = 1; i < seq.size(); i++) { |
| 85 |
graph.addEdge(seq.getCoordinate(i-1), seq.getCoordinate(i)); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
} |
| 91 |
|