[postgis-users] <-> operator does not return true distance

Jakob Miksch Jakob.Miksch at nina.no
Thu Jul 13 01:26:38 PDT 2017


Hi,

(I asked a similar question here https://gis.stackexchange.com/questions/247034/postgis-operator-does-not-return-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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20170713/9ccb9735/attachment.html>


More information about the postgis-users mailing list