| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package org.locationtech.jts.geomgraph; |
| 16 |
|
| 17 |
|
| 18 |
import java.io.PrintStream; |
| 19 |
import java.util.ArrayList; |
| 20 |
import java.util.Collection; |
| 21 |
import java.util.Iterator; |
| 22 |
import java.util.Map; |
| 23 |
import java.util.TreeMap; |
| 24 |
|
| 25 |
import org.locationtech.jts.geom.Coordinate; |
| 26 |
import org.locationtech.jts.geom.Location; |
| 27 |
|
| 28 |
/** |
| 29 |
* A map of nodes, indexed by the coordinate of the node |
| 30 |
* @version 1.7 |
| 31 |
*/ |
| 32 |
public class NodeMap |
| 33 |
|
| 34 |
{ |
| 35 |
|
| 36 |
Map nodeMap = new TreeMap(); |
| 37 |
NodeFactory nodeFact; |
| 38 |
|
| 39 |
public NodeMap(NodeFactory nodeFact) { |
| 40 |
this.nodeFact = nodeFact; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Factory function - subclasses can override to create their own types of nodes |
| 45 |
*/ |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
/** |
| 53 |
* This method expects that a node has a coordinate value. |
| 54 |
*/ |
| 55 |
public Node addNode(Coordinate coord) |
| 56 |
{ |
| 57 |
Node node = (Node) nodeMap.get(coord); |
| 58 |
if (node == null) { |
| 59 |
node = nodeFact.createNode(coord); |
| 60 |
nodeMap.put(coord, node); |
| 61 |
} |
| 62 |
return node; |
| 63 |
} |
| 64 |
|
| 65 |
public Node addNode(Node n) |
| 66 |
{ |
| 67 |
Node node = (Node) nodeMap.get(n.getCoordinate()); |
| 68 |
if (node == null) { |
| 69 |
nodeMap.put(n.getCoordinate(), n); |
| 70 |
return n; |
| 71 |
} |
| 72 |
node.mergeLabel(n); |
| 73 |
return node; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Adds a node for the start point of this EdgeEnd |
| 78 |
* (if one does not already exist in this map). |
| 79 |
* Adds the EdgeEnd to the (possibly new) node. |
| 80 |
*/ |
| 81 |
public void add(EdgeEnd e) |
| 82 |
{ |
| 83 |
Coordinate p = e.getCoordinate(); |
| 84 |
Node n = addNode(p); |
| 85 |
n.add(e); |
| 86 |
} |
| 87 |
/** |
| 88 |
* @return the node if found; null otherwise |
| 89 |
*/ |
| 90 |
public Node find(Coordinate coord) { return (Node) nodeMap.get(coord); } |
| 91 |
|
| 92 |
public Iterator iterator() |
| 93 |
{ |
| 94 |
return nodeMap.values().iterator(); |
| 95 |
} |
| 96 |
public Collection values() |
| 97 |
{ |
| 98 |
return nodeMap.values(); |
| 99 |
} |
| 100 |
|
| 101 |
public Collection getBoundaryNodes(int geomIndex) |
| 102 |
{ |
| 103 |
Collection bdyNodes = new ArrayList(); |
| 104 |
for (Iterator i = iterator(); i.hasNext(); ) { |
| 105 |
Node node = (Node) i.next(); |
| 106 |
if (node.getLabel().getLocation(geomIndex) == Location.BOUNDARY) |
| 107 |
bdyNodes.add(node); |
| 108 |
} |
| 109 |
return bdyNodes; |
| 110 |
} |
| 111 |
|
| 112 |
public void print(PrintStream out) |
| 113 |
{ |
| 114 |
for (Iterator it = iterator(); it.hasNext(); ) |
| 115 |
{ |
| 116 |
Node n = (Node) it.next(); |
| 117 |
n.print(out); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|