Check if a polygon is a polygon in Shapely

How can I check if a polygon is actually a multipolygon? I tried:

if len(polygon) > 1: 

but then get the error:

 TypeError: object of type 'Polygon' has no len() 

I tried Nill , None and others, nothing Nill .

+8
source share
2 answers

Use the string object.geom_type (see Common Attributes and Methods ).

For instance:

 if poly.geom_type == 'MultiPolygon': # do multipolygon things. elif poly.geom_type == 'Polygon': # do polygon things. else: # raise IOError('Shape is not a polygon.') 
+9
source

Ok, this worked for me:

 print ('type = ', type(poly)) 

outputs from:

 type = <class 'shapely.geometry.polygon.Polygon'> 

in the case of a polygon and:

 type = <class 'shapely.geometry.multipolygon.MultiPolygon'> 

in case of multipolygon.

To check if a variable is a polygon or a polygon, I did this:

 if (isinstance(poly, shapely.geometry.multipolygon.MultiPolygon)): code... 
+5
source

All Articles