SCool
SCool

Reputation: 3385

Convert Geopandas Multipolygon to Polygon

I have a geodataframe with a Multipolygon geometry:

enter image description here

I would like to convert them to Polygons i.e. fill in the holes of multipolygon and make it a single polygon.

I have tried the code from this similar question:


from shapely.geometry import MultiPolygon, Polygon
gdf['Polygon'] =  gdf['SHAPE'].apply( lambda x: MultiPolygon(Polygon(p.exterior) for p in x))

But I get the error:

TypeError: 'Polygon' object is not subscriptable

I have tried other solutions from stack overflow with no luck.

Any ideas?

Here are the dtypes:

FID                 int64
LHO                object
Shape__Area       float64
Shape__Length     float64
SHAPE            geometry

Here is the complete code to get the shapefile:


import pandas as pd
import geopandas as gpd
from arcgis import GIS
gis = GIS(verify_cert=False,api_key='your_api_key')

search_result = gis.content.search(query="title:National_LHO", item_type="Feature Layer")

# get layer
layer = search_result[0].layers[0]

# dataframe from layer
df= pd.DataFrame.spatial.from_layer(layer)

gdf = gpd.GeoDataFrame(df)

gdf = gdf.set_geometry('SHAPE')

gdf = gdf.set_crs(epsg='3857')

gdf = gdf.to_crs(epsg='4326')

Upvotes: 17

Views: 23135

Answers (1)

Babak Fi Foo
Babak Fi Foo

Reputation: 1048

There is method called .explode you can use on your GeoDataFrame:

gdf_exploded=gdf.explode()

you can find the docs here

Upvotes: 23

Related Questions