【问题标题】:Postgresql: Find road endpoints within distance from coastPostgresql:在距离海岸的距离内查找道路端点
【发布时间】:2016-02-13 15:26:30
【问题描述】:

我在一张桌子上有 220 万条线几何图形(道路),在另一张桌子上有 1500 条线几何图形(海岸线)。两个表都有空间索引。 我需要找到距离海岸一定距离内的道路端点,并将点几何与距离一起存储。 当前的解决方案似乎效率低下,并且在一台速度非常快的机器上需要很多小时才能完成;

使用 ST_STARTPOINT、ST_ENDPOINT 和 ST_DWITHIN 使用距离内道路几何图形的起点和终点创建 TEMP TABLE。 为临时表中的两个几何列创建空间索引。

做两个INSERT INTO操作,一个用于起点,一个用于端点; 选择几何和距离,使用从点到海岸线的 ST_DISTANCE 和 WHERE ST_DWITHIN 仅考虑所选距离内的点。

代码看起来是这样的:

create temp table roadpoints_temp as select st_startpoint(road.geom) as geomstart, st_endpoint(road.geom) as geomend from 
    coastline_table coast, roadline_table road where st_dwithin(road.geom, coast.geom, 100);

create index on roadpoints_temp (geomstart);

create index on roadpoints_temp (geomend);

create table roadcoast_points as select roadpoints_temp.geomstart as geom, round(cast(st_distance(roadpoints_temp.geomstart,kyst.geom) as numeric),2) as dist 
    from roadpoints_temp, coastline_table coast where st_dwithin(roadpoints_temp.geomstart, coast.geom, 100);

insert into roadcoast_points select roadpoints_temp.geomend as geom, round(cast(st_distance(roadpoints_temp.geomend,kyst.geom) as numeric),2) as dist 
    from roadpoints_temp, coastline_table coast where st_dwithin(roadpoints_temp.geomend, coast.geom, 100);

drop table roadpoints_temp;

欢迎所有 cmets 和建议 :-)

【问题讨论】:

  • 我用一些测试数据尝试了您的代码,但没有出现性能问题。你有非常复杂的几何形状吗? (有很多顶点)。也许你能提供一个数据样本吗?
  • 我已经尝试显式使用 gist,但这并没有影响性能 - 临时表的创建似乎占用了大部分处理时间。
  • 最长的海岸线长达 1000 多公里,由 190.000 个顶点组成。我会查看 st_segmentize 看看这是否对速度有影响。

标签: postgresql postgis


【解决方案1】:

您需要有效地利用您的索引。似乎最快的计划是为每个海岸找到其距离内的所有道路。分别进行两次重新检查意味着您失去了与道路最近的海岸线的连接,需要一次又一次地重新找到这对。

您需要使用 EXPLAIN 检查您的执行计划,以便对海岸线表进行 Seq Scan 并对道路表进行 GiST 索引扫描。

select road.* 
from coastline_table coast, roadline_table road
where 
    ST_DWithin(coast.geom, road.geom, 100) -- indexed query    
    and -- non-indexed recheck
    (
        ST_DWithin(ST_StartPoint(road.geom), coast.geom, 100)
        or ST_DWithin(ST_EndPoint(road.geom), coast.geom, 100)
    );

【讨论】:

    猜你喜欢
    • 2015-02-26
    • 2019-01-21
    • 1970-01-01
    • 1970-01-01
    • 2014-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多