Reputation: 10792
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 !
Upvotes: 1
Views: 601
Reputation: 1166
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. 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).
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. You can choose which aggregation function to use to group them together if you want.
Upvotes: 1
Reputation: 26
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
Upvotes: 0