开源GIS+空间数据应用开源PostgreSQL

PostGIS拆分LineString为segment,poin

2018-11-30  本文已影响68人  遥想公瑾当年

一 创建测试表

创建一个测试表

 CREATE TABLE lines ( 
      gid integer primary key, 
      geom geometry(Linestring, 4326)
 );

插入测试数据

 INSERT INTO lines VALUES (1, 'SRID=4326;LINESTRING(1 1, 2 2, 3 3, 4 4)');
 INSERT INTO lines VALUES (2, 'SRID=4326;LINESTRING(0 1, 0 2, 0 3, 0 4)');

二 拆分成点

SELECT gid,(pt).path as ptindex,(pt).geom
FROM (SELECT gid, ST_DumpPoints(geom) AS pt FROM lines) as foo;
拆点.png

三 拆分成线

 WITH segments AS (
    SELECT gid, ST_MakeLine(lag((pt).geom, 1, NULL) OVER (PARTITION BY gid ORDER BY gid, (pt).path), (pt).geom) AS geom
      FROM (SELECT gid, ST_DumpPoints(geom) AS pt FROM lines) as dumps
    )
    SELECT * FROM segments WHERE geom IS NOT NULL;
拆segment.png
上一篇下一篇

猜你喜欢

热点阅读