Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
* <p>
* 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -128,28 +128,22 @@ 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:
* <ul>
* <li>A geoms which require self-noding (lines or GCs, except for single-polygon GCs)
* <li>B geoms which are mixed A/L GCs
* </ul>
* 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
*/
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))";
Expand Down
42 changes: 42 additions & 0 deletions modules/tests/src/test/resources/testxml/general/TestRelateAA.xml
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,48 @@
<test> <op name="contains" arg1="A" arg2="B"> false </op></test>
</case>

<case>
<desc>AA - A with hole, B shell equal</desc>
<a>
POLYGON ((0 0, 4 0, 4 4, 0 4, 0 0), (2 0, 1 1, 3 1, 2 0))
</a>
<b>
POLYGON ((0 0, 4 0, 4 4, 0 4, 0 0))
</b>
<test><op name="relate" arg3="2FF11F2F2" arg1="A" arg2="B"> true </op></test>
<test><op name="contains" arg1="A" arg2="B"> false </op></test>
<test><op name="coveredBy" arg1="A" arg2="B"> true </op></test>
<test><op name="covers" arg1="A" arg2="B"> false </op></test>
<test><op name="crosses" arg1="A" arg2="B"> false </op></test>
<test><op name="disjoint" arg1="A" arg2="B"> false </op></test>
<test><op name="equalsTopo" arg1="A" arg2="B"> false </op></test>
<test><op name="intersects" arg1="A" arg2="B"> true </op></test>
<test><op name="overlaps" arg1="A" arg2="B"> false </op></test>
<test><op name="touches" arg1="A" arg2="B"> false </op></test>
<test><op name="within" arg1="A" arg2="B"> true </op></test>
</case>

<case>
<desc>mA/A - mA with single polygon with hole, B shell equal</desc>
<a>
MULTIPOLYGON (((0 0, 4 0, 4 4, 0 4, 0 0), (2 0, 1 1, 3 1, 2 0)))
</a>
<b>
POLYGON ((0 0, 4 0, 4 4, 0 4, 0 0))
</b>
<test><op name="relate" arg3="2FF11F2F2" arg1="A" arg2="B"> true </op></test>
<test><op name="contains" arg1="A" arg2="B"> false </op></test>
<test><op name="coveredBy" arg1="A" arg2="B"> true </op></test>
<test><op name="covers" arg1="A" arg2="B"> false </op></test>
<test><op name="crosses" arg1="A" arg2="B"> false </op></test>
<test><op name="disjoint" arg1="A" arg2="B"> false </op></test>
<test><op name="equalsTopo" arg1="A" arg2="B"> false </op></test>
<test><op name="intersects" arg1="A" arg2="B"> true </op></test>
<test><op name="overlaps" arg1="A" arg2="B"> false </op></test>
<test><op name="touches" arg1="A" arg2="B"> false </op></test>
<test><op name="within" arg1="A" arg2="B"> true </op></test>
</case>

<case>
<desc>A/mA A-shells overlapping B-shell at A-vertex</desc>
<a>
Expand Down
13 changes: 13 additions & 0 deletions modules/tests/src/test/resources/testxml/general/TestRelateLA.xml
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,19 @@
</test>
</case>

<case>
<!-- https://github.com/libgeos/geos/issues/1595 -->
<desc>mLA - ring of polygon plus touching line</desc>
<a>
MULTILINESTRING((0 0,10 0,0 10,0 0),(2 -5,2 0))
</a>
<b>
POLYGON((0 0,10 0,0 10,0 0))
</b>
<test><op name="relate" arg1="A" arg2="B" arg3="F11F002F2">true</op></test>
<test><op name="relate" arg1="B" arg2="A" arg3="FF210F102">true</op></test>
</case>

<case>
<desc>LA - closed line / empty polygon</desc>
<a>
Expand Down
71 changes: 71 additions & 0 deletions modules/tests/src/test/resources/testxml/misc/TestRelateGC.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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
<test><op name="relate" arg1="A" arg2="B" arg3="2FFF1FFF2"> true </op></test>
</case>

