Check if a polygon is a multipolygon in Shapely

Yair picture Yair · Aug 25, 2016 · Viewed 11.2k times · Source

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

if len(polygon) > 1:

but then get the error:

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

I've tried Nill, None and others, nothing worked.

Answer

jmsinusa picture jmsinusa · Feb 9, 2017

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.')