In Shapely 1.x, multi-part geometries (MultiPoint, MultiLineString, MultiPolygon and GeometryCollection) implement a part of the “sequence” python interface (making them list-like). This means you can iterate through the object to get the parts, index into the object to get a specific part, and ask for the number of parts with the len() method.
在Shapely 1.x中,复合几何对象(MultiPoint、MultiLineString、MultiPolygon和GeometryCollection)实现了 “序列 “python接口的一部分(使它们成为列表状)。这意味着你可以通过迭代对象来获得部件,通过索引对象来获得特定部件,并通过len()方法询问部件的数量。
Some examples of this with Shapely 1.x:
一些使用Shapely 1.x的例子:
>>> from shapely.geometry import Point, MultiPoint
>>> mp = MultiPoint([(1, 1), (2, 2), (3, 3)])
>>> print(mp)
MULTIPOINT (1 1, 2 2, 3 3)
>>> for part in mp:
... print(part)
POINT (1 1)
POINT (2 2)
POINT (3 3)
>>> print(mp[1])
POINT (2 2)
>>> len(mp)
3
>>> list(mp)
[<shapely.geometry.point.Point at 0x7f2e0912bf10>,
<shapely.geometry.point.Point at 0x7f2e09fed820>,
<shapely.geometry.point.Point at 0x7f2e09fed4c0>]
Starting with Shapely 1.8, all the examples above will start raising a deprecation warning. For example:
从Shapely 1.8开始,上面所有的例子都会开始发出弃用警告。例如:
>>> for part in mp:
... print(part)
ShapelyDeprecationWarning: Iteration over multi-part geometries is deprecated
and will be removed in Shapely 2.0. Use the `geoms` property to access the
constituent parts of a multi-part geometry.
POINT (1 1)
POINT (2 2)
POINT (3 3)
In Shapely 2.0, all those examples will raise an error.
在Shapely 2.0中,所有这些例子都会引发一个错误。
How do I update my code? To access the geometry parts of a multi-part geometry, you can use the .geoms
attribute, as the warning indicates.
我怎样才能更新我的代码?要访问一个多部分的几何体的几何部分,你可以使用如警告所示的.geoms属性。
The examples above can be updated to:
上面的例子可以更新为:
>>> for part in mp.geoms:
... print(part)
POINT (1 1)
POINT (2 2)
POINT (3 3)
>>> print(mp.geoms[1])
POINT (2 2)
>>> len(mp.geoms)
3
>>> list(mp.geoms)
[<shapely.geometry.point.Point at 0x7f2e0912bf10>,
<shapely.geometry.point.Point at 0x7f2e09fed820>,
<shapely.geometry.point.Point at 0x7f2e09fed4c0>]
The single-part geometries (Point, LineString, Polygon) already didn’t support those features, and for those classes there is no change in behaviour for this aspect.
单个部分的几何对象(Point, LineString, Polygon)已经不支持这些特征了,对于这些类来说,这方面没有变化。