<case>
<!-- https://github.com/libgeos/geos/issues/1595 -->
<desc>GC:A/mA - polygon, overlapping polygons with one equal </desc>
<a>
POLYGON((0 0,10 0,10 10,0 10,0 0))
</a>
<b>
GEOMETRYCOLLECTION(POLYGON((0 0,10 0,10 10,0 10,0 0)), POLYGON((5 -5,15 -5,15 5,5 5,5 -5)))
</b>
<test><op name="relate" arg3="2FF11F212" arg1="A" arg2="B"> true </op></test>
<test><op name="relate" arg3="212F11FF2" arg1="B" arg2="A"> true </op></test>

<test><op name="contains" arg1="A" arg2="B"> false </op></test>
<test><op name="coveredBy" arg1="A" arg2="B"> true </op></test>
<test><op name="covers" arg1="A" arg2="B"> false </op></test>
<test><op name="crosses" arg1="A" arg2="B"> false </op></test>
<test><op name="disjoint" arg1="A" arg2="B"> false </op></test>
<test><op name="equalsTopo" arg1="A" arg2="B"> false </op></test>
<test><op name="intersects" arg1="A" arg2="B"> true </op></test>
<test><op name="overlaps" arg1="A" arg2="B"> false </op></test>
<test><op name="touches" arg1="A" arg2="B"> false </op></test>
<test><op name="within" arg1="A" arg2="B"> true </op></test>

<test><op name="contains" arg1="B" arg2="A"> true </op></test>
<test><op name="coveredBy" arg1="B" arg2="A"> false </op></test>
<test><op name="covers" arg1="B" arg2="A"> true </op></test>
<test><op name="crosses" arg1="B" arg2="A"> false </op></test>
<test><op name="disjoint" arg1="B" arg2="A"> false </op></test>
<test><op name="equalsTopo" arg1="B" arg2="A"> false </op></test>
<test><op name="intersects" arg1="B" arg2="A"> true </op></test>
<test><op name="overlaps" arg1="B" arg2="A"> false </op></test>
<test><op name="touches" arg1="B" arg2="A"> false </op></test>
<test><op name="within" arg1="B" arg2="A"> false </op></test>
</case>

<case>
<!-- https://github.com/libgeos/geos/issues/1597 -->
<desc>GC:AL/A - polygon with line, equal line </desc>
<a>
GEOMETRYCOLLECTION(GEOMETRYCOLLECTION(
LINESTRING(0 0,10 10), POLYGON((-2 -1,4 -1,4 6,-2 6,-2 -1))))
</a>
<b>
LINESTRING(0 0,10 10)
</b>
<test><op name="relate" arg3="102001FF2" arg1="A" arg2="B"> true </op></test>
<test><op name="relate" arg3="10F00F212" arg1="B" arg2="A"> true </op></test>

<test><op name="contains" arg1="A" arg2="B"> true </op></test>
<test><op name="coveredBy" arg1="A" arg2="B"> false </op></test>
<test><op name="covers" arg1="A" arg2="B"> true </op></test>
<test><op name="crosses" arg1="A" arg2="B"> false </op></test>
<test><op name="disjoint" arg1="A" arg2="B"> false </op></test>
<test><op name="equalsTopo" arg1="A" arg2="B"> false </op></test>
<test><op name="intersects" arg1="A" arg2="B"> true </op></test>
<test><op name="overlaps" arg1="A" arg2="B"> false </op></test>
<test><op name="touches" arg1="A" arg2="B"> false </op></test>
<test><op name="within" arg1="A" arg2="B"> false </op></test>

<test><op name="contains" arg1="B" arg2="A"> false </op></test>
<test><op name="coveredBy" arg1="B" arg2="A"> true </op></test>
<test><op name="covers" arg1="B" arg2="A"> false </op></test>
<test><op name="crosses" arg1="B" arg2="A"> false </op></test>
<test><op name="disjoint" arg1="B" arg2="A"> false </op></test>
<test><op name="equalsTopo" arg1="B" arg2="A"> false </op></test>
<test><op name="intersects" arg1="B" arg2="A"> true </op></test>
<test><op name="overlaps" arg1="B" arg2="A"> false </op></test>
<test><op name="touches" arg1="B" arg2="A"> false </op></test>
<test><op name="within" arg1="B" arg2="A"> true </op></test>
</case>

</run>
Loading