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

时间:2016-04-19 13:55:37

标签: sql postgresql postgis

我有以下情况,我已经实现了一个小的查询,它合并相同的#34;类型"的相邻多边形。但当然我丢失了所有信息,除了几何和"类型"。

以下图片是第一步:

enter image description here

但我想在新的蓝色多边形中连接两个旧蓝色多边形的信息。

我尝试实现一个查询,其中不同的字段基于不同的group by。

看起来像:

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)

但我无法理解如何管理这部分!

1 个答案:

答案 0 :(得分:0)

我在一个简单的环境中测试了这个脚本:

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))'));

simple environment

和结果

sample output

相关问题