Convert Geopandas Multipolygon to Polygon
Asked Answered
P

1

16

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')
Precipitancy answered 20/8, 2021 at 11:4 Comment(4)
Can you remove .exterior ?Junior
Can you also print the dtypes of the columns?Junior
@Junior I have edited my post to add dtypes and more info.Precipitancy
could you paste a copy of the dataframe just a few lines ? because i cant seem to install the arcgis package properly, you could also upload a csv file of just the first few lines as well that would make it easierJunior
M
23

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

gdf_exploded=gdf.explode()

you can find the docs here

Mediatory answered 25/8, 2021 at 11:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.