diff --git a/modules/core/src/main/java/org/locationtech/jts/operation/relateng/RelateGeometry.java b/modules/core/src/main/java/org/locationtech/jts/operation/relateng/RelateGeometry.java
index 1961798c04..aa043cca6a 100644
--- a/modules/core/src/main/java/org/locationtech/jts/operation/relateng/RelateGeometry.java
+++ b/modules/core/src/main/java/org/locationtech/jts/operation/relateng/RelateGeometry.java
@@ -33,6 +33,7 @@
import org.locationtech.jts.geom.MultiPolygon;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;
+import org.locationtech.jts.geom.Polygonal;
import org.locationtech.jts.geom.util.ComponentCoordinateExtracter;
import org.locationtech.jts.geom.util.PointExtracter;
@@ -251,30 +252,49 @@ public int locateWithDim(Coordinate pt) {
/**
* Indicates whether the geometry requires self-noding
- * for correct evaluation of specific spatial predicates.
- * Self-noding is required for geometries which may self-cross
- * - i.e. lines, and overlapping elements in GeometryCollections.
- * Self-noding is not required for polygonal geometries,
- * since they can only touch at vertices.
+ * for correct evaluation of spatial predicates which need it.
+ *
+ * Generally self-noding is required for geometries containing
+ * edges (i.e. polygons or lines).
+ * However, polygonal geometries which do not contain holes
+ * do not need to be self-noded.
+ * This is a relatively common case, so is worth checking for efficiency.
+ * Self-noding is not required
+ * because while MultiPolygon elements may self-touch at a vertex and an edge,
+ * that vertex is noded against the other predicate input geometry.
+ * The situation where this doesn't hold is when
+ * a hole vertex touches the shell,
+ * and the other geometry has an identical shell.
+ * Thus the presence of holes indicates self-noding is required.
*
* @return true if self-noding is required for this geometry
*/
public boolean isSelfNodingRequired() {
- if (geom instanceof Point
- || geom instanceof MultiPoint
- || geom instanceof Polygon
- || geom instanceof MultiPolygon)
- return false;
- //-- a GC with a single polygon does not need noding
- if (hasAreas && geom.getNumGeometries() == 1)
- return false;
- //-- GCs with only points do not need noding
- if (! hasAreas && ! hasLines)
+ //-- Geometries with only points do not need noding
+ if (! hasEdges())
return false;
+
+ // See https://github.com/libgeos/geos/issues/1558
+ if (isPolygonalNoHoles(geom))
+ return false;
+ //TODO: could avoid self-noding GC with single polygon with no holes
return true;
}
- /**
+ private static boolean isPolygonalNoHoles(Geometry geom) {
+ if (! (geom instanceof Polygonal)) {
+ return false;
+ }
+ //-- test if any polygon element has a hole
+ for (int i = 0; i < geom.getNumGeometries(); i++) {
+ Polygon poly = (Polygon) geom.getGeometryN(i);
+ if (poly.getNumInteriorRing() > 0)
+ return false;
+ }
+ return true;
+ }
+
+ /**
* Tests whether the geometry has polygonal topology.
* This is not the case if it is a GeometryCollection
* containing more than one polygon (since they may overlap
diff --git a/modules/core/src/main/java/org/locationtech/jts/operation/relateng/TopologyComputer.java b/modules/core/src/main/java/org/locationtech/jts/operation/relateng/TopologyComputer.java
index 339c69bebc..06553161c2 100644
--- a/modules/core/src/main/java/org/locationtech/jts/operation/relateng/TopologyComputer.java
+++ b/modules/core/src/main/java/org/locationtech/jts/operation/relateng/TopologyComputer.java
@@ -118,7 +118,7 @@ public boolean isAreaArea() {
* Indicates whether the input geometries require self-noding
* for correct evaluation of specific spatial predicates.
* Self-noding is required for geometries which may
- * have self-crossing linework,
+ * have self-crossing or non-noded self-touching linework,
* or may have lines lying in the boundary of an area.
* This causes the coordinates of nodes created by
* crossing segments to be computed explicitly.
@@ -128,14 +128,11 @@ public boolean isAreaArea() {
* identical to one of the crossed segments.
*
* Currently, requiring self-noding prevents noder caching.
- * So it it important to limit the cases which require self-noding.
- * Currently self-noding is required for:
- *
- * - A geoms which require self-noding (lines or GCs, except for single-polygon GCs)
- *
- B geoms which are mixed A/L GCs
- *
- * Note that linear B inputs do not require self-noding in all cases.
- * In particular, if A is polygonal then predicates with linear B do not require self-noding.
+ * So it is more efficient to limit the cases which require self-noding.
+ * However, avoiding self-noding needs to be conservative, to ensure
+ * correct predicate evaluation.
+ * Currently self-noding is required for most geometry containing
+ * edges, except for the (common) case of polygonal geometries with no holes.
*
* @return true if self-noding is required
*/
@@ -143,13 +140,10 @@ public boolean isSelfNodingRequired() {
if (! predicate.requireSelfNoding())
return false;
- if (geomA.isSelfNodingRequired())
+ if (geomA.isSelfNodingRequired()
+ || geomB.isSelfNodingRequired())
return true;
- //-- if B is a mixed GC with A and L require full noding
- if (geomB.hasAreaAndLine())
- return true;
-
return false;
}
diff --git a/modules/core/src/test/java/org/locationtech/jts/operation/relateng/RelateNGTest.java b/modules/core/src/test/java/org/locationtech/jts/operation/relateng/RelateNGTest.java
index a3cd4018ac..2de684a6f5 100644
--- a/modules/core/src/test/java/org/locationtech/jts/operation/relateng/RelateNGTest.java
+++ b/modules/core/src/test/java/org/locationtech/jts/operation/relateng/RelateNGTest.java
@@ -542,6 +542,79 @@ public void testPolygonsOverlapVeryNarrow() {
//checkTouches(false, a, b);
}
+ //----------- Tests for self-noding -----------------
+
+ // see https://github.com/libgeos/geos/issues/1558
+ public void testSelfNodingPolygonWithHole() {
+ String a = "POLYGON ((0 0, 4 0, 4 4, 0 4, 0 0), (2 0, 1 1, 3 1, 2 0))";
+ String b = "POLYGON ((0 0, 4 0, 4 4, 0 4, 0 0))";
+ checkRelate(a, b, "2FF11F2F2");
+ checkIntersectsDisjoint(a, b, true);
+ checkContainsWithin(a, b, false);
+ checkContainsWithin(b, a, true);
+ checkCoversCoveredBy(a, b, false);
+ checkCoversCoveredBy(b, a, true);
+ checkOverlaps(b, a, false);
+ checkTouches(a, b, false);
+ }
+
+ public void testSelfNodingMultiPolygonWithHole() {
+ String a = "MULTIPOLYGON (((0 0, 4 0, 4 4, 0 4, 0 0), (2 0, 1 1, 3 1, 2 0)))";
+ String b = "POLYGON ((0 0, 4 0, 4 4, 0 4, 0 0))";
+ checkRelate(a, b, "2FF11F2F2");
+ checkIntersectsDisjoint(a, b, true);
+ checkContainsWithin(a, b, false);
+ checkContainsWithin(b, a, true);
+ checkCoversCoveredBy(a, b, false);
+ checkCoversCoveredBy(b, a, true);
+ checkOverlaps(b, a, false);
+ checkTouches(a, b, false);
+ }
+
+ // see https://github.com/libgeos/geos/issues/1597
+ public void testSelfNodingNestedGC() {
+ String a = "GEOMETRYCOLLECTION(GEOMETRYCOLLECTION(LINESTRING(0 0,10 10), POLYGON((-2 -1,4 -1,4 6,-2 6,-2 -1))))";
+ String b = "LINESTRING(0 0,10 10)";
+ checkRelate(a, b, "102001FF2");
+ checkIntersectsDisjoint(a, b, true);
+ checkContainsWithin(a, b, true);
+ checkContainsWithin(b, a, false);
+ checkCoversCoveredBy(a, b, true);
+ checkCoversCoveredBy(b, a, false);
+ checkOverlaps(b, a, false);
+ checkTouches(a, b, false);
+ }
+
+ // see https://github.com/libgeos/geos/issues/1595
+ public void testSelfNodingBOverlappingPolygons() {
+ String a = "POLYGON((0 0,10 0,10 10,0 10,0 0))";
+ String b = "GEOMETRYCOLLECTION(POLYGON((0 0,10 0,10 10,0 10,0 0)), POLYGON((5 -5,15 -5,15 5,5 5,5 -5)))";
+ checkRelate(a, b, "2FF11F212");
+ checkIntersectsDisjoint(a, b, true);
+ checkContainsWithin(a, b, false);
+ checkContainsWithin(b, a, true);
+ checkCoversCoveredBy(a, b, false);
+ checkCoversCoveredBy(b, a, true);
+ checkOverlaps(b, a, false);
+ checkTouches(a, b, false);
+ }
+
+ // see https://github.com/libgeos/geos/issues/1595
+ public void testSelfNodingBLines() {
+ String a = "POLYGON((0 0,10 0,0 10,0 0))";
+ String b = "MULTILINESTRING((0 0,10 0,0 10,0 0),(2 -5,2 0))";
+ checkRelate(a, b, "FF210F102");
+ checkIntersectsDisjoint(a, b, true);
+ checkContainsWithin(a, b, false);
+ checkContainsWithin(b, a, false);
+ checkCoversCoveredBy(a, b, false);
+ checkCoversCoveredBy(b, a, false);
+ checkOverlaps(b, a, false);
+ checkTouches(a, b, true);
+ }
+
+ //-------------------------------------------
+
public void testValidateRelateAA_86() {
String a = "POLYGON ((170 120, 300 120, 250 70, 120 70, 170 120))";
String b = "POLYGON ((150 150, 410 150, 280 20, 20 20, 150 150), (170 120, 330 120, 260 50, 100 50, 170 120))";
diff --git a/modules/tests/src/test/resources/testxml/general/TestRelateAA.xml b/modules/tests/src/test/resources/testxml/general/TestRelateAA.xml
index b73c12d077..b97567deba 100644
--- a/modules/tests/src/test/resources/testxml/general/TestRelateAA.xml
+++ b/modules/tests/src/test/resources/testxml/general/TestRelateAA.xml
@@ -231,6 +231,48 @@
false
+
+ AA - A with hole, B shell equal
+
+ POLYGON ((0 0, 4 0, 4 4, 0 4, 0 0), (2 0, 1 1, 3 1, 2 0))
+
+
+ POLYGON ((0 0, 4 0, 4 4, 0 4, 0 0))
+
+ true
+ false
+ true
+ false
+ false
+ false
+ false
+ true
+ false
+ false
+ true
+
+
+
+ mA/A - mA with single polygon with hole, B shell equal
+
+ MULTIPOLYGON (((0 0, 4 0, 4 4, 0 4, 0 0), (2 0, 1 1, 3 1, 2 0)))
+
+
+ POLYGON ((0 0, 4 0, 4 4, 0 4, 0 0))
+
+ true
+ false
+ true
+ false
+ false
+ false
+ false
+ true
+ false
+ false
+ true
+
+
A/mA A-shells overlapping B-shell at A-vertex
diff --git a/modules/tests/src/test/resources/testxml/general/TestRelateLA.xml b/modules/tests/src/test/resources/testxml/general/TestRelateLA.xml
index 482dc7b2a4..982294e01a 100644
--- a/modules/tests/src/test/resources/testxml/general/TestRelateLA.xml
+++ b/modules/tests/src/test/resources/testxml/general/TestRelateLA.xml
@@ -186,6 +186,19 @@
+
+
+mLA - ring of polygon plus touching line
+
+ MULTILINESTRING((0 0,10 0,0 10,0 0),(2 -5,2 0))
+
+
+ POLYGON((0 0,10 0,0 10,0 0))
+
+ true
+ true
+
+
LA - closed line / empty polygon
diff --git a/modules/tests/src/test/resources/testxml/misc/TestRelateGC.xml b/modules/tests/src/test/resources/testxml/misc/TestRelateGC.xml
index c551e1b8c0..9aed6e3ba3 100644
--- a/modules/tests/src/test/resources/testxml/misc/TestRelateGC.xml
+++ b/modules/tests/src/test/resources/testxml/misc/TestRelateGC.xml
@@ -644,4 +644,75 @@ GEOMETRYCOLLECTION (POLYGON ((1 9, 5 9, 6 6, 1 5, 1 9), (2 6, 4 8, 6 6, 2 6)), P
true
+
+
+ GC:A/mA - polygon, overlapping polygons with one equal
+
+ POLYGON((0 0,10 0,10 10,0 10,0 0))
+
+
+ GEOMETRYCOLLECTION(POLYGON((0 0,10 0,10 10,0 10,0 0)), POLYGON((5 -5,15 -5,15 5,5 5,5 -5)))
+
+ true
+ true
+
+ false
+ true
+ false
+ false
+ false
+ false
+ true
+ false
+ false
+ true
+
+ true
+ false
+ true
+ false
+ false
+ false
+ true
+ false
+ false
+ false
+
+
+
+
+ GC:AL/A - polygon with line, equal line
+
+ GEOMETRYCOLLECTION(GEOMETRYCOLLECTION(
+ LINESTRING(0 0,10 10), POLYGON((-2 -1,4 -1,4 6,-2 6,-2 -1))))
+
+
+ LINESTRING(0 0,10 10)
+
+ true
+ true
+
+ true
+ false
+ true
+ false
+ false
+ false
+ true
+ false
+ false
+ false
+
+ false
+ true
+ false
+ false
+ false
+ false
+ true
+ false
+ false
+ true
+
+