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:
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.)