如何在postgres / postgis中空间连接2个点图层

时间:2015-01-22 06:25:14

标签: postgresql postgis

我试图使用空间连接加入极点(点)和客户(点)图层。我在杆周围创建了一个20米长的缓冲区,然后写下面的查询。这需要花费太多时间

SELECT  distinct h.gid, d.polecode FROM  
buildings as h  left join   
pole_buffer_20 as d
on
ST_Intersects(d.the_geom,h.the_geom)

在不使用缓冲的情况下帮助我查询

1 个答案:

答案 0 :(得分:0)

为了检查多边形中是否包含点,您应该使用更快的函数ST_DWithin

SELECT  distinct h.gid, d.polecode FROM  
buildings as h  left join   
pole_buffer_20 as d
on
ST_DWithin(d.the_geom,h.the_geom, 0);

ST_DWithin也接受一个距离参数,因此,根据您的需要,您可以避免使用缓冲表并直接使用原始表:

SELECT  distinct h.gid, d.polecode FROM  
buildings as h  left join   
pole as d
on
ST_DWithin(d.the_geom,h.the_geom, 20);

距离参数以几何为单位。如果您希望搜索半径为20米,则需要以米为单位表示的几何图形(或使用地理类型)