Opened 12 months ago

Closed 8 months ago

#5426 closed defect (duplicate)

ST_Difference produces wrong results for lines which are vertically / horizontally straight and completely contained in other input geometry

Reported by: 22141992 Owned by: pramsey
Priority: medium Milestone:
Component: postgis Version: 3.2.x
Keywords: ST_Difference Cc:

Description

ST_Difference is not working correctly in cases when one of the input geometries of ST_Difference is

  • vertically / horizontally straight line

AND

  • completely contained in other input geometry.

The bug has been detected when trying to determine difference between line geometry and polygon geometry, but it is likely that similar behaviour appears when comparing vertically / horizontally straight line geometry with another line geometry completely containing the first line).

It states in ST_Difference's documentation: "Returns a geometry representing the part of geometry A that does not intersect geometry B. This is equivalent to A - ST_Intersection(A,B). If A is completely contained in B then an empty atomic geometry of appropriate type is returned."

With that in mind, the second and third example below should return LINESTRING EMPTY (since the whole example line is completely contained in our example polygon, there should be no difference between them).

-- Polygon completely containing not vertially nor horizontally straight line
SELECT ST_AsText(ST_Difference('LINESTRING (5 7, 6 11)'::geometry, 'POLYGON ((0 0, 1000 0, 1000 1000, 0 1000, 0 0))'::geometry));
-- LINESTRING EMPTY -> CORRECT RESULT
-- Polygon completely containing vertically straight line
SELECT AsText(ST_Difference('LINESTRING (5 7, 5 11)'::geometry, 'POLYGON ((0 0, 1000 0, 1000 1000, 0 1000, 0 0))'::geometry));
-- LINESTRING(5 7,5 11)	-> INCORRECT RESULT
-- Polygon completely containing horizontally straight line
SELECT ST_AsText(ST_Difference('LINESTRING (5 7, 10 7)'::geometry, 'POLYGON ((0 0, 1000 0, 1000 1000, 0 1000, 0 0))'::geometry));
-- LINESTRING(5 7,10 7)-> INCORRECT RESULT
-- Polygon partly containing vertically straight line
SELECT ST_AsText(ST_Difference('LINESTRING(50 100, 50 200)'::geometry, 'POLYGON((0 0, 0 150, 150 150, 150 0, 0 0))'::geometry));
-- LINESTRING(50 150,50 200) -> CORRECT RESULT
-- Polygon completely containing the interior of the vertically straight line, end point of the line intersects with polygon boundary
SELECT ST_AsText(ST_Difference('LINESTRING(50 100, 50 150)'::geometry, 'POLYGON((0 0, 0 150, 150 150, 150 0, 0 0))'::geometry));
-- LINESTRING EMPTY -> CORRECT RESULT

Change History (4)

comment:1 by mdavis, 12 months ago

What PostGIS full version is showing this error?

comment:2 by lkajan, 8 months ago

Confirmed on: POSTGIS="3.4.0 0874ea3" [EXTENSION] PGSQL="160" GEOS="3.9.0-CAPI-1.16.2" PROJ="7.2.1 NETWORK_ENABLED=OFF URL_ENDPOINT=https://cdn.proj.org USER_WRITABLE_DIRECTORY=/var/lib/postgresql/.local/share/proj DATABASE_PATH=/usr/share/proj/proj.db" LIBXML="2.9.10" LIBJSON="0.15" LIBPROTOBUF="1.3.3" WAGYU="0.5.0 (Internal)" TOPOLOGY

comment:3 by mdavis, 8 months ago

This is a GEOS problem that was fixed in GEOS 3.10.

comment:4 by mdavis, 8 months ago

Resolution: duplicate
Status: newclosed
Note: See TracTickets for help on using tickets.