14

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')
4
  • Can you remove .exterior ?
    – anarchy
    Aug 20, 2021 at 11:07
  • Can you also print the dtypes of the columns?
    – anarchy
    Aug 20, 2021 at 11:11
  • @anarchy I have edited my post to add dtypes and more info.
    – SCool
    Aug 20, 2021 at 11:15
  • 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 easier
    – anarchy
    Aug 20, 2021 at 11:26

1 Answer 1

16

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

gdf_exploded=gdf.explode()

you can find the docs here

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.