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 aa043cca6a..91ad3d7954 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 @@ -256,16 +256,11 @@ public int locateWithDim(Coordinate pt) { *

* Generally self-noding is required for geometries containing * edges (i.e. polygons or lines). - * However, polygonal geometries which do not contain holes + * However, a single polygon which does 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. + * Self-noding is not required because the shell of a valid Polygon cannot self-touch + * (whereas valid MultiPolygons and holes can self-touch.) + * This is a relatively common case, so is worth checking for efficiency. * * @return true if self-noding is required for this geometry */ @@ -275,23 +270,22 @@ public boolean isSelfNodingRequired() { return false; // See https://github.com/libgeos/geos/issues/1558 - if (isPolygonalNoHoles(geom)) + if (isSinglePolygonNoHoles(geom)) return false; //TODO: could avoid self-noding GC with single polygon with no holes return true; } - private static boolean isPolygonalNoHoles(Geometry geom) { + private static boolean isSinglePolygonNoHoles(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; + if (geom.getNumGeometries() > 1) + return false; + + Polygon poly = (Polygon) geom.getGeometryN(0); + boolean hasHole = poly.getNumInteriorRing() > 0; + return ! hasHole; } /** 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 06553161c2..bdc3ce6378 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 @@ -120,19 +120,19 @@ public boolean isAreaArea() { * Self-noding is required for geometries which may * 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. - * This ensures that node locations match in situations - * where a self-crossing and mutual crossing occur at the same logical location. - * The canonical example is a self-crossing line tested against a single segment - * identical to one of the crossed segments. + * Self-noding causes nodes created by + * at crosses and touches to be computed explicitly, + * which is required for correct topology building at the node. + * Canonical examples include a self-crossing line tested against a single segment + * identical to one of the crossed segments; + * and two elements of a MultiPolygon touching at a vertex and and edge. * * Currently, requiring self-noding prevents noder caching. * 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. + * edges, except for the (common) case of a polygon with no holes. * * @return true if self-noding is required */ 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 2de684a6f5..fd2a2f0447 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 @@ -599,6 +599,19 @@ public void testSelfNodingBOverlappingPolygons() { checkTouches(a, b, false); } + public void testSelfNodingMultiPolygonTouch() { + String a = "MULTIPOLYGON (((1 5, 5 5, 5 1, 1 1, 1 5)), ((1 8, 5 8, 3 5, 1 8)))"; + String b = "POLYGON ((1 5, 5 5, 5 1, 1 1, 1 5))"; + //checkRelate(a, b, "2FF11F212"); + 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 testSelfNodingBLines() { String a = "POLYGON((0 0,10 0,0 10,0 0))"; diff --git a/modules/tests/src/test/resources/testxml/general/TestRelateAA.xml b/modules/tests/src/test/resources/testxml/general/TestRelateAA.xml index b97567deba..d3b55a2950 100644 --- a/modules/tests/src/test/resources/testxml/general/TestRelateAA.xml +++ b/modules/tests/src/test/resources/testxml/general/TestRelateAA.xml @@ -287,4 +287,25 @@ true + + A/mA - A equal to one eleent of mA, touching at vertex and edge + + POLYGON ((1 5, 5 5, 5 1, 1 1, 1 5)) + + + MULTIPOLYGON (((1 5, 5 5, 5 1, 1 1, 1 5)), ((1 8, 5 8, 3 5, 1 8))) + + true + false + true + false + false + false + false + true + false + false + true + +