[prev in list] [next in list] [prev in thread] [next in thread]
List: postgis-users
Subject: Re: [postgis-users] <-> operator does not return true distance
From: "Regina Obe" <lr () pcorp ! us>
Date: 2017-07-13 20:50:56
Message-ID: 005d01d2fc19$b9dc2500$2d946f00$ () pcorp ! us
[Download RAW message or body]
This is a multipart message in MIME format.
[Attachment #2 (multipart/alternative)]
This is a multipart message in MIME format.
Jakob,
Did you upgrade your install from an older version say 9.4.
I think if you did a pg_upgrade, and didn't do a ALTER EXTENSION postgis.
after upgrade, this would explain your issue since the operators would not
get upgraded.
On my PostGIS 2.3.2, 9.6 behavior is as expected. Unfortunately I don't
have 2.2.2 lying around at moment.
Also there was an ordering bug I think in 2.2 early version that may have
caused this. If that is your issue, then output of <-> would be real even
though ordering is wrong.
Try this query:
SELECT id,
ST_Distance(my_point.geom,
my_polygons.geom) AS real_distance,
ST_Distance(my_point.geom,
ST_Centroid(my_polygons.geom)) AS centroid_distance,
my_point.geom <-> my_polygons.geom
FROM (
SELECT 'blue' AS id, ST_Polygon(ST_GeomFromText('LINESTRING(0 3, 2 3, 2
4, 0 4 ,0 3)'),4326) AS geom
UNION
SELECT 'green' AS id, ST_Polygon(ST_GeomFromText('LINESTRING(2 0, 8 0 ,
8 2, 2 2 ,2 0)'),4326) AS geom
) AS my_polygons,
(
SELECT ST_SetSRID(ST_Point(1, 1),4326) AS geom
) AS my_point
ORDER BY my_point.geom <-> my_polygons.geom
-- output should be --
id | real_distance | centroid_distance | ?column?
-------+---------------+-------------------+----------
green | 1 | 4 | 1
blue | 2 | 2.5 | 2
(2 rows)
Hope that helps,
Regina
http://postgis.us
http://www.paragoncorporation.com
From: postgis-users [mailto:postgis-users-bounces@lists.osgeo.org] On Behalf
Of Jakob Miksch
Sent: Thursday, July 13, 2017 4:27 AM
To: postgis-users@lists.osgeo.org
Subject: [postgis-users] <-> operator does not return true distance
Hi,
(I asked a similar question here
https://gis.stackexchange.com/questions/247034/postgis-operator-does-not-ret
urn-true-distance )
I used the <-> operator with PostGIS 2.2.2 and PostgreSQL 9.5.7 .
According to the docs
(http://postgis.net/docs/manual-2.2/geometry_distance_knn.html) the <->
operator should order by "true KNN distance" and *not* by "centroid
distance".
However it does not work for me. See the minimal example below:
( illustration: https://i.stack.imgur.com/QZA6D.png )
SELECT id,
ST_Distance(my_point.geom,
my_polygons.geom) AS real_distance,
ST_Distance(my_point.geom,
ST_Centroid(my_polygons.geom)) AS centroid_distance
FROM (
SELECT 'blue' AS id, ST_Polygon(ST_GeomFromText('LINESTRING(0 3, 2 3, 2
4, 0 4 ,0 3)'),4326) AS geom
UNION
SELECT 'green' AS id, ST_Polygon(ST_GeomFromText('LINESTRING(2 0, 8 0 ,
8 2, 2 2 ,2 0)'),4326) AS geom
) AS my_polygons,
(
SELECT ST_SetSRID(ST_Point(1, 1),4326) AS geom
) AS my_point
ORDER BY my_point.geom <-> my_polygons.geom
--- obtained Result:
| | real_distance | centroid_distance |
---------------------------------------------
| blue | 2 | 2.5 |
| green | 1 | 4 |
(I expected the rows to be in the opposite order)
Apparently the <-> operator orders by the centroid_distance and not by the
real_distance, even though the docs say: "[...] for PostgreSQL 9.5+, does
true KNN distance search giving true distance between geometries [...]"
I tried the same example on 2.3.2 and PostgreSQL 9.5.7 and I get the
expected result (which orders by "real_distance").
So, for me it seems that ordering by "real_distance" with the <-> operator
does not work with PostGIS 2.2.2 - but this is *not* written in the docs.
Can you reproduce this behaviour? Or did I understand/make something wrong
here?
Thanks and best regards,
Jakob
[Attachment #5 (text/html)]
<html xmlns:v="urn:schemas-microsoft-com:vml" \
xmlns:o="urn:schemas-microsoft-com:office:office" \
xmlns:w="urn:schemas-microsoft-com:office:word" \
xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" \
xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type \
content="text/html; charset=us-ascii"><meta name=Generator content="Microsoft Word 15 \
(filtered medium)"><style><!-- /* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:#0563C1;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:#954F72;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal;
font-family:"Calibri",sans-serif;
color:windowtext;}
span.EmailStyle18
{mso-style-type:personal-reply;
font-family:"Calibri",sans-serif;
color:#1F497D;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;}
@page WordSection1
{size:8.5in 11.0in;
margin:70.85pt 70.85pt 70.85pt 70.85pt;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-US link="#0563C1" \
vlink="#954F72"><div class=WordSection1><p class=MsoNormal><span \
style='color:#1F497D'>Jakob,<o:p></o:p></span></p><p class=MsoNormal><span \
style='color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span \
style='color:#1F497D'>Did you upgrade your install from an older version say \
9.4.<o:p></o:p></span></p><p class=MsoNormal><span \
style='color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span \
style='color:#1F497D'>I think if you did a pg_upgrade, and didn't do a ALTER \
EXTENSION postgis… after upgrade, this would explain your issue since the \
operators would not get upgraded.<o:p></o:p></span></p><p class=MsoNormal><span \
style='color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span \
style='color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span \
style='color:#1F497D'>On my PostGIS 2.3.2, 9.6 behavior is as expected. \
Unfortunately I don't have 2.2.2 lying around at moment.<o:p></o:p></span></p><p \
class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p><p \
class=MsoNormal><span style='color:#1F497D'>Also there was an ordering bug I think in \
2.2 early version that may have caused this. If that is your issue, then output \
of <-> would be real even though ordering is wrong.<o:p></o:p></span></p><p \
class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p><p \
class=MsoNormal><span style='color:#1F497D'>Try this query:<o:p></o:p></span></p><p \
class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p><p \
class=MsoNormal><span style='color:#1F497D'>SELECT id,<o:p></o:p></span></p><p \
class=MsoNormal><span style='color:#1F497D'> \
ST_Distance(my_point.geom, <o:p></o:p></span></p><p class=MsoNormal><span \
style='color:#1F497D'> my_polygons.geom) \
AS real_distance,<o:p></o:p></span></p><p class=MsoNormal><span \
style='color:#1F497D'> \
ST_Distance(my_point.geom, <o:p></o:p></span></p><p class=MsoNormal><span \
style='color:#1F497D'> ST_Centroid(my_polygons.geom)) \
AS centroid_distance, my_point.geom <-> my_polygons.geom \
<o:p></o:p></span></p><p class=MsoNormal><span \
style='color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span \
style='color:#1F497D'>FROM (<o:p></o:p></span></p><p class=MsoNormal><span \
style='color:#1F497D'> SELECT 'blue' AS id, \
ST_Polygon(ST_GeomFromText('LINESTRING(0 3, 2 3, 2 4, 0 4 ,0 3)'),4326) AS \
geom<o:p></o:p></span></p><p class=MsoNormal><span \
style='color:#1F497D'> UNION<o:p></o:p></span></p><p \
class=MsoNormal><span style='color:#1F497D'> SELECT 'green' AS \
id, ST_Polygon(ST_GeomFromText('LINESTRING(2 0, 8 0 , 8 2, 2 2 ,2 0)'),4326) AS \
geom<o:p></o:p></span></p><p class=MsoNormal><span \
style='color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span \
style='color:#1F497D'>) AS my_polygons,<o:p></o:p></span></p><p class=MsoNormal><span \
style='color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span \
style='color:#1F497D'>(<o:p></o:p></span></p><p class=MsoNormal><span \
style='color:#1F497D'> SELECT ST_SetSRID(ST_Point(1, 1),4326) AS \
geom<o:p></o:p></span></p><p class=MsoNormal><span style='color:#1F497D'>) AS \
my_point<o:p></o:p></span></p><p class=MsoNormal><span \
style='color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span \
style='color:#1F497D'>ORDER BY my_point.geom <-> \
my_polygons.geom<o:p></o:p></span></p><p class=MsoNormal><span \
style='color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span \
style='color:#1F497D'>-- output should be --<o:p></o:p></span></p><p \
class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p><p \
class=MsoNormal><span style='color:#1F497D'> id | real_distance | \
centroid_distance | ?column?<o:p></o:p></span></p><p class=MsoNormal><span \
style='color:#1F497D'>-------+---------------+-------------------+----------<o:p></o:p></span></p><p \
class=MsoNormal><span style='color:#1F497D'> green \
| 1 \
| \
4 | 1<o:p></o:p></span></p><p \
class=MsoNormal><span style='color:#1F497D'> blue \
| 2 \
| \
2.5 | 2<o:p></o:p></span></p><p \
class=MsoNormal><span style='color:#1F497D'>(2 rows)<o:p></o:p></span></p><p \
class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p><p \
class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p><p \
class=MsoNormal><span style='color:#1F497D'>Hope that helps,<o:p></o:p></span></p><p \
class=MsoNormal><span style='color:#1F497D'>Regina<o:p></o:p></span></p><p \
class=MsoNormal><span style='color:#1F497D'><a \
href="http://postgis.us">http://postgis.us</a><o:p></o:p></span></p><p \
class=MsoNormal><span style='color:#1F497D'><a \
href="http://www.paragoncorporation.com">http://www.paragoncorporation.com</a><o:p></o:p></span></p><p \
class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p><div><div \
style='border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in'><p \
class=MsoNormal style='margin-left:.5in'><b>From:</b> postgis-users \
[mailto:postgis-users-bounces@lists.osgeo.org] <b>On Behalf Of </b>Jakob \
Miksch<br><b>Sent:</b> Thursday, July 13, 2017 4:27 AM<br><b>To:</b> \
postgis-users@lists.osgeo.org<br><b>Subject:</b> [postgis-users] <-> operator \
does not return true distance<o:p></o:p></p></div></div><p class=MsoNormal \
style='margin-left:.5in'><o:p> </o:p></p><p class=MsoNormal \
style='margin-left:.5in'><span lang=EN-GB>Hi, <o:p></o:p></span></p><p \
class=MsoNormal style='margin-left:.5in'><span \
lang=EN-GB><o:p> </o:p></span></p><p class=MsoNormal \
style='margin-left:.5in'><span lang=EN-GB>(I asked a similar question here <a \
href="https://gis.stackexchange.com/questions/247034/postgis-operator-does-not-return- \
true-distance">https://gis.stackexchange.com/questions/247034/postgis-operator-does-not-return-true-distance</a> \
)<o:p></o:p></span></p><p class=MsoNormal style='margin-left:.5in'><span \
lang=EN-GB><o:p> </o:p></span></p><p class=MsoNormal \
style='margin-left:.5in'><span lang=EN-GB>I used the <-> operator with PostGIS \
2.2.2 and PostgreSQL 9.5.7 .<o:p></o:p></span></p><p class=MsoNormal \
style='margin-left:.5in'><span lang=EN-GB><o:p> </o:p></span></p><p \
class=MsoNormal style='margin-left:.5in'><span lang=EN-GB>According to the docs (<a \
href="http://postgis.net/docs/manual-2.2/geometry_distance_knn.html">http://postgis.net/docs/manual-2.2/geometry_distance_knn.html</a>) \
the <-> operator should order by "true KNN distance" and *not* by \
"centroid distance". <o:p></o:p></span></p><p class=MsoNormal \
style='margin-left:.5in'><span lang=EN-GB><o:p> </o:p></span></p><p \
class=MsoNormal style='margin-left:.5in'><span lang=EN-GB>However it does not work \
for me. See the minimal example below: <o:p></o:p></span></p><p class=MsoNormal \
style='margin-left:.5in'><span lang=EN-GB>( illustration: <a \
href="https://i.stack.imgur.com/QZA6D.png">https://i.stack.imgur.com/QZA6D.png</a> \
)<o:p></o:p></span></p><p class=MsoNormal style='margin-left:.5in'><span \
lang=EN-GB><o:p> </o:p></span></p><p class=MsoNormal \
style='margin-left:.5in'><span lang=EN-GB>SELECT id,<o:p></o:p></span></p><p \
class=MsoNormal style='margin-left:.5in'><span \
lang=EN-GB> ST_Distance(my_point.geom, \
<o:p></o:p></span></p><p class=MsoNormal style='margin-left:.5in'><span \
lang=EN-GB> my_polygons.geom) \
AS real_distance,<o:p></o:p></span></p><p class=MsoNormal \
style='margin-left:.5in'><span lang=EN-GB> \
ST_Distance(my_point.geom, <o:p></o:p></span></p><p class=MsoNormal \
style='margin-left:.5in'><span \
lang=EN-GB> ST_Centroid(my_polygons.geom)) \
AS centroid_distance<o:p></o:p></span></p><p class=MsoNormal \
style='margin-left:.5in'><span lang=EN-GB><o:p> </o:p></span></p><p \
class=MsoNormal style='margin-left:.5in'><span lang=EN-GB>FROM \
(<o:p></o:p></span></p><p class=MsoNormal style='margin-left:.5in'><span \
lang=EN-GB> SELECT 'blue' AS id, \
ST_Polygon(ST_GeomFromText('LINESTRING(0 3, 2 3, 2 4, 0 4 ,0 3)'),4326) AS \
geom<o:p></o:p></span></p><p class=MsoNormal style='margin-left:.5in'><span \
lang=EN-GB> UNION<o:p></o:p></span></p><p class=MsoNormal \
style='margin-left:.5in'><span lang=EN-GB> SELECT 'green' AS \
id, ST_Polygon(ST_GeomFromText('LINESTRING(2 0, 8 0 , 8 2, 2 2 ,2 0)'),4326) AS \
geom<o:p></o:p></span></p><p class=MsoNormal style='margin-left:.5in'><span \
lang=EN-GB><o:p> </o:p></span></p><p class=MsoNormal \
style='margin-left:.5in'><span lang=EN-GB>) AS my_polygons,<o:p></o:p></span></p><p \
class=MsoNormal style='margin-left:.5in'><span \
lang=EN-GB><o:p> </o:p></span></p><p class=MsoNormal \
style='margin-left:.5in'><span lang=EN-GB>(<o:p></o:p></span></p><p class=MsoNormal \
style='margin-left:.5in'><span lang=EN-GB> SELECT \
ST_SetSRID(ST_Point(1, 1),4326) AS geom<o:p></o:p></span></p><p class=MsoNormal \
style='margin-left:.5in'><span lang=EN-GB>) AS my_point<o:p></o:p></span></p><p \
class=MsoNormal style='margin-left:.5in'><span \
lang=EN-GB><o:p> </o:p></span></p><p class=MsoNormal \
style='margin-left:.5in'><span lang=EN-GB>ORDER BY my_point.geom <-> \
my_polygons.geom <o:p></o:p></span></p><p class=MsoNormal \
style='margin-left:.5in'><span lang=EN-GB><o:p> </o:p></span></p><p \
class=MsoNormal style='margin-left:.5in'><span \
lang=EN-GB><o:p> </o:p></span></p><p class=MsoNormal \
style='margin-left:.5in'><span lang=EN-GB>--- obtained \
Result:<o:p></o:p></span></p><p class=MsoNormal style='margin-left:.5in'><span \
lang=EN-GB><o:p> </o:p></span></p><p class=MsoNormal \
style='margin-left:.5in'><span lang=EN-GB><o:p> </o:p></span></p><p \
class=MsoNormal style='margin-left:.5in'><span \
lang=EN-GB>| | real_distance | centroid_distance \
|<o:p></o:p></span></p><p class=MsoNormal style='margin-left:.5in'><span \
lang=EN-GB>---------------------------------------------<o:p></o:p></span></p><p \
class=MsoNormal style='margin-left:.5in'><span lang=EN-GB>| blue \
| 2 \
| \
2.5 |<o:p></o:p></span></p><p \
class=MsoNormal style='margin-left:.5in'><span lang=EN-GB>| green \
| 1 \
| \
4 |<o:p></o:p></span></p><p \
class=MsoNormal style='margin-left:.5in'><span \
lang=EN-GB><o:p> </o:p></span></p><p class=MsoNormal \
style='margin-left:.5in'><span lang=EN-GB>(I expected the rows to be in the opposite \
order)<o:p></o:p></span></p><p class=MsoNormal style='margin-left:.5in'><span \
lang=EN-GB><o:p> </o:p></span></p><p class=MsoNormal \
style='margin-left:.5in'><span lang=EN-GB>Apparently the <-> operator orders by \
the centroid_distance and not by the real_distance, even though the docs say: \
"[...] for PostgreSQL 9.5+, does true KNN distance search giving true distance \
between geometries [...]"<o:p></o:p></span></p><p class=MsoNormal \
style='margin-left:.5in'><span lang=EN-GB><o:p> </o:p></span></p><p \
class=MsoNormal style='margin-left:.5in'><span lang=EN-GB>I tried the same example on \
2.3.2 and PostgreSQL 9.5.7 and I get the expected result (which orders by \
"real_distance").<o:p></o:p></span></p><p class=MsoNormal \
style='margin-left:.5in'><span lang=EN-GB><o:p> </o:p></span></p><p \
class=MsoNormal style='margin-left:.5in'><span lang=EN-GB>So, for me it seems that \
ordering by "real_distance" with the <-> operator does not work with \
PostGIS 2.2.2 - but this is *not* written in the docs.<o:p></o:p></span></p><p \
class=MsoNormal style='margin-left:.5in'><span \
lang=EN-GB><o:p> </o:p></span></p><p class=MsoNormal \
style='margin-left:.5in'><span lang=EN-GB>Can you reproduce this behaviour? Or did I \
understand/make something wrong here?<o:p></o:p></span></p><p class=MsoNormal \
style='margin-left:.5in'><span lang=EN-GB><o:p> </o:p></span></p><p \
class=MsoNormal style='margin-left:.5in'><span lang=NO-BOK>Thanks and best \
regards,<o:p></o:p></span></p><p class=MsoNormal style='margin-left:.5in'><span \
lang=NO-BOK>Jakob<o:p></o:p></span></p></div></body></html>
[Attachment #6 (text/plain)]
_______________________________________________
postgis-users mailing list
postgis-users@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/postgis-users
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic