簡體   English   中英

如何在 st_union (POSTGIS) 之后保存所有多邊形中包含的信息

[英]How to save the information contains by all the polygons after a st_union (POSTGIS)

我有以下情況,我實現了一個合並相同“類型”的相鄰多邊形的小查詢。 但是我當然會丟失所有信息,除了幾何和“類型”。

下圖總結了第一步:

在此處輸入圖像描述

但我想將兩個舊藍色多邊形的信息連接到新的多邊形中。

我嘗試實現一個查詢,其中不同的字段基於不同的分組依據。

看起來像:

SELECT ST_DUMP(ST_Union(geom)).geom as geom,string_agg(param1,',') as  param1, string_agg(param2,',') as param2, type
FROM t1  
GROUP BY type (for the st_union function)
GROUP BY geom (for the string_agg function)

但我無法理解如何管理這部分!

我在一個簡單的環境中測試了這個腳本:

select geom, my_type , 
    case when the_path is not null then values1[the_path] else values1[1] end as value1, 
case when the_path is not null then values2[the_path] else values2[1] end as value2
from (
    select 
        st_asewkt(  (st_dump(st_union(geom))).geom  ) as geom,
        (st_dump(st_union(geom))).path[1] as the_path  ,
        my_type, 
        array_agg(value1) as values1, 
        array_agg(value2) as values2
    from t1
    group by my_type
) tx

我希望它可以幫助您解決這個問題。

這是簡單環境的腳本:

drop table t1;
create table t1(
    value1 text,
    value2 text,
    my_type text,
    geom geometry(Polygon)
);

insert into t1 (value1,value2,my_type,geom) values ('1-one','2-one','red',ST_GeomFromText('POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))'));
insert into t1 (value1,value2,my_type,geom) values ('1-two','2-two','red',ST_GeomFromText('POLYGON((1 0, 1 1, 2 1, 2 0, 1 0))'));
insert into t1 (value1,value2,my_type,geom) values ('1-three','2-three','blue',ST_GeomFromText('POLYGON((4 0, 4 1, 5 1, 5 0, 4 0))'));
insert into t1 (value1,value2,my_type,geom) values ('1-four','2-four','blue',ST_GeomFromText('POLYGON((7 0, 7 1, 8 1, 8 0, 7 0))'));

簡單的環境

和結果

樣本輸出

聚會有點晚了,但您需要做的是首先合並多邊形,然后找到哪些舊多邊形與合並的多邊形相交。 您基本上需要 2 個新表。 一張包含新的分組多邊形的表和一張連接表,用於將新合並的多邊形連接到原始多邊形(僅包含 grouped_polygon_ids 和 original_polygon_ids)。

這可以通過以下方式完成:

CREATE TABLE grouped_polygons AS
    SELECT uuid_generate_v4() as id,
           ST_DUMP(ST_Union(geom)).geom as geom,
           string_agg(param1,',') as  param1,
           string_agg(param2,',') as param2,
           type
FROM t1  
GROUP BY type (for the st_union function)
GROUP BY geom (for the string_agg function);

CREATE TABLE join_table AS
SELECT t1.id as original_polygon_id,
       grouped_polygons.id as grouped_polygon_id
FROM t1 
JOIN grouped_polygons 
ON st_intersects(t1.geom, grouped_polygons.geom);

然后你可以像這樣查詢結果:

SELECT  gb.id, gp.geom, t1.*
FROM grouped_polygons gp
JOIN join_table jt 
ON gp.id = jt.grouped_polygon_id 
JOIN t1 
ON t1.id  = jt.original_polygon_id 
ORDER BY gp.id;

這樣,您可以將 t1 的所有屬性連接到新多邊形。 如果需要,您可以選擇使用哪個聚合函數將它們組合在一起。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM