简体   繁体   English

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

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

I have the following case, i have implemented a small query that merge the adjacent polygons of the same "type".我有以下情况,我实现了一个合并相同“类型”的相邻多边形的小查询。 But of course I lose all the information, except the geometry and the "type".但是我当然会丢失所有信息,除了几何和“类型”。

The following image sumarize the first step:下图总结了第一步:

在此处输入图像描述

But I want to concatenate the information of the two old blue polygon in the new one.但我想将两个旧蓝色多边形的信息连接到新的多边形中。

I try to implement a query where differents fields are based on differents group by.我尝试实现一个查询,其中不同的字段基于不同的分组依据。

Something that look like:看起来像:

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)

But i'm unable to understand how to manage this part !但我无法理解如何管理这部分!

I tested this script in a simple environment:我在一个简单的环境中测试了这个脚本:

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

I hope it could help you to figure out this problem.我希望它可以帮助您解决这个问题。

This is the script for the simple environment:这是简单环境的脚本:

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

简单的环境

and the result和结果

样本输出

A bit late to the party but what you need to do is to first merge the polygons and then find which of the old polygons intersect with the merged ones.聚会有点晚了,但您需要做的是首先合并多边形,然后找到哪些旧多边形与合并的多边形相交。 You basically need 2 new tables.您基本上需要 2 个新表。 One table with the new, grouped polygons and one join table to connect the new merged polygons to your original polygons (containing only the grouped_polygon_ids and the original_polygon_ids).一张包含新的分组多边形的表和一张连接表,用于将新合并的多边形连接到原始多边形(仅包含 grouped_polygon_ids 和 original_polygon_ids)。

This can be done with something like this:这可以通过以下方式完成:

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

Then you can query the results like this:然后你可以像这样查询结果:

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;

This way you can have all the attributes of t1 connected to the new polygons.这样,您可以将 t1 的所有属性连接到新多边形。 You can choose which aggregation function to use to group them together if you want.如果需要,您可以选择使用哪个聚合函数将它们组合在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM