Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#4223 closed defect (fixed)

ST_Distance returns 0 instead of 5

Reported by: br0ke Owned by: pramsey
Priority: medium Milestone: PostGIS 2.5.1
Component: postgis Version: 2.5.x -- EOL
Keywords: Cc:

Description

Hey!

ST_Distance function returns incorrect value but _ST_DistanceUncached returns correct. And it's not a rare, weird case: I have two simple rectangles, distance between is 5 meters.

https://image.ibb.co/cryRL0/Screenshot-from-2018-11-01-13-10-10.png

My query (also can be found here https://pastebin.com/QTfV19vh):

SELECT
	ST_Distance(f1.geog, f2.geog),
	_ST_DistanceTree(f1.geog, f2.geog),
	_ST_DistanceUncached(f1.geog, f2.geog)
FROM
	LATERAL (
		SELECT ST_Transform(
			ST_SetSRID(
				ST_GeomFromGeoJson('{"type": "Polygon", "coordinates": [[[-5, 0], [-5, 100], [5, 100], [5, 0], [-5, 0]]]}'),
				32641
			),
			4326
		)::geography AS geog
	) AS f1,
	LATERAL (
		SELECT ST_Transform(
			ST_SetSRID(
				ST_GeomFromGeoJson('{"type": "Polygon", "coordinates": [[[-10, 30], [-10, 40], [-20, 40], [-20, 30], [-10, 30]]]}'),
				32641
			),
			4326
		)::geography AS geog
	) AS f2,
	generate_series(1, 10);

Output:

 st_distance | _st_distancetree | _st_distanceuncached 
-------------+------------------+----------------------
           0 |                0 |     4.98653949096524
           0 |                0 |     4.98653949096524
           0 |                0 |     4.98653949096524
           0 |                0 |     4.98653949096524
           0 |                0 |     4.98653949096524
           0 |                0 |     4.98653949096524
           0 |                0 |     4.98653949096524
           0 |                0 |     4.98653949096524
           0 |                0 |     4.98653949096524
           0 |                0 |     4.98653949096524

My PostGIS version:

POSTGIS="2.5.0 r16836" [EXTENSION] PGSQL="100" GEOS="3.5.1-CAPI-1.9.1 r4246" PROJ="Rel. 4.9.3, 15 August 2016" GDAL="GDAL 2.1.2, released 2016/10/24" LIBXML="2.9.4" LIBJSON="0.12.1" LIBPROTOBUF="1.2.1" TOPOLOGY RASTER

My PostgreSQL version:

PostgreSQL 10.5 (Debian 10.5-2.pgdg90+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516, 64-bit

Change History (6)

comment:1 by pramsey, 6 years ago

Good ticket. Can reproduce and appreciate the terse example.

Last edited 6 years ago by pramsey (previous) (diff)

comment:2 by pramsey, 6 years ago

WITH g AS (
SELECT 
  'POLYGON((58.5112113206308 0,58.5112113200772 0.000901937525203378,58.511300910044 0.000901937636668872,58.5113009105976 0,58.5112113206308 0))'::geography AS a,
  'POLYGON((58.5111665256017 0.000270581240841207,58.5111665255629 0.000360774987788249,58.5110769356128 0.000360774943200728,58.5110769356515 0.000270581207400566,58.5111665256017 0.000270581240841207))'::geography AS b
 )
 SELECT _ST_DistanceTree(a, b), _ST_DistanceUncached(a, b)
 FROM g;
Last edited 6 years ago by pramsey (previous) (diff)

comment:3 by pramsey, 6 years ago

Resolution: fixed
Status: newclosed

In 16979:

Patch over bad behaviour for near-parallel boxes and distancetree.
Unfortunately, this "fix" is probably breakable, just put the boxes closer and closer together until we get "close enough to be the same" results from the edge normal comparison again. Only way around this is to do dot product and comparisons in higher-than-double space.
Reworking this example with even closer together inputs would be useful for exposing this problem for eventual complete fix.
Closes #4223

comment:4 by pramsey, 6 years ago

In 16980:

Patch over bad behaviour for near-parallel boxes and distancetree.
Unfortunately, this "fix" is probably breakable, just put the boxes closer and closer together until we get "close enough to be the same" results from the edge normal comparison again. Only way around this is to do dot product and comparisons in higher-than-double space
References #4223

comment:5 by pramsey, 6 years ago

In 16981:

Patch over bad behaviour for near-parallel boxes and distancetree.
Unfortunately, this "fix" is probably breakable, just put the boxes closer and closer together until we get "close enough to be the same" results from the edge normal comparison again. Only way around this is to do dot product and comparisons in higher-than-double space
References #4223

comment:6 by pramsey, 6 years ago

In 16993:

Patch over bad behaviour for near-parallel boxes and distancetree.

Unfortunately, this "fix" is probably breakable, just put the boxes closer and closer together until we get "close enough to be the same" results from the edge normal comparison again. Only way around this is to do dot product and comparisons in higher-than-double space
References #4223

Note: See TracTickets for help on using tickets.