geos icon indicating copy to clipboard operation
geos copied to clipboard

Contains gives the error answer, but ContainsPrep gives the correct one

Open cuteDen-ECNU opened this issue 1 year ago • 3 comments

Consider the statement:

DROP TABLE IF EXISTS t0;
CREATE TABLE t0 (id int, geom geometry, valid boolean);
INSERT INTO t0 (id, geom) VALUES (1,ST_GeomFromText('LINESTRING(1 2,1 1)',0));
INSERT INTO t0 (id, geom) VALUES (2,ST_GeomFromText('GEOMETRYCOLLECTION(POINT(2 2),POINT(1 0),LINESTRING(1 2,1 1))',0));
INSERT INTO t0 (id, geom) VALUES (3,ST_GeomFromText('POLYGON((1 0,0 4,2 2,1 0))',0));
SELECT a1.id, a2.id FROM t0 As a1 JOIN t0 As a2 ON ST_Contains(a1.geom, a2.geom);      
-- result: {1,1; 2,1; 2,2; 3,1; 3,2; 3, 3}   
SELECT  ST_Contains(a1.geom, a2.geom) FROM t0 as a1, t0 as a2 WHERE a1.id = 3 and a2.id = 2;
-- expected: true; actual: false

g3 contains g2 because POINTs of g2 lie in the boundary of g3 and LINESTRING of g2 lies in the interior of g3, which satisfied the sematics of contains:

ST_Contains(A, B) ⇔ (A ⋂ B = B) ∧ (Int(A) ⋂ Int(B) ≠ ∅)

I consider the issue different from #982 because the boundary of g3 is not ambiguous.

The predicate of ContainsPrep gives the correct answer. A simple statement reproduced in Geos:

bin/geosop -a 'POLYGON((1 0,0 4,2 2,1 0))' -b 'GEOMETRYCOLLECTION(POINT(2 2),POINT(1 0),LINESTRING(1 2,1 1))' contains
# false
bin/geosop -a 'POLYGON((1 0,0 4,2 2,1 0))' -b 'GEOMETRYCOLLECTION(POINT(2 2),POINT(1 0),LINESTRING(1 2,1 1))' containsPrep
# true

The version of Geos is the latest one: https://github.com/libgeos/geos/commit/6f70b63a0d976ce31dd2952094bc1a39209f8a20

cuteDen-ECNU avatar Jan 25 '24 06:01 cuteDen-ECNU

Interesting, same result in covers/coversPrep.

pramsey avatar Feb 08 '24 20:02 pramsey

This is a problem relatively deep in the IM calculation, so we are going to instead focus on the RelateNG work, which hopefully removes this inconsistency in future releases (and is faster).

pramsey avatar Feb 12 '24 19:02 pramsey

Looking forward to the new releases! 😊

cuteDen-ECNU avatar Feb 15 '24 05:02 cuteDen-ECNU