1

I'm really desapointed, because I did everything to avoid Multipolygon in my script but last treatment blocks because of Multipolygon :

File "/.../Zonage/Zonage_standalone.py", line 254, in zonage zonage = gpd.overlay(clip, parc, how='intersection') # Intersection parcellaire et zonage

AttributeError: 'MultiPolygon' object has no attribute 'exterior'

You can find my script and datas here if you want to help me.

4
  • You can convert the MultiPolygon to a Polygon? (or split them over multiple rows?)
    – joris
    Feb 6, 2018 at 16:12
  • I create a fonction to explode multipolygons. I tested my script with some datas and it worked well. But with these datas Multipolygons come back... :/
    – Tim C.
    Feb 6, 2018 at 16:14
  • There is a explode method on GeoSeries/GeoDataFrame that can do that as well. You will need to be clearer with what you mean with "ghost" multipolygons.
    – joris
    Feb 6, 2018 at 21:09
  • Oh ! I didn't find this method before :/ I will try it now. "Ghost multipolygons" means that one or both of gdf used in intersection may contain multipolygons but before intersection I used 2 tricks (my explode fonction for both, and geometry to str wkt, groupby str wkt, str wkt to geometry) to delete them. When i tried to highlight multipolygons, it didn't work for i, row in parc.iterrows(): typeparc = parc.geom_type[i] print(typeparc) if typeparc != 'Polygon': print('parc',typeparc)
    – Tim C.
    Feb 7, 2018 at 9:01

1 Answer 1

3

The geopandas.overlay function indeed does not work on MultiPolygons.

But, you can use the explode method to ensure that your dataframe does not contain any MultiPolygons. The explode method will convert any row with a MultiPolygon geometry into multiple rows with the different Polygons. Currently this only 'explodes' the geometry column, so if you want to retain the other columns (and repeating the values for exploded rows), you need to merge the result with the original frame (assuming df is a GeoDataFrame with MultiPolygons):

geoms_exploded = df.explode().reset_index(level=1, drop=True)
df_new = df.drop(columns='geometry').join(geoms_exploded.rename('geometry'))
df_new = geopandas.GeoDataFrame(df_new)

but I opened an issue for an enhancement request to let explode also work on GeoDataFrames out of the box: https://github.com/geopandas/geopandas/issues/667

2
  • I exploded the 2 gdf before overlay and I still have the error...
    – Tim C.
    Feb 8, 2018 at 16:20
  • Then please show the code (you can add it to the original question)
    – joris
    Feb 9, 2018 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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