TransWikia.com

Building larger polygons from smaller ones *without* common ID in PostGIS?

Geographic Information Systems Asked on July 17, 2021

I am looking for a method, using PostGIS, of reconstituting a set of polygons from tens of thousands of smaller polygon ‘pieces’.

This is a simplified example, with the pieces shaded:Polygon pieces

Unfortunately, the pieces do not have a common ID or other attribute of the larger polygons that would allow me to group them. In some cases, I have the grid that was used to cut the pieces, but in other cases I do not.

The only approach I’ve been able to devise — and I haven’t been able to make it work yet — is to use ST_BuildArea on pairs of intersecting pieces in an iterative process as follows (renaming ‘composite’ to ‘pieces’ between iterations):

SELECT p1.id,ST_BuildArea(ST_Collect(p1.the_geom,p2.the_geom)) AS the_geom
INTO composite
FROM pieces p1 LEFT OUTER JOIN pieces p2
    ON (ST_DWithin(p1.the_geom,p2.the_geom,1))
WHERE ST_Intersects(p1.the_geom,p2.the_geom) AND
p1.id < p2.id;

This generates larger and larger composite pieces, but many of them (nearly 1,300 after three iterations on the example), and they come increasingly to overlap. It seems to me that to go much further I’d need a means of paring down the results each iteration. (I’m also getting side location conflicts on the fourth iteration, but that may be a separate problem.)

One Answer

The simplest and fastest solution I found for this problem is

SELECT ST_UnaryUnion(unnest(ST_ClusterIntersecting(geom))) FROM pieces;

Dissolving a set of 165,000 polygons took 38 seconds in my case.

Correct answer by Leendert Van Wolfswinkel on July 17, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP