I have a Shapefile with overlapping features. Is there an easy way to merge them with geopandas?

I found a way with shapely, but I would like to do it with geopandas directly.


The GeoDataFrame

import geopandas as gpd
g1 = gpd.GeoDataFrame.from_file("poly_intersect.shp")
(4, 3)

enter image description here

enter image description here

1) You can use the itertools module

a) If you want to merge the intersections of the overlapping polygons

import itertools
geoms = g1['geometry'].tolist()
intersection_iter = gpd.GeoDataFrame(gpd.GeoSeries([poly[0].intersection(poly[1]) for poly in  itertools.combinations(geoms, 2) if poly[0].intersects(poly[1])]), columns=['geometry'])

enter image description here

enter image description here


union_iter = intersection_iter.unary_union

b) If you want to merge the intersected polygons change intersection by union (all the polygons overlap in my example)

enter image description here

2) You can use GeoPandas Overlay


auto_inter = gpd.overlay(g1, g1, how='intersection')

The resulting GeoDataframe

enter image description here

GeoPandas add the intersection geometries to the existing geometries, therefore

intersection = auto_inter[4:7]

enter image description here


union = intersection.unary_union

b) use gpd.overlay(g1, g1, how='union')

| improve this answer | |

If you want to output a layer of distinct merged overlapping polygons, and not just a single multi-polygon, then after using:

union = intersection.unary_union

you can convert the resulting multi-poly to a GeoSeries:

shapes_series = gpd.GeoSeries([polygon for polygon in union])

or to a GeoDataFrame:

shapes_df = gpd.GeoDataFrame([polygon for polygon in union]).set_geometry(0)
| improve this answer | |
  • This should probably be an edit to the first answer rather than an answer in it's own right – nmtoken Jan 26 '18 at 8:14
  • Thanks for the tip, but I'd end up reworking gene's answer, and it's not worth the effort. I couldn't add a comment either as my rep is below 50. – Pawel Kranzberg Jan 26 '18 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.