Skip to content

Commit c874270

Browse files
authored
Added spatial index to OverlayNG PolygonBuilder.placeFreeHoles (#1173)
Signed-off-by: arriopolis <arjan_rekenmachine@hotmail.com>
1 parent 870d701 commit c874270

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

modules/core/src/main/java/org/locationtech/jts/operation/overlayng/PolygonBuilder.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import org.locationtech.jts.geom.Polygon;
2020
import org.locationtech.jts.geom.TopologyException;
2121
import org.locationtech.jts.util.Assert;
22+
import org.locationtech.jts.index.SpatialIndex;
23+
import org.locationtech.jts.index.hprtree.HPRtree;
2224

2325
class PolygonBuilder {
2426

@@ -175,11 +177,16 @@ private static void assignHoles(OverlayEdgeRing shell, List<OverlayEdgeRing> edg
175177
*/
176178
private void placeFreeHoles(List<OverlayEdgeRing> shellList, List<OverlayEdgeRing> freeHoleList)
177179
{
178-
// TODO: use a spatial index to improve performance
180+
SpatialIndex index = new HPRtree();
181+
for (OverlayEdgeRing shell : shellList) {
182+
index.insert(shell.getRing().getEnvelopeInternal(), shell);
183+
}
184+
179185
for (OverlayEdgeRing hole : freeHoleList ) {
180186
// only place this hole if it doesn't yet have a shell
181187
if (hole.getShell() == null) {
182-
OverlayEdgeRing shell = hole.findEdgeRingContaining(shellList);
188+
List<OverlayEdgeRing> shellListOverlaps = index.query(hole.getRing().getEnvelopeInternal());
189+
OverlayEdgeRing shell = hole.findEdgeRingContaining(shellListOverlaps);
183190
// only when building a polygon-valid result
184191
if (isEnforcePolygonal && shell == null) {
185192
throw new TopologyException("unable to assign free hole to a shell", hole.getCoordinate());

0 commit comments

Comments
 (0)