I have the following scenario:
SELECT * FROM owner_poly;
>
|id| owner_id | polygon_ids |
|1 | 10 |'{1,2,3,20}' |
|2 | 10 |'{12,22,2}' |
|3 | 10 |'{1}' |
|4 | 10 |'{12}' |
|5 | 11 |'{5,7}' |
|6 | 11 |'{5,9}' |
|7 | 12 |'{15,77}' |
|8 | 12 |'{19,47}' |
SELECT * FROM poly;
>
| id | geom |
| 1 |MultiPolygon(..) |
| 2 |MultiPolygon(..) |
....
I have a owner_poly table that stores an owner_id and associated polygons through their id's as an array. It is ensured that all polygons inside one array are interconnected. Now I need to group them by their interconnectivity with other arrays. For the above example I need the following output:
| owner_id | polygon_ids |
| 10 |'{1,2,3,12,20,22}'|
| 11 |'{5,7,9}' |
| 12 |'{15,77}' |
| 12 |'{19,47}' |
All polygons for owner_id 10 and 11 were interconnected and, thus, need to be stored in one array. For 12 it is not the case and, thus, they stay as they are.
Now I am trying to wrap my head around to group them based on the array values but couldn't find a solution.
Therefore I am wondering if a PostGIS function / algorithm exists that could identify interconnected geometries. For instance: id 4 is connected with id 1 through id 2 (polygon_id 12->2), but may not directly intersect with it spatially.
Could someone provide me with a guideline for either way (through the array or the geometry)?