Check if a polygon is a multipolygon in Shapely Check if a polygon is a multipolygon in Shapely python python

Check if a polygon is a multipolygon in Shapely


Use the object.geom_type string (see general attributes and methods).

For example:

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


Ok, this worked for me:

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

outputs with:

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

in case of a polygon, and:

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

in case of a multipolygon.

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

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


you can do this simply.

import shapely.geometry.multipolygon as shif isinstance(polygon, sh.MultiPolygon):    print('yes I am')