001: /*
002: * The JTS Topology Suite is a collection of Java classes that
003: * implement the fundamental operations required to validate a given
004: * geo-spatial data set to a known topological specification.
005: *
006: * Copyright (C) 2001 Vivid Solutions
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Lesser General Public
010: * License as published by the Free Software Foundation; either
011: * version 2.1 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public
019: * License along with this library; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021: *
022: * For more information, contact:
023: *
024: * Vivid Solutions
025: * Suite #1A
026: * 2328 Government Street
027: * Victoria BC V8T 5G5
028: * Canada
029: *
030: * (250)385-6040
031: * www.vividsolutions.com
032: */
033: package com.vividsolutions.jts.operation.linemerge;
034:
035: import com.vividsolutions.jts.geom.*;
036: import com.vividsolutions.jts.geom.Coordinate;
037: import com.vividsolutions.jts.geom.CoordinateList;
038: import com.vividsolutions.jts.geom.GeometryFactory;
039: import com.vividsolutions.jts.geom.LineString;
040:
041: import java.util.ArrayList;
042: import java.util.Iterator;
043: import java.util.List;
044:
045: /**
046: * A sequence of {@link LineMergeDirectedEdge}s forming one of the lines that will
047: * be output by the line-merging process.
048: *
049: * @version 1.7
050: */
051: public class EdgeString {
052: private GeometryFactory factory;
053: private List directedEdges = new ArrayList();
054: private Coordinate[] coordinates = null;
055:
056: /**
057: * Constructs an EdgeString with the given factory used to convert this EdgeString
058: * to a LineString
059: */
060: public EdgeString(GeometryFactory factory) {
061: this .factory = factory;
062: }
063:
064: /**
065: * Adds a directed edge which is known to form part of this line.
066: */
067: public void add(LineMergeDirectedEdge directedEdge) {
068: directedEdges.add(directedEdge);
069: }
070:
071: private Coordinate[] getCoordinates() {
072: if (coordinates == null) {
073: int forwardDirectedEdges = 0;
074: int reverseDirectedEdges = 0;
075: CoordinateList coordinateList = new CoordinateList();
076: for (Iterator i = directedEdges.iterator(); i.hasNext();) {
077: LineMergeDirectedEdge directedEdge = (LineMergeDirectedEdge) i
078: .next();
079: if (directedEdge.getEdgeDirection()) {
080: forwardDirectedEdges++;
081: } else {
082: reverseDirectedEdges++;
083: }
084: coordinateList.add(((LineMergeEdge) directedEdge
085: .getEdge()).getLine().getCoordinates(), false,
086: directedEdge.getEdgeDirection());
087: }
088: coordinates = coordinateList.toCoordinateArray();
089: if (reverseDirectedEdges > forwardDirectedEdges) {
090: CoordinateArrays.reverse(coordinates);
091: }
092: }
093:
094: return coordinates;
095: }
096:
097: /**
098: * Converts this EdgeString into a LineString.
099: */
100: public LineString toLineString() {
101: return factory.createLineString(getCoordinates());
102: }
103: }
|