K-Nearest Neighbor Query in PostGIS

后端 未结 5 552
伪装坚强ぢ
伪装坚强ぢ 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:27

    You can do it with KNN index and lateral join.

    SELECT v.gid, v2.gid,st_distance(v.the_geom, v2.the_geom)
      FROM geonames v, 
           lateral(select * 
                     from geonames v2
                    where v2.id<>v.id
                    ORDER BY v.the_geom <-> v2.the_geom LIMIT 10) v2
    where v.gid in (...) - or other filtering condition
    

提交回复
热议问题