Class EdgeString

 1  
 2 /*
 3  * Copyright (c) 2016 Vivid Solutions.
 4  *
 5  * All rights reserved. This program and the accompanying materials
 6  * are made available under the terms of the Eclipse Public License 2.0
 7  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
 8  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
 9  * and the Eclipse Distribution License is available at
10  *
11  * http://www.eclipse.org/org/documents/edl-v10.php.
12  */
13 package org.locationtech.jts.operation.linemerge;
14  
15  
16 import java.util.ArrayList;
17 import java.util.Iterator;
18 import java.util.List;
19  
20 import org.locationtech.jts.geom.Coordinate;
21 import org.locationtech.jts.geom.CoordinateArrays;
22 import org.locationtech.jts.geom.CoordinateList;
23 import org.locationtech.jts.geom.GeometryFactory;
24 import org.locationtech.jts.geom.LineString;
25  
26 /**
27  * A sequence of {@link LineMergeDirectedEdge}s forming one of the lines that will
28  * be output by the line-merging process.
29  *
30  * @version 1.7
31  */
32 public class EdgeString {
33   private GeometryFactory factory;
34   private List directedEdges = new ArrayList();
35   private Coordinate[] coordinates = null;
36   /**
37    * Constructs an EdgeString with the given factory used to convert this EdgeString
38    * to a LineString
39    */
40   public EdgeString(GeometryFactory factory) {
41     this.factory = factory;
42   }
43  
44   /**
45    * Adds a directed edge which is known to form part of this line.
46    */
47   public void add(LineMergeDirectedEdge directedEdge) {
48     directedEdges.add(directedEdge);
49   }
50  
51   private Coordinate[] getCoordinates() {
52     if (coordinates == null) {
53       int forwardDirectedEdges = 0;
54       int reverseDirectedEdges = 0;
55       CoordinateList coordinateList = new CoordinateList();
56       for (Iterator i = directedEdges.iterator(); i.hasNext();) {
57         LineMergeDirectedEdge directedEdge = (LineMergeDirectedEdge) i.next();
58         if (directedEdge.getEdgeDirection()) {
59           forwardDirectedEdges++;
60         }
61         else {
62           reverseDirectedEdges++;
63         }
64         coordinateList.add(((LineMergeEdge) directedEdge.getEdge()).getLine()
65                             .getCoordinates(), false,
66           directedEdge.getEdgeDirection());
67       }
68       coordinates = coordinateList.toCoordinateArray();
69       if (reverseDirectedEdges > forwardDirectedEdges) {
70         CoordinateArrays.reverse(coordinates);
71       }
72     }
73  
74     return coordinates;
75   }
76  
77   /**
78    * Converts this EdgeString into a LineString.
79    */
80   public LineString toLineString() {
81     return factory.createLineString(getCoordinates());
82   }
83 }
84