PostGIS minimum distance between two large sets of points

后端 未结 1 1147

I have two tables of points in PostGIS, say A and B, and I want to know, for every point in A, what is the distance to the closest point in B. I am able to solve this for sm

相关标签:
1条回答
  • 2020-12-11 09:19

    Your query is slow because it computes the distance between every points without using any index. You could rewrite it to use the <-> operator that uses the index if used in the order by clause.

    select a.id,closest_pt.id, closest_pt.dist
    from tablea a
    CROSS JOIN LATERAL
      (SELECT
         id , 
         a.geom <-> b.geom as dist
         FROM tableb b
         ORDER BY a.geom <-> b.geom
       LIMIT 1) AS closest_pt;
    
    0 讨论(0)
提交回复
热议问题