[GIS] Merge overlapping/neighboring polygons – GeoPandas

geopandaspythonshapefileshapely

I am reading a shapefile and I have extracted a specific row from it that is a Multipolygon. I can extract all the polygons in it with .explode() which works great and I get 130+ polygons.

How can I merge the ones that are next to each other?

I know the 130+ resulting polygons represent two islands, and I want to get each as a polygon. I have looked up how to do it but haven't been able to find anything and this is my first project ever using geopandas and shapely.

gdf=gpd.read_file("comunas.shp").to_crs({'init': 'epsg:4326'})
Rapa_Nui=gdf.loc[gdf["Comuna"]=="Isla de Pascua"]
all_Rapa_Nui=Rapa_Nui.explode()

Best Answer

You can first union all polygons with unary_union:

single_multi_polygon = all_Rapa_Nui.unary_union

This should now be a single MultiPolygon consisting of two polygons for the two islands. And then you can get the polygon parts of this MultiPolygon:

polygons = single_multi_polygon.geoms
Related Question