3

I am curious how can I find all polygons that are touching with each other but are completely separate from the rest of the polygons is the same dataset, using PostGIS.

For better understanding I uploaded a picture.

enter image description here

Vince
  • 20,017
  • 15
  • 45
  • 64
DrJacoby
  • 781
  • 3
  • 15
  • Are they MultiPolygon data type? – Taras Aug 30 '21 at 08:44
  • Yes, but usually they consists of one polygon. Every polygon you see on the picture is separate from others, but every one is defined as MultiPolygon in a database. – DrJacoby Aug 30 '21 at 08:49
  • 4
    I don't really understand even with the picture ... Is the question from your picture is : how to find an entire square ? If so, merge / dump and intersect with itself, if no intersections and contains more than 1 origin polygon, it is what you search. – J. Monticolo Aug 30 '21 at 08:50
  • PgRouting should work but it requires some pre-processing. Nodes that you cannot access from a start node belong to other islands. – user30184 Aug 30 '21 at 13:15
  • 1
    I think you may be looking for clustering based on intersection? If so, try this. – geozelot Aug 30 '21 at 15:17
  • See this solution: https://gis.stackexchange.com/a/390276/88814 : buffer your polygons and dissolve the buffer to get groups of contiguous polygons. Use a very small buffer size or even a size of 0. – Babel Aug 31 '21 at 06:53

2 Answers2

4

As suggested in this answer, use ST_ClusterDBSCAN to assign an id to each touching group of polygons:

SELECT *,
       ST_ClusterDBSCAN(geom, 0, 1) OVER() AS clst_id
FROM   poly_table;
dr_jts
  • 5,038
  • 18
  • 15
2

You can merge touching polygons, see this answer:

https://stackoverflow.com/a/49970447/2816941

This will produce a layer for these "islands".

Then you can simply join back onto the islands from the original data using st_intersects(islands.geom, st_pointonsurface(original.geom))

Babel
  • 71,072
  • 14
  • 78
  • 208
HeikkiVesanto
  • 16,433
  • 2
  • 46
  • 68