1 2 3 4 /* 5 * Copyright (c) 2016 Vivid Solutions. 6 * 7 * All rights reserved. This program and the accompanying materials 8 * are made available under the terms of the Eclipse Public License 2.0 9 * and Eclipse Distribution License v. 1.0 which accompanies this distribution. 10 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html 11 * and the Eclipse Distribution License is available at 12 * 13 * http://www.eclipse.org/org/documents/edl-v10.php. 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 //Map nodeMap = new HashMap(); 36 Map nodeMap = new TreeMap(); 37 NodeFactory nodeFact; 38 NodeMap(NodeFactory nodeFact)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 protected Node createNode(Coordinate coord) 48 { 49 return new Node(coord); 50 } 51 */ 52 /** 53 * This method expects that a node has a coordinate value. 54 */ addNode(Coordinate coord)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 addNode(Node n)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 */ add(EdgeEnd e)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 */ find(Coordinate coord)90 public Node find(Coordinate coord) { return (Node) nodeMap.get(coord); } 91 iterator()92 public Iterator iterator() 93 { 94 return nodeMap.values().iterator(); 95 } values()96 public Collection values() 97 { 98 return nodeMap.values(); 99 } 100 getBoundaryNodes(int geomIndex)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 print(PrintStream out)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