根据线串的id从线串创建多边形/多重线串

kadbb459  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(466)

我有两张这样的table:

lines:
line_id|    geometry
-------------------
1      |    ....
2      |    ....
3      |    ....
4      |    ....
5      |    ....
6      |    ....

(几何图形列在线条表中填写)

polygons:
    poly_id |    line_id 
    --------------------
    1       |       1     
    1       |       2     
    1       |       3        
    1       |       4        
    2       |       3       
    2       |       5      
    2       |       6        
    2       |       7

正如你在多边形表中看到的,我有一条构成多边形的线的id-s。我要做的是,我创建一个新表,每个poly\u id只有一条记录:

SELECT DISTINCT(poly_id) 
INTO polygons_new
FROM polygons;

SELECT AddGeometryColumn('polygons_new','geom',23700,'POLYGON',2);

在这之后,我想填写几何专栏,但到目前为止,我无法做到这一点。我试过的是这样的:

UPDATE polygons_new
SET geom = (SELECT ST_Collect(SELECT geometry FROM lines as a, polygons as b WHERE a.line_id = b.line_id))

问题是它在子查询中返回多行。有办法完成这个任务吗?

qyyhg6bp

qyyhg6bp1#

你试过用, ST_Collect , ST_MergeLine 然后使用 ST_MakePolygon ? 下面的例子将让您了解我所说的内容:

WITH j (id,geom) AS (
  VALUES (1,'SRID=4326;LINESTRING(30 10, 10 30)'),
         (1,'SRID=4326;LINESTRING(10 30, 40 40)'),
         (1,'SRID=4326;LINESTRING(40 40, 30 10)'),
         (2,'SRID=4326;LINESTRING(50 60, 60 20)'),
         (2,'SRID=4326;LINESTRING(60 20, 45 45)'),
         (2,'SRID=4326;LINESTRING(45 45, 50 60)')
) 
 SELECT 
  ST_MakePolygon(
    ST_LineMerge(
      ST_Collect(geom))) 
FROM j
GROUP BY id


编辑:如果只有 MULTILINESTRING 够了,就用吧 ST_Collect (见注解):

WITH j (id,geom) AS (
  VALUES (1,'SRID=4326;LINESTRING(30 10, 10 30)'),
         (1,'SRID=4326;LINESTRING(10 30, 40 40)'),
         (1,'SRID=4326;LINESTRING(40 40, 30 10)'),
         (2,'SRID=4326;LINESTRING(50 60, 60 20)'),
         (2,'SRID=4326;LINESTRING(60 20, 45 45)'),
         (2,'SRID=4326;LINESTRING(45 45, 50 60)')
) 
SELECT ST_Collect(geom)
FROM j
GROUP BY id;

相关问题