I am developing an application using PostgreSQL as database; my goal is to archive polygon datas in geography format so i can visualize them easily using pgAdmin OpenStreetMap integration when i make some query. My problem is that when i try to show the distance in meters between a polygon settled approximately in the north pole and a point settled in Italy, the distance gives me a result of '0' and I know that a distance is zero when the polygon itself contains the point... but this is not our case. The code below is the query I used for this example:
SELECT
ST_Distance('POLYGON((88.20524 80.31286,89.30506 88.84312,83.62463 89.51824,88.20524 80.31286))'::geography,
ST_MakePoint(14.295750, 40.898207)::geography)
Can you help me to solve this problem? Thank you.
I believe PostGIS 3.0 is having some trouble with the amount of decimals you have in your coordinates.
Reduce the decimal precision by 1 digit, e.g. ST_MakePoint(14.29575, 40.89820)
, and it should work. It's not a solution I am very proud of, but assuming it is a bug in ST_Distance I believe it's an acceptable workaround.
SELECT
ST_Distance('POLYGON((88.20524 80.31286,89.30506 88.84312,83.62463 89.51824,88.20524 80.31286))'::geography,
ST_MakePoint(14.29575, 40.89820)::geography);
st_distance
------------------
5250149.46929165
(1 Zeile)
Testing envorinment
SELECT version();
version
---------------------------------------------------------------------------------------------------------------------------------------------
PostgreSQL 12.1 (Ubuntu 12.1-1.pgdg16.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609, 64-bit
(1 Zeile)
SELECT postgis_full_version();
postgis_full_version
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
POSTGIS="3.0.0 r17983" [EXTENSION] PGSQL="120" GEOS="3.7.1-CAPI-1.11.1 27a5e771" PROJ="Rel. 4.9.2, 08 September 2015" LIBXML="2.9.3" LIBJSON="0.11.99" LIBPROTOBUF="1.2.1" WAGYU="0.4.3 (Internal)"
(1 Zeile)
I'm trying to make sense out of this result set, but it seems that ST_Distance
cannot cope with a certain combination of decimal points between points and polygons. With the given polygon it won't work if the point has more than 5 decimal digits:
WITH j (p1,p2) AS (
VALUES ('POLYGON((88.20524 80.31286,89.30506 88.84312,83.62463 89.51824,88.20524 80.31286))',
'POINT(14.295750 40.898207)')
)
SELECT
ST_Distance(ST_AsText(p1,5)::GEOGRAPHY,
ST_AsText(p2,5)::GEOGRAPHY) AS five_digits,
ST_Distance(ST_AsText(p1,6)::GEOGRAPHY,
ST_AsText(p2,6)::GEOGRAPHY) AS six_digits
FROM j;
five_digits | six_digits
------------------+------------
5250148.38625588 | 0
(1 Zeile)
Further reading:
ST_AsText()
- max decimal digits