| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
package org.locationtech.jts.operation.valid; |
| 13 |
|
| 14 |
import java.util.ArrayList; |
| 15 |
import java.util.List; |
| 16 |
|
| 17 |
import org.locationtech.jts.algorithm.PointLocation; |
| 18 |
import org.locationtech.jts.algorithm.locate.IndexedPointInAreaLocator; |
| 19 |
import org.locationtech.jts.geom.Coordinate; |
| 20 |
import org.locationtech.jts.geom.Envelope; |
| 21 |
import org.locationtech.jts.geom.LinearRing; |
| 22 |
import org.locationtech.jts.geom.Location; |
| 23 |
import org.locationtech.jts.geomgraph.GeometryGraph; |
| 24 |
import org.locationtech.jts.index.SpatialIndex; |
| 25 |
import org.locationtech.jts.index.strtree.STRtree; |
| 26 |
|
| 27 |
/** |
| 28 |
* Tests whether any of a set of {@link LinearRing}s are |
| 29 |
* nested inside another ring in the set, using a spatial |
| 30 |
* index to speed up the comparisons. |
| 31 |
* |
| 32 |
* @version 1.7 |
| 33 |
*/ |
| 34 |
public class IndexedNestedRingTester |
| 35 |
{ |
| 36 |
private GeometryGraph graph; |
| 37 |
private List rings = new ArrayList(); |
| 38 |
private Envelope totalEnv = new Envelope(); |
| 39 |
private SpatialIndex index; |
| 40 |
private Coordinate nestedPt; |
| 41 |
|
| 42 |
public IndexedNestedRingTester(GeometryGraph graph) |
| 43 |
{ |
| 44 |
this.graph = graph; |
| 45 |
} |
| 46 |
|
| 47 |
public Coordinate getNestedPoint() { return nestedPt; } |
| 48 |
|
| 49 |
public void add(LinearRing ring) |
| 50 |
{ |
| 51 |
rings.add(ring); |
| 52 |
totalEnv.expandToInclude(ring.getEnvelopeInternal()); |
| 53 |
} |
| 54 |
|
| 55 |
public boolean isNonNested() |
| 56 |
{ |
| 57 |
buildIndex(); |
| 58 |
|
| 59 |
for (int i = 0; i < rings.size(); i++) { |
| 60 |
LinearRing innerRing = (LinearRing) rings.get(i); |
| 61 |
Coordinate[] innerRingPts = innerRing.getCoordinates(); |
| 62 |
|
| 63 |
List results = index.query(innerRing.getEnvelopeInternal()); |
| 64 |
|
| 65 |
for (int j = 0; j < results.size(); j++) { |
| 66 |
LinearRing searchRing = (LinearRing) results.get(j); |
| 67 |
Coordinate[] searchRingPts = searchRing.getCoordinates(); |
| 68 |
|
| 69 |
if (innerRing == searchRing) |
| 70 |
continue; |
| 71 |
|
| 72 |
if (! innerRing.getEnvelopeInternal().intersects(searchRing.getEnvelopeInternal())) |
| 73 |
continue; |
| 74 |
|
| 75 |
Coordinate innerRingPt = IsValidOp.findPtNotNode(innerRingPts, searchRing, graph); |
| 76 |
|
| 77 |
/** |
| 78 |
* If no non-node pts can be found, this means |
| 79 |
* that the searchRing touches ALL of the innerRing vertices. |
| 80 |
* This indicates an invalid polygon, since either |
| 81 |
* the two holes create a disconnected interior, |
| 82 |
* or they touch in an infinite number of points |
| 83 |
* (i.e. along a line segment). |
| 84 |
* Both of these cases are caught by other tests, |
| 85 |
* so it is safe to simply skip this situation here. |
| 86 |
*/ |
| 87 |
if (innerRingPt == null) |
| 88 |
continue; |
| 89 |
|
| 90 |
boolean isInside = PointLocation.isInRing(innerRingPt, searchRingPts); |
| 91 |
if (isInside) { |
| 92 |
nestedPt = innerRingPt; |
| 93 |
return false; |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
return true; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* An implementation of an optimization introduced in GEOS |
| 102 |
* https://github.com/libgeos/geos/pull/255/commits/1bf16cdf5a4827b483a1f712e0597ccb243f58cb |
| 103 |
* |
| 104 |
* Not used for now, since improvement is small and very data-dependent. |
| 105 |
* |
| 106 |
* @return |
| 107 |
*/ |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
private void buildIndex() |
| 148 |
{ |
| 149 |
index = new STRtree(); |
| 150 |
|
| 151 |
for (int i = 0; i < rings.size(); i++) { |
| 152 |
LinearRing ring = (LinearRing) rings.get(i); |
| 153 |
Envelope env = ring.getEnvelopeInternal(); |
| 154 |
index.insert(env, ring); |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|