5

This question is related to: Filtering by geometry type in GeoPandas

I need to check if my geodataframe contains 'MultiPolygon' type.

Below is my attempt, but it doesn't work. According to documentation it should work, I think. How can I do it?

import geopandas as gpd
from shapely.geometry import Point, Polygon, MultiPolygon, MultiPoint, LineString, MultiLineString

geo_dict= {"ogc_fid":[58], "geometry":[MultiPolygon([Polygon([(1, 2), (2, 1), (3, 2), (1, 2)])])]}
gdf= gpd.GeoDataFrame(geo_dict)

result1 ='MultiPolygon' in (gdf['geometry'].geom_type)
result2 = MultiPolygon in (gdf['geometry'].geom_type)
result3 ='Polygon' in (gdf['geometry'].geom_type)
result4 = Polygon in (gdf['geometry'].geom_type)
print(result1) # prints False
print(result2) # prints False
print(result3) # prints False
print(result4) # prints False

2 Answers 2

4

You don't need a list, simply use

'MultiPolygon' in gdf['geometry'].geom_type.values
0

To solve this, it is enough to add list():

import geopandas as gpd
from shapely.geometry import Point, Polygon, MultiPolygon, MultiPoint, LineString, MultiLineString

geo_dict= {"ogc_fid":[58], "geometry":[MultiPolygon([Polygon([(1, 2), (2, 1), (3, 2), (1, 2)])])]}
gdf= gpd.GeoDataFrame(geo_dict)

result1 ='MultiPolygon' in list(gdf['geometry'].geom_type)
result2 = MultiPolygon in list(gdf['geometry'].geom_type)
result3 ='Polygon' in list(gdf['geometry'].geom_type)
result4 = Polygon in list(gdf['geometry'].geom_type)
print(result1) # prints True
print(result2) # prints False
print(result3) # prints False
print(result4) # prints False
2
  • You don't need a list, simply use 'MultiPolygon' in gdf['geometry'].geom_type.values
    – gene
    Commented Mar 18, 2022 at 19:49
  • @gene Thank you, if you add this solution as separate answer, I will accept it.
    – Karol Zlot
    Commented Mar 18, 2022 at 19:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.