27700

GeoPandas: Convert GeometryCollection to MultiPolygon

I've been using food security data from the the Integrated Phase Classificaiton API. As it's a work in progress, there are some quirks for how data is returned for different countries. When accessing the Niger data, I noticed my standard script failed as the geometries are returned as a GeometryCollection rather than as simple MultiPolygon.

After much searching and testing out different approaches for converting the geometry, the answer turned out to be surprisingly simple - you can use explode() on the GeometryCollection to return it's constituent parts. In this case, as each collection only held a single MultiPolygon, the returned values could be directly used as the new feature geometry.

df.geometry = df.geometry.explode()[0:].values

The page that was most helpful in figuring this out was this bug report on the GeoPandas GitHub.