Search code examples
pythonpython-3.xgisgeopandasshapely

Group by and combine intersecting/overlapping geometries in geopandas


I have a geopandas dataframe that has several rows with overlapping polygon geometries along with an index (unique and sequential). I want to merge the overlapping polygon geometries into a multi-polygon and keep the corresponding minimum index of the individual overlapping polygons.

For example: The geodataframe is as follows:

original geodataframe

Lets say the polygon geometries with index 10233, 10235, 10238 overlap. I want a single row for this with these geometries merged in a multi-polygon (instead of 3 separate geometries) and the corresponding index should be the minimum index of the 3 rows that is 10233. I would like to do this for the entire geodataframe

I tried using the dissolve function from geopandas:

gdf = gdf.dissolve(by = 'index').reset_index()

This does not do anything since 'index' is unique. I also tried:

gdf = gdf.dissolve().reset_index()

However, this combines all geometries into single row of multi-polygons


Solution

  • I think this is what you had in mind:

    Input geometries

    import geopandas as gpd
    
    # load your geodataframe ..
    
    # self join on geodataframe to get all polygon intersections
    intersects = gdf.sjoin(gdf, how="left", predicate="intersects")
    
    # dissolve intersections on right index indices using the minimum value
    intersects_diss = intersects.dissolve("id_right",aggfunc="min")
    
    # dissolve again on left index using minimum
    intersects_diss = intersects_diss.reset_index().dissolve("id_left",aggfunc="min")
    

    Output