Heterogeneous collections of geometric objects may result from some Shapely operations. For example, two LineStrings may intersect along a line and at a point. To represent these kind of results, Shapely provides frozenset-like, immutable collections of geometric objects. The collections may be homogeneous (MultiPoint etc.) or heterogeneous.
一些Shapely操作可能会出现多项几何对象的集合(collections)。例如,两个LineString可能沿着一条线和在一个点相交。为了表示这些结果,Shapely提供了类似于frozenset的、不可改变的几何对象的集合。这些集合可以是同质的(复合点等)或异质的(点与线的集合)。
>>> a = LineString([(0, 0), (1, 1), (1,2), (2,2)])
>>> b = LineString([(0, 0), (1, 1), (2,1), (2,2)])
>>> x = a.intersection(b)
>>> x
<shapely.geometry.collection.GeometryCollection object at 0x...>
>>> from pprint import pprint
>>> pprint(list(x))
[<shapely.geometry.point.Point object at 0x...>,
<shapely.geometry.linestring.LineString object at 0x...>]
(Source code, png, hires.png, pdf)
Members of a GeometryCollection are accessed via the geoms
property or via the iterator protocol using in
or list()
.
几何集合(GeometryCollection)的成员可以通过geoms属性或通过in、list()的迭代器协议来访问。
>>> pprint(list(x.geoms))
[<shapely.geometry.point.Point object at 0x...>,
<shapely.geometry.linestring.LineString object at 0x...>]
>>> pprint(list(x))
[<shapely.geometry.point.Point object at 0x...>,
<shapely.geometry.linestring.LineString object at 0x...>]
Collections can also be sliced.
集合也可以执行切片操作。
>>> from shapely.geometry import MultiPoint
>>> m = MultiPoint([(0, 0), (1, 1), (1,2), (2,2)])
>>> m[:1].wkt
'MULTIPOINT (0.0000000000000000 0.0000000000000000)'
>>> m[3:].wkt
'MULTIPOINT (2.0000000000000000 2.0000000000000000)'
>>> m[4:].wkt
'GEOMETRYCOLLECTION EMPTY'
New in version 1.2.14.
Note
When possible, it is better to use one of the homogeneous collection types described below.
在可能的情况下,最好使用下面描述的同质化集合类型之一。