001:
002: /*
003: * The JTS Topology Suite is a collection of Java classes that
004: * implement the fundamental operations required to validate a given
005: * geo-spatial data set to a known topological specification.
006: *
007: * Copyright (C) 2001 Vivid Solutions
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022: *
023: * For more information, contact:
024: *
025: * Vivid Solutions
026: * Suite #1A
027: * 2328 Government Street
028: * Victoria BC V8T 5G5
029: * Canada
030: *
031: * (250)385-6040
032: * www.vividsolutions.com
033: */
034: package com.vividsolutions.jts.geomgraph;
035:
036: import java.io.PrintStream;
037: import java.util.*;
038: import com.vividsolutions.jts.geom.Coordinate;
039: import com.vividsolutions.jts.geom.Location;
040: import com.vividsolutions.jts.geomgraph.Node;
041:
042: /**
043: * A map of nodes, indexed by the coordinate of the node
044: * @version 1.7
045: */
046: public class NodeMap
047:
048: {
049: //Map nodeMap = new HashMap();
050: Map nodeMap = new TreeMap();
051: NodeFactory nodeFact;
052:
053: public NodeMap(NodeFactory nodeFact) {
054: this .nodeFact = nodeFact;
055: }
056:
057: /**
058: * Factory function - subclasses can override to create their own types of nodes
059: */
060: /*
061: protected Node createNode(Coordinate coord)
062: {
063: return new Node(coord);
064: }
065: */
066: /**
067: * This method expects that a node has a coordinate value.
068: */
069: public Node addNode(Coordinate coord) {
070: Node node = (Node) nodeMap.get(coord);
071: if (node == null) {
072: node = nodeFact.createNode(coord);
073: nodeMap.put(coord, node);
074: }
075: return node;
076: }
077:
078: public Node addNode(Node n) {
079: Node node = (Node) nodeMap.get(n.getCoordinate());
080: if (node == null) {
081: nodeMap.put(n.getCoordinate(), n);
082: return n;
083: }
084: node.mergeLabel(n);
085: return node;
086: }
087:
088: /**
089: * Adds a node for the start point of this EdgeEnd
090: * (if one does not already exist in this map).
091: * Adds the EdgeEnd to the (possibly new) node.
092: */
093: public void add(EdgeEnd e) {
094: Coordinate p = e.getCoordinate();
095: Node n = addNode(p);
096: n.add(e);
097: }
098:
099: /**
100: * @return the node if found; null otherwise
101: */
102: public Node find(Coordinate coord) {
103: return (Node) nodeMap.get(coord);
104: }
105:
106: public Iterator iterator() {
107: return nodeMap.values().iterator();
108: }
109:
110: public Collection values() {
111: return nodeMap.values();
112: }
113:
114: public Collection getBoundaryNodes(int geomIndex) {
115: Collection bdyNodes = new ArrayList();
116: for (Iterator i = iterator(); i.hasNext();) {
117: Node node = (Node) i.next();
118: if (node.getLabel().getLocation(geomIndex) == Location.BOUNDARY)
119: bdyNodes.add(node);
120: }
121: return bdyNodes;
122: }
123:
124: public void print(PrintStream out) {
125: for (Iterator it = iterator(); it.hasNext();) {
126: Node n = (Node) it.next();
127: n.print(out);
128: }
129: }
130: }
|