如何检查多边形实体是否确实是多重多边形?我尝试过:
if len(polygon) > 1:
但是却收到了错误:
TypeError: object of type 'Polygon' has no len()
我尝试过Nill
、None
等,都没用。
object.geom_type
字符串(详见一般属性和方法)。if poly.geom_type == 'MultiPolygon':
# do multipolygon things.
elif poly.geom_type == 'Polygon':
# do polygon things.
else:
# raise IOError('Shape is not a polygon.')
好的,这个方法对我有效:
print ('type = ', type(poly))
使用以下输出:
type = <class 'shapely.geometry.polygon.Polygon'>
type = <class 'shapely.geometry.multipolygon.MultiPolygon'>
在多边形的情况下,进行以下操作。
要检查一个变量是多边形还是多边形集合,可以这样做:
if (isinstance(poly, shapely.geometry.multipolygon.MultiPolygon)):
code...
isinstance()
代码,我得到了:NameError: name 'shapely' is not defined
。 - janeimport shapely
。 - Fee你可以简单地做到这一点。
import shapely.geometry.multipolygon as sh
if isinstance(polygon, sh.MultiPolygon):
print('yes I am')
class MultiPolygon
的内容:http://toblerity.org/shapely/manual.html#collections-of-polygons - Alessandro Trinca Tornidor