我设法解决了这个问题,而没有使用提到的GRASS工具或拓扑功能。
基本上,我将所有开始节点和结束节点放入新的临时表中,在它们周围放置一个缓冲区,合并缓冲区对象,然后将每个缓冲区中找到的所有节点移到缓冲区的质心。
完成后,我将原始的起点和终点移动到新位置。
比预期的要容易,并且仍然很快,但是我希望PostGIS为此提供一些内置功能-这样会更快。
编辑:为了回馈社区,这是我的代码(现在很糟糕)。
drop table if exists nodes;
drop table if exists nodes2;
drop table if exists buffers;
-- Get Start and End nodes
select ST_StartPoint(wkb_geometry) startnode, ST_EndPoint(wkb_geometry) endnode, ogc_fid into nodes from sourceTable;
-- Combine all nodes into one table for easier queries
select startnode node, ogc_fid into nodes2 from nodes;
insert into nodes2 select endnode node, ogc_fid from nodes;
-- Some indexes to speed everything up
CREATE INDEX nodesstart_idx ON nodes USING gist (startnode);
CREATE INDEX nodesend_idx ON nodes USING gist (endnode);
CREATE INDEX nodes2_idx ON nodes2 USING gist (node);
CREATE INDEX nodes_ogcfid_idx ON nodes USING btree (ogc_fid ASC NULLS LAST);
-- Create buffers, combine them, split combined objects again
select (ST_Dump(ST_Union(ST_Buffer(node, 1)))).geom geom into buffers from nodes2;
CREATE INDEX buffers_idx ON buffers USING gist (geom);
-- Update start/end nodes table
UPDATE nodes SET startnode = ST_Centroid((select geom from buffers WHERE geom && startnode));
UPDATE nodes SET endnode = ST_Centroid((select geom from buffers WHERE geom && endnode));
-- Update original points
update sourceTable set wkb_geometry = ST_SetPoint(
ST_SetPoint(wkb_geometry, 0, (select startnode from nodes where ogc_fid=sourceTable.ogc_fid)),
ST_NumPoints(wkb_geometry) - 1, (select endnode from nodes where ogc_fid=sourceTable.ogc_fid));
DROP TABLE nodes;
DROP TABLE nodes2;
DROP TABLE buffers;