Class ConsistentAreaTester

  1  
  2  
  3 /*
  4  * Copyright (c) 2016 Vivid Solutions.
  5  *
  6  * All rights reserved. This program and the accompanying materials
  7  * are made available under the terms of the Eclipse Public License 2.0
  8  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
  9  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
 10  * and the Eclipse Distribution License is available at
 11  *
 12  * http://www.eclipse.org/org/documents/edl-v10.php.
 13  */
 14 package org.locationtech.jts.operation.valid;
 15  
 16 import java.util.Iterator;
 17  
 18 import org.locationtech.jts.algorithm.LineIntersector;
 19 import org.locationtech.jts.algorithm.RobustLineIntersector;
 20 import org.locationtech.jts.geom.Coordinate;
 21 import org.locationtech.jts.geom.MultiPolygon;
 22 import org.locationtech.jts.geom.Polygon;
 23 import org.locationtech.jts.geomgraph.GeometryGraph;
 24 import org.locationtech.jts.geomgraph.index.SegmentIntersector;
 25 import org.locationtech.jts.operation.relate.EdgeEndBundle;
 26 import org.locationtech.jts.operation.relate.RelateNode;
 27 import org.locationtech.jts.operation.relate.RelateNodeGraph;
 28  
 29 /**
 30  * Checks that a {@link GeometryGraph} representing an area
 31  * (a {@link Polygon} or {@link MultiPolygon} )
 32  * has consistent semantics for area geometries.
 33  * This check is required for any reasonable polygonal model
 34  * (including the OGC-SFS model, as well as models which allow ring self-intersection at single points)
 35  * <p>
 36  * Checks include:
 37  * <ul>
 38  * <li>test for rings which properly intersect
 39  * (but not for ring self-intersection, or intersections at vertices)
 40  * <li>test for consistent labelling at all node points
 41  * (this detects vertex intersections with invalid topology,
 42  * i.e. where the exterior side of an edge lies in the interior of the area)
 43  * <li>test for duplicate rings
 44  * </ul>
 45  * If an inconsistency is found the location of the problem
 46  * is recorded and is available to the caller.
 47  *
 48  * @version 1.7
 49  */
 50 public class ConsistentAreaTester {
 51  
 52   private final LineIntersector li = new RobustLineIntersector();
 53   private GeometryGraph geomGraph;
 54   private RelateNodeGraph nodeGraph = new RelateNodeGraph();
 55  
 56   // the intersection point found (if any)
 57   private Coordinate invalidPoint;
 58  
 59   /**
 60    * Creates a new tester for consistent areas.
 61    *
 62    * @param geomGraph the topology graph of the area geometry
 63    */
 64   public ConsistentAreaTester(GeometryGraph geomGraph)
 65   {
 66     this.geomGraph = geomGraph;
 67   }
 68  
 69     /**
 70    * @return the intersection point, or <code>null</code> if none was found
 71    */
 72   public Coordinate getInvalidPoint() { return invalidPoint; }
 73  
 74   /**
 75    * Check all nodes to see if their labels are consistent with area topology.
 76    *
 77    * @return <code>true</code> if this area has a consistent node labelling
 78    */
 79   public boolean isNodeConsistentArea()
 80   {
 81     /**
 82      * To fully check validity, it is necessary to
 83      * compute ALL intersections, including self-intersections within a single edge.
 84      */
 85     SegmentIntersector intersector = geomGraph.computeSelfNodes(li, truetrue);
 86     /**
 87      * A proper intersection means that the area is not consistent.
 88      */
 89     if (intersector.hasProperIntersection()) {
 90       invalidPoint = intersector.getProperIntersectionPoint();
 91       return false;
 92     }
 93  
 94     nodeGraph.build(geomGraph);
 95  
 96     return isNodeEdgeAreaLabelsConsistent();
 97   }
 98  
 99   /**
100    * Check all nodes to see if their labels are consistent.
101    * If any are not, return false
102    *
103    * @return <code>true</code> if the edge area labels are consistent at this node
104    */
105   private boolean isNodeEdgeAreaLabelsConsistent()
106   {
107     for (Iterator nodeIt = nodeGraph.getNodeIterator(); nodeIt.hasNext(); ) {
108       RelateNode node = (RelateNode) nodeIt.next();
109       if (! node.getEdges().isAreaLabelsConsistent(geomGraph)) {
110         invalidPoint = node.getCoordinate().copy();
111         return false;
112       }
113     }
114     return true;
115   }
116  
117   /**
118    * Checks for two duplicate rings in an area.
119    * Duplicate rings are rings that are topologically equal
120    * (that is, which have the same sequence of points up to point order).
121    * If the area is topologically consistent (determined by calling the
122    * <code>isNodeConsistentArea</code>,
123    * duplicate rings can be found by checking for EdgeBundles which contain
124    * more than one EdgeEnd.
125    * (This is because topologically consistent areas cannot have two rings sharing
126    * the same line segment, unless the rings are equal).
127    * The start point of one of the equal rings will be placed in
128    * invalidPoint.
129    *
130    * @return true if this area Geometry is topologically consistent but has two duplicate rings
131    */
132   public boolean hasDuplicateRings()
133   {
134     for (Iterator nodeIt = nodeGraph.getNodeIterator(); nodeIt.hasNext(); ) {
135       RelateNode node = (RelateNode) nodeIt.next();
136       for (Iterator i = node.getEdges().iterator(); i.hasNext(); ) {
137         EdgeEndBundle eeb = (EdgeEndBundle) i.next();
138         if (eeb.getEdgeEnds().size() > 1) {
139           invalidPoint = eeb.getEdge().getCoordinate(0);
140           return true;
141         }
142       }
143     }
144     return false;
145   }
146  
147  
148  
149 }
150