Class LineMergeGraph

 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 import org.locationtech.jts.geom.Coordinate;
16 import org.locationtech.jts.geom.CoordinateArrays;
17 import org.locationtech.jts.geom.LineString;
18 import org.locationtech.jts.planargraph.DirectedEdge;
19 import org.locationtech.jts.planargraph.Edge;
20 import org.locationtech.jts.planargraph.Node;
21 import org.locationtech.jts.planargraph.PlanarGraph;
22  
23 /**
24  * A planar graph of edges that is analyzed to sew the edges together. The 
25  * <code>marked</code> flag on {@link org.locationtech.jts.planargraph.Edge}s
26  * and {@link org.locationtech.jts.planargraph.Node}s indicates whether they have been
27  * logically deleted from the graph.
28  *
29  * @version 1.7
30  */
31 public class LineMergeGraph extends PlanarGraph 
32 {
33   /**
34    * Adds an Edge, DirectedEdges, and Nodes for the given LineString representation
35    * of an edge. 
36    * Empty lines or lines with all coordinates equal are not added.
37    * 
38    * @param lineString the linestring to add to the graph
39    */
40   public void addEdge(LineString lineString) {
41     if (lineString.isEmpty()) { return; }
42     
43     Coordinate[] coordinates = CoordinateArrays.removeRepeatedPoints(lineString.getCoordinates());
44     
45     // don't add lines with all coordinates equal
46     if (coordinates.length <= 1return;
47     
48     Coordinate startCoordinate = coordinates[0];
49     Coordinate endCoordinate = coordinates[coordinates.length - 1];
50     Node startNode = getNode(startCoordinate);
51     Node endNode = getNode(endCoordinate);
52     DirectedEdge directedEdge0 = new LineMergeDirectedEdge(startNode, endNode,
53         coordinates[1], true);
54     DirectedEdge directedEdge1 = new LineMergeDirectedEdge(endNode, startNode,
55         coordinates[coordinates.length - 2], false);
56     Edge edge = new LineMergeEdge(lineString);
57     edge.setDirectedEdges(directedEdge0, directedEdge1);
58     add(edge);
59   }
60  
61   private Node getNode(Coordinate coordinate) {
62     Node node = findNode(coordinate);
63     if (node == null) {
64       node = new Node(coordinate);
65       add(node);
66     }
67  
68     return node;
69   }
70 }
71