检索给定区域内“线串”子段的长度(使用ST_DWithin)

时间:2019-04-04 15:54:28

标签: postgis openstreetmap

我需要检索一个圆内的线串几何对象的长度(点+半径)。

我当前正在使用ST_DWithin函数,但不幸的是,它返回完整的对象,而不是区域内的对象。实际上,将半径(从100更改为70)不会改变检索到的长度。

osm_data=# select highway, st_length(way) from planet_osm_line where ST_DWITHIN(ST_Transform(way,4326),ST_GeomFromText('POINT(4.1884918 51.144580)',4326)::geography,100) and highway is not null;
   highway    |    st_length     
--------------+------------------
 motorway     | 5079.24632083105
 unclassified | 1110.56834915499
 motorway     | 1499.83459080537
(3 rows)

osm_data=# select highway, st_length(way) from planet_osm_line where ST_DWITHIN(ST_Transform(way,4326),ST_GeomFromText('POINT(4.1884918 51.144580)',4326)::geography,70) and highway is not null;
   highway    |    st_length     
--------------+------------------
 motorway     | 5079.24632083105
 unclassified | 1110.56834915499
 motorway     | 1499.83459080537
(3 rows)

1 个答案:

答案 0 :(得分:0)

ST_DWithin用于选择距离参考点至少在所述距离之内的几何形状。

要计算其中的实际长度(或多边形的面积),您需要计算交点。

select highway, 
   st_length(way) full_length,   
   st_length(
     ST_INTERSECTION(
        ST_Transform(way,4326)::geography,
        ST_BUFFER(
          ST_GeomFromText('POINT(4.1884918 51.144580)',4326)::geography,
         100)
      )
   )
  from planet_osm_line 
  where
    ST_DWITHIN(
      ST_Transform(way,4326)::geography,
      ST_GeomFromText('POINT(4.1884918 51.144580)',4326)::geography,100) 
    and highway is not null;

要优化此查询,您可以使用

WITH buff as (
   SELECT ST_BUFFER(
            ST_GeomFromText('POINT(4.1884918 51.144580)',4326)::geography, 
            1000) as geog)  
select highway, 
   st_length(way) full_length,   
   st_length(
     ST_INTERSECTION(
        ST_Transform(way,4326)::geography,
        buff.geog
      )
   )
from osm.osm_line
    INNER JOIN buff
        ON ST_DWITHIN(
          ST_Transform(way,4326),
          buff.geog,
          1000) 
where highway is not null;

请注意,缓冲区仅是一个圆的近似值,因此结果可能略有不同