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.
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.
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...
isinstance()
code, I get: NameError: name 'shapely' is not defined
–
Embow import shapely
to the top of your code –
Asta you can do this simply.
import shapely.geometry.multipolygon as sh
if isinstance(polygon, sh.MultiPolygon):
print('yes I am')
© 2022 - 2024 — McMap. All rights reserved.
class MultiPolygon
here: toblerity.org/shapely/manual.html#collections-of-polygons – Lynn