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.index.SpatialIndex;
040: import com.vividsolutions.jts.index.quadtree.Quadtree;
041:
042: /**
043: * A EdgeList is a list of Edges. It supports locating edges
044: * that are pointwise equals to a target edge.
045: * @version 1.7
046: */
047: public class EdgeList {
048: private List edges = new ArrayList();
049: /**
050: * An index of the edges, for fast lookup.
051: *
052: * a Quadtree is used, because this index needs to be dynamic
053: * (e.g. allow insertions after queries).
054: * An alternative would be to use an ordered set based on the values
055: * of the edge coordinates
056: *
057: */
058: private SpatialIndex index = new Quadtree();
059:
060: public EdgeList() {
061: }
062:
063: /**
064: * Insert an edge unless it is already in the list
065: */
066: public void add(Edge e) {
067: edges.add(e);
068: index.insert(e.getEnvelope(), e);
069: }
070:
071: public void addAll(Collection edgeColl) {
072: for (Iterator i = edgeColl.iterator(); i.hasNext();) {
073: add((Edge) i.next());
074: }
075: }
076:
077: public List getEdges() {
078: return edges;
079: }
080:
081: // <FIX> fast lookup for edges
082: /**
083: * If there is an edge equal to e already in the list, return it.
084: * Otherwise return null.
085: * @return equal edge, if there is one already in the list
086: * null otherwise
087: */
088: public Edge findEqualEdge(Edge e) {
089: Collection testEdges = index.query(e.getEnvelope());
090:
091: for (Iterator i = testEdges.iterator(); i.hasNext();) {
092: Edge testEdge = (Edge) i.next();
093: if (testEdge.equals(e))
094: return testEdge;
095: }
096: return null;
097: }
098:
099: public Iterator iterator() {
100: return edges.iterator();
101: }
102:
103: public Edge get(int i) {
104: return (Edge) edges.get(i);
105: }
106:
107: /**
108: * If the edge e is already in the list, return its index.
109: * @return index, if e is already in the list
110: * -1 otherwise
111: */
112: public int findEdgeIndex(Edge e) {
113: for (int i = 0; i < edges.size(); i++) {
114: if (((Edge) edges.get(i)).equals(e))
115: return i;
116: }
117: return -1;
118: }
119:
120: public void print(PrintStream out) {
121: out.print("MULTILINESTRING ( ");
122: for (int j = 0; j < edges.size(); j++) {
123: Edge e = (Edge) edges.get(j);
124: if (j > 0)
125: out.print(",");
126: out.print("(");
127: Coordinate[] pts = e.getCoordinates();
128: for (int i = 0; i < pts.length; i++) {
129: if (i > 0)
130: out.print(",");
131: out.print(pts[i].x + " " + pts[i].y);
132: }
133: out.println(")");
134: }
135: out.print(") ");
136: }
137:
138: }
|