Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostGIS - Merge buffers geometry in different groups (when intersecting)

I've some geometries stored in this table (140k)

CREATE TABLE buffers (pk integer NOT NULL,geom geometry(MultiPolygon,4326),)

- buffers geometries from table "buffers" -

and i would like to create (if possible) a new table (buffersmerged) that will contain the resultants geometries where the previews are grouped just when them are intersecting between each other.

My expected output, it is like when using in QGIS the Buffer(s) tool checking on "dissolve buffer results", but i would like, rather than just a big unique geometry as output, more small groups (one for each group of intersecting geometries)

- this is the expected result -

can you please help me to understand how can I construct a query that allows me to do that? (Postgres + PostGIS enabled) Thank You

Stefano

like image 798
entalpia Avatar asked Jan 29 '26 10:01

entalpia


1 Answers

Do you need the attributes too?

If not, this query should do the job:

CREATE TABLE buffersmerged AS 
SELECT (ST_dump(ST_union(a.geom))).geom 
FROM buffers a, buffers b 
WHERE ST_intersects(a.geom,b.geom) AND a.id != b.id;
like image 182
Tom-db Avatar answered Jan 31 '26 01:01

Tom-db