【发布时间】: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 和建议 :-)
【问题讨论】:
-
尝试为索引添加
USING gist? postgresql.org/docs/9.3/interactive/sql-createindex.html -
我用一些测试数据尝试了您的代码,但没有出现性能问题。你有非常复杂的几何形状吗? (有很多顶点)。也许你能提供一个数据样本吗?
-
我已经尝试显式使用 gist,但这并没有影响性能 - 临时表的创建似乎占用了大部分处理时间。
-
最长的海岸线长达 1000 多公里,由 190.000 个顶点组成。我会查看 st_segmentize 看看这是否对速度有影响。
标签: postgresql postgis