K-Nearest Neighbor Query in PostGIS

后端 未结 5 527
伪装坚强ぢ
伪装坚强ぢ 2020-12-15 08:15

I am using the following Nearest Neighbor Query in PostGIS :

SELECT g1.gid g2.gid FROM points as g1, polygons g2   
WHERE g1.gid <> g2.gid
ORDER BY g1.         


        
5条回答
  •  感情败类
    2020-12-15 08:39

    Since late September 2011, PostGIS has supported indexed nearest neighbor queries via a special operator(s) usable in the ORDER BY clause:

    SELECT name, gid
    FROM geonames
    ORDER BY geom <-> st_setsrid(st_makepoint(-90,40),4326)
    LIMIT 10;
    

    ...will return the 10 objects whose geom is nearest -90,40 in a scalable way. A few more details (options and caveats) are in that announcement post and use of the <-> and the <#> operators is also now documented in the official PostGIS 2.0 reference. (The main difference between the two is that <-> compares the shape centroids and <#> compares their boundaries — no difference for points, other shapes choose what is appropriate for your queries.)

提交回复
热议问题