<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:0cm;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;
mso-fareast-language:EN-US;}
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-compose;
font-family:"Calibri",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri",sans-serif;
mso-fareast-language:EN-US;}
@page WordSection1
{size:612.0pt 792.0pt;
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="NO-BOK" link="#0563C1" vlink="#954F72">
<div class="WordSection1">
<p class="MsoNormal"><span lang="EN-GB">Hi, <o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">(I asked a similar question here https://gis.stackexchange.com/questions/247034/postgis-operator-does-not-return-true-distance )<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><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"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">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".
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><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"><span lang="EN-GB">( illustration: https://i.stack.imgur.com/QZA6D.png )<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">SELECT id,<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"> ST_Distance(my_point.geom, <o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"> my_polygons.geom) AS real_distance,<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"> ST_Distance(my_point.geom, <o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"> ST_Centroid(my_polygons.geom)) AS centroid_distance<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">FROM (<o:p></o:p></span></p>
<p class="MsoNormal"><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"><span lang="EN-GB"> UNION<o:p></o:p></span></p>
<p class="MsoNormal"><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"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">) AS my_polygons,<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">(<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"> SELECT ST_SetSRID(ST_Point(1, 1),4326) AS geom<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">) AS my_point<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">ORDER BY my_point.geom <-> my_polygons.geom
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">--- obtained Result:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">| | real_distance | centroid_distance |<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">---------------------------------------------<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">| blue | 2 | 2.5 |<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">| green | 1 | 4 |<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB">(I expected the rows to be in the opposite order)<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><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"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><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"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><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"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal"><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"><span lang="EN-GB"><o:p> </o:p></span></p>
<p class="MsoNormal">Thanks and best regards,<o:p></o:p></p>
<p class="MsoNormal">Jakob<o:p></o:p></p>
</div>
</body>
</html>