3

I have a shapefile with polygons and need to group them with the same Group_ID if they touch each others. The thing is, once I start assigning a number to one polygon, it needs to keep looking for neighbors of neighbors until all are assigned the same ID in the attributes table. For example:

enter image description here

The grouping should be like:

Polygon_ID          Group_ID
1                      1
2                      1
3                      1
4                      1
5                      1
6                      1
7                      1
8                      2
9                      2
10                     3

I am using QGIS for this, trying a script with python with a recursive function that keeps checking for neighbors until no more are found, no luck so far.

closed as off-topic by PolyGeo Jun 4 at 19:42

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    If you're amenable to ArcGIS, I have an arcpy function to do this. – Tom Jun 4 at 16:33
  • 2
    post your code -- – ziggy Jun 4 at 17:39
  • 2
    if you know postgis you can use the st_clusterdbscan function for this fairly easily – ziggy Jun 4 at 17:47
  • 2
    In ArcGIS I would try dissolve-(multipart to singlepart)-calculate an id column, spatial join this back to the original polygons. This must also be possible i QGIS – BERA Jun 4 at 18:51
  • 2
    @ziggy Thanks for the info, with this I can even group polygons that dont strictly touch but are close enough to be considered a group. – AJMR Jun 5 at 6:56

Browse other questions tagged or ask your own question.