检索给定区域内 "linestring" 子段的长度(使用 ST_DWithin)

Retrieve length of "linestring" sub-segment within given area (using ST_DWithin)

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

我目前正在使用 ST_DWithin 功能,但不幸的是它 returns 是完整的对象,而不是区域内的对象片段。事实上,改变半径(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)

ST_DWithin 用于 select 至少部分在距参考点的所述距离内的几何图形。

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

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;

请注意,缓冲区只是一个近似的圆,因此结果可能略有偏差