Opened 6 weeks ago

Last modified 6 weeks ago

#5740 new defect

ST_DistanceSpheroid doesn't return 0 when one geometry contains the other

Reported by: cdestigter Owned by: pramsey
Priority: medium Milestone: PostGIS 3.4.3
Component: postgis Version: 3.4.x
Keywords: Cc:

Description (last modified by cdestigter)

The docs say

Returns minimum distance in meters between two lon/lat geometries given a particular spheroid

Based on this description it seems that any intersection between the two geometries should cause the result to be 0 (the minimum distance between the geometries is 0 because they overlap)

However, it seems that actually compares the _edges_ of the geometries - if one geometry contains the other you get a positive number:

-- touching the edge of the polygon, the result is zero
=> select st_distancespheroid('SRID=4326;POLYGON((0 0,0 1,1 1,1 0,0 0))'::GEOMETRY, 'SRID=4326;POINT(0 0.5)'::geometry);

0


-- close to the edge, the result is nonzero
=> select st_distancespheroid('SRID=4326;POLYGON((0 0,0 1,1 1,1 0,0 0))'::GEOMETRY, 'SRID=4326;POINT(0.1 0.5)'::geometry);

11131.528045775543


-- the further you get from the edge, the larger the result
=> select st_distancespheroid('SRID=4326;POLYGON((0 0,0 1,1 1,1 0,0 0))'::GEOMETRY, 'SRID=4326;POINT(0.5 0.5)'::geometry);

55657.640177233385

Interestingly, ST_DistanceSphere gives the expected result:

=> select st_distancesphere('SRID=4326;POLYGON((0 0,0 1,1 1,1 0,0 0))'::GEOMETRY, 'SRID=4326;POINT(0.5 0.5)'::geometry);

0

Change History (2)

comment:1 by cdestigter, 6 weeks ago

Description: modified (diff)

comment:2 by cdestigter, 6 weeks ago

I've had a browse around the code, and it seems like the bug is here - the code checks for edge intersections and returns 0 if there are any but it doesn't check for containment.

I'm not sure how ST_DistanceSphere avoids the issue since it looks like it probably goes down a similar codepath

Version 0, edited 6 weeks ago by cdestigter (next)
Note: See TracTickets for help on using tickets.