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.

7
  • 1
    If you're amenable to ArcGIS, I have an arcpy function to do this.
    – Tom
    Jun 4 '19 at 16:33
  • 2
    post your code --
    – ziggy
    Jun 4 '19 at 17:39
  • 2
    if you know postgis you can use the st_clusterdbscan function for this fairly easily
    – ziggy
    Jun 4 '19 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 '19 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 '19 at 6:56

Browse other questions tagged or ask your own question.