2

I have a large polygon shapefile and a small polygon shapefile. Both shapefiles are in the same coordinate system. What I do next is, clip small polygons within large polygon boundaries using GeoPandas's Clip.

And here comes the question, before clipping, small polygon geometry contains only Polygon features and not MultiPolygon, but after using geopandas clip(), some small polygon geometry transforms to MultiPolygons. Is there some specific reason why it happens? I should use the polygons in further analysis where I assume, because of this which will arise some error or problem.

Note: none of the small, neither large polygons overlay.

I did try to transform the MultiPolygons to Polygons with gpd.explode(), which gave me geopandas.geoseries.GeoSeries and the result below, however it can be noted that some polygon "ID" are missing, I also cannot assign an "ID" to each feature with df['ID'] = df.index(). Then arises an error:

TypeError: 'MultiIndex' object is not callable.

Is there some explanation why I get MultiPolygons instead of Polygons in the gpd.clip() result?

import geopandas as gpd

small = gpd.read_file(r'R:\...\small.shp')
large = gpd.read_file(r'R:\...\large.shp')

smallClip = gpd.clip(small, large)
smallClip_explode = smallClip['geometry'].explode()

print(type(smallClip_explode))
Out[50]: geopandas.geoseries.GeoSeries

gdf = gpd.GeoDataFrame(geometry=gpd.GeoSeries(smallClip_explode))

enter image description here

2
  • If the update is an answer to your question, please add it using "Answer Your Question" button. Nov 23, 2021 at 8:04
  • Of course, thanks for the advice!
    – g123456k
    Nov 23, 2021 at 8:11

2 Answers 2

4

The reason why it happens can be the following:

Input: input

Output: result

The resulting pink feature is the MULTIPOLYGON(((1)),((2)))

To tackle this circumstances you can either:

1
  • I guess that makes sense, a very simple explanation. I was sure that I had no such polygons, but I guess there is. Thanks a lot!
    – g123456k
    Nov 23, 2021 at 8:07
4

As was written by @g12345kk in his question:

I did find a way how to transform from MultiPolygons to Polygons without error:

import geopandas as gpd

small = gpd.read_file(r'R:\...\small.shp')
large = gpd.read_file(r'R:\...\large.shp')

smallClip = gpd.clip(small, large)
smallClip_explode = smallClip['geometry'].explode()

gdf = gpd.GeoDataFrame(geometry=gpd.GeoSeries(smallClip_explode))
gdf.reset_index(inplace=True)

gdf['FID'] = gdf.index
fid = gdf['FID']
gdf.drop(labels=['FID'], axis=1, inplace=True)
gdf.insert(0, 'FID', fid)

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.