001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: * (C) 2002, Refractions Reserach Inc.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation;
010: * version 2.1 of the License.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.graph.build.line;
018:
019: import org.geotools.graph.structure.Edge;
020: import org.geotools.graph.structure.Graphable;
021: import org.geotools.graph.structure.Node;
022:
023: import com.vividsolutions.jts.geom.Coordinate;
024: import com.vividsolutions.jts.geom.GeometryFactory;
025: import com.vividsolutions.jts.geom.LineSegment;
026: import com.vividsolutions.jts.geom.LineString;
027: import com.vividsolutions.jts.geom.MultiLineString;
028:
029: /**
030: * Builds a graph representing a line network in which edges in the network are
031: * represented by LineString geometries. This implementation is a wrapper around
032: * a LineGraphGenerator which sets underlying edge objects to be LineString
033: * objects, and underlying Node objects to be Point objects. While generating
034: * the graph, the generator uses the visited flag of created components to
035: * determine when to create underying objects. For this reason it is not recomended
036: * to modify the visited flag of any graph components.
037: *
038: * @see com.vividsolutions.jts.geom.LineString
039: * @see com.vividsolutions.jts.geom.Point
040: *
041: * @author Justin Deoliveira, Refractions Research Inc, jdeolive@refractions.net
042: *
043: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/graph/src/main/java/org/geotools/graph/build/line/LineStringGraphGenerator.java $
044: */
045: public class LineStringGraphGenerator extends BasicLineGraphGenerator {
046:
047: private static GeometryFactory gf = new GeometryFactory();
048:
049: public Graphable add(Object obj) {
050: LineString ls = null;
051: if (obj instanceof MultiLineString)
052: ls = (LineString) ((MultiLineString) obj).getGeometryN(0);
053: else
054: ls = (LineString) obj;
055:
056: //parent class expects a line segment
057: Edge e = (Edge) super .add(new LineSegment(ls.getCoordinateN(0),
058: ls.getCoordinateN(ls.getNumPoints() - 1)));
059:
060: //over write object to be the linestring
061: e.setObject(ls);
062: return (e);
063: }
064:
065: public Graphable remove(Object obj) {
066: LineString ls = (LineString) obj;
067:
068: //parent ecpexts a line segment
069: return (super .remove(new LineSegment(ls.getCoordinateN(0), ls
070: .getCoordinateN(ls.getNumPoints() - 1))));
071: }
072:
073: public Graphable get(Object obj) {
074: LineString ls = (LineString) obj;
075:
076: //parent ecpexts a line segment
077: return (super .get(new LineSegment(ls.getCoordinateN(0), ls
078: .getCoordinateN(ls.getNumPoints() - 1))));
079: }
080:
081: protected void setObject(Node n, Object obj) {
082: //set underlying object to be point instead of coordinate
083: Coordinate c = (Coordinate) obj;
084: n.setObject(gf.createPoint(c));
085: }
086:
087: // /** underlying line graph generator **/
088: // private LineGraphGenerator m_generator;
089: //
090: // /**
091: // * Constructs a new LineStringGraphGenerator.
092: // *
093: // */
094: // public LineStringGraphGenerator() {
095: // this(new BasicLineGraphGenerator());
096: // }
097: //
098: // /**
099: // * Constructs a new LineStringGraphGenerator.
100: // *
101: // * @param generator Underlying generator.
102: // */
103: // public LineStringGraphGenerator(LineGraphGenerator generator) {
104: // m_generator = generator;
105: // }
106: //
107: // /**
108: // * Adds a LineString object to the graph. An edge is created to represent the
109: // * LineString and its underlying object is set to the LineString.
110: // *
111: // * @param obj A LineString.
112: // *
113: // * @see GraphGenerator#add(Object)
114: // */
115: // public Graphable add(Object obj) {
116: // LineString line = (LineString)obj;
117: // Coordinate[] c = line.getCoordinates();
118: //
119: // //instruct the underlying line graph generate and edge represented by
120: // // the endpoints of the linestring, set the underlying object to be the
121: // //linstring itself
122: // Edge e = (Edge)m_generator.add(
123: // new LineSegment(c[0], c[c.length-1])
124: // );
125: // e.setObject(line);
126: //
127: // //check nodes of edge, if not visited, set object to point
128: // if (!e.getNodeA().isVisited()) {
129: // e.getNodeA().setVisited(true);
130: // e.getNodeA().setObject(line.getPointN(0));
131: // }
132: //
133: // if (!e.getNodeB().isVisited()) {
134: // e.getNodeB().setVisited(true);
135: // e.getNodeB().setObject(line.getPointN(c.length-1));
136: // }
137: //
138: // return(e);
139: // }
140: //
141: // /**
142: // * Returns the edge created to represent a LineString object.
143: // *
144: // * @param obj A LineString.
145: // *
146: // * @see GraphGenerator#get(Object)
147: // */
148: // public Graphable get(Object obj) {
149: // LineString line = (LineString)obj;
150: // Coordinate[] c = line.getCoordinates();
151: //
152: // Edge e = (Edge)m_generator.get(
153: // new LineSegment(c[0], c[c.length-1])
154: // );
155: //
156: // return(e);
157: // }
158: //
159: // /**
160: // * Removes an edge from the graph which represents a LineString object.
161: // *
162: // * @param A LineString.
163: // *
164: // * @see GraphGenerator#remove(Object)
165: // */
166: // public Graphable remove(Object obj) {
167: // LineString line = (LineString)obj;
168: // Coordinate[] c = line.getCoordinates();
169: //
170: // Edge e = (Edge)m_generator.remove(
171: // new LineSegment(c[0], c[c.length-1])
172: // );
173: //
174: // return(e);
175: // }
176: //
177: // /**
178: // * Sets the underlying builder of the underlying generator.
179: // *
180: // * @see GraphGenerator#setGraphBuilder(GraphBuilder)
181: // */
182: // public void setGraphBuilder(GraphBuilder builder) {
183: // m_generator.setGraphBuilder(builder);
184: // }
185: //
186: // /**
187: // * Returns the underlying builder of the underlying generator.
188: // *
189: // * @see GraphGenerator#getGraphBuilder()
190: // */
191: // public GraphBuilder getGraphBuilder() {
192: // return(m_generator.getGraphBuilder());
193: // }
194: //
195: // /**
196: // * @see GraphGenerator#getGraph()
197: // */
198: // public Graph getGraph() {
199: // return(m_generator.getGraph());
200: // }
201: //
202: // public Node getNode(Coordinate c) {
203: // return(m_generator.getNode(c));
204: // }
205: //
206: // public Edge getEdge(Coordinate c1, Coordinate c2) {
207: // return(m_generator.getEdge(c1,c2));
208: // }
209: }
|