>>> from env_helper import info; info()
页面更新时间: 2023-04-15 20:03:05
运行环境:
    Linux发行版本: Debian GNU/Linux 12 (bookworm)
    操作系统内核: Linux-6.1.0-7-amd64-x86_64-with-glibc2.36
    Python版本: 3.11.2

7.5. 生成新几何对象的方法

7.5.1. 集合论方法

几乎每一个二元谓词方法都有一个返回新几何对象的副本的方法。 且一个对象的集合论边界是作为只读属性得到的。

边界与中心

一个多边形的边界是一条线, 线的边界是一系列的点。一个点的边界是一个空集合。

>>> from shapely.geometry import LineString, MultiLineString, Point
>>> coords = [((0, 0), (1, 1)), ((-1, 0), (1, 0))]
>>> lines = MultiLineString(coords)
>>> lines.boundary
_images/sec5_sa_2_0.svg

获取多元要素的每个部分,在Shapely 2.0以前使用 lines.boundary ,在版本2.0后,使用下面的方法:

>>> len(lines.geoms)
2
>>> for geom in lines.geoms:
>>>     print(geom)
LINESTRING (0 0, 1 1)
LINESTRING (-1 0, 1 0)
>>> lines.boundary.boundary
_images/sec5_sa_6_0.svg
>>> lines.boundary.boundary.is_empty
True

object.centroid

返回几何对象的质心的代表。

>>> LineString([(0, 0), (1, 1)]).centroid
_images/sec5_sa_9_0.svg
>>> LineString([(0, 0), (1, 1)]).centroid.wkt
'POINT (0.5 0.5)'

注意:

一个对象的质心可能是它点中的一个,但是并不保证所有对象的质心都是这样。

7.5.2. 构建新要素的方法

Shapely几何对象有一些方法产生新的要素,而不是由集理论得到的对象。

object.buffer(distance,resolution=16)

返回一个几何对象指定距离内所有点的近似代表。

一个正的距离有扩张的效果;一个负的距离有减少的效果。 可选的分辨率参数决定一个点周围近似四分之一圈的段数。

>>> from shapely.geometry import LineString
>>> line = LineString([(0, 0), (1, 1), (0, 2), (2, 2), (3, 1), (1, 0)])
>>> dilated = line.buffer(0.5)
>>> eroded = dilated.buffer(-0.3)
>>> dilated
_images/sec5_sa_14_0.svg
>>> eroded
_images/sec5_sa_15_0.svg

一个点的默认缓冲区是与99.8%的圆盘面积的多边形补丁。

>>> from shapely.geometry import Point
>>> p = Point(0, 0).buffer(10.0)
>>> len(p.exterior.coords)
65
>>> p.area
313.6548490545941

分辨率为1,缓冲区是一个矩形补丁。例如:

>>> q = Point(0, 0).buffer(10.0, 1)
>>> len(q.exterior.coords)
5
>>> q
_images/sec5_sa_21_0.svg
>>> q.area
200.0

通过设定距离为0, buffer 可清除自接触或者自交多边形,例如经典的“领结”。

>>> from shapely.geometry import Polygon
>>> coords = [(0, 0), (0, 2), (1, 1), (2, 2), (2, 0), (1, 1), (0, 0)]
>>> bowtie = Polygon(coords)
>>> bowtie.is_valid
False
>>> bowtie
_images/sec5_sa_25_0.svg
>>> clean = bowtie.buffer(0)
>>> clean.is_valid
True
>>> clean
_images/sec5_sa_27_0.svg
>>> len(clean.geoms)
2
>>> list(clean.geoms[0].exterior.coords)
[(0.0, 0.0), (0.0, 2.0), (1.0, 1.0), (0.0, 0.0)]
>>> clean.geoms[0].exterior
_images/sec5_sa_30_0.svg
>>> list(clean.geoms[0].exterior.coords)
[(0.0, 0.0), (0.0, 2.0), (1.0, 1.0), (0.0, 0.0)]
>>> clean.geoms[0].exterior.coords
<shapely.coords.CoordinateSequence at 0x7fcffed64950>
>>> list(clean.geoms[0].exterior.coords)
[(0.0, 0.0), (0.0, 2.0), (1.0, 1.0), (0.0, 0.0)]

缓冲区在它们接触的点的地方将多边形一分为二。

object.convex_hull

返回一个包含对象中所有点的表示为最小凸多边形,除非对象中点的数量少于三个。 对于2个点,凸包折叠为一条线;一个点则折叠为一点。

>>> Point(0, 0).convex_hull
_images/sec5_sa_35_0.svg
>>> from shapely.geometry import MultiPoint
>>> MultiPoint([(0, 0), (1, 1)]).convex_hull
_images/sec5_sa_36_0.svg
>>> from shapely.geometry import MultiPoint
>>> MultiPoint([(0, 0), (1, 1), (1, -1)]).convex_hull
_images/sec5_sa_37_0.svg

object.envelope

返回那个点或包含那个对象的最小矩形多边形 (边平行于坐标轴)的代表。

>>> from shapely.geometry import Point
>>> Point(0, 0).envelope
_images/sec5_sa_39_0.svg
>>> from shapely.geometry import MultiPoint
>>> MultiPoint([(0, 0), (1, 1)]).envelope
_images/sec5_sa_40_0.svg

object.simplify(tolerance,preserve_topology=True)

返回一个几何对象的简化形式。

简化对象中的所有点将会在原始几何距离的容差内。 默认情况下,较慢的算法被用于保留拓扑结构。 如果保留拓扑结构设置为假,更快的道格拉斯 - 普克算法将被采用。

>>> p = Point(0.0, 0.0)
>>> x = p.buffer(1.0)
>>> x.area
3.1365484905459398
>>> len(x.exterior.coords)
65
>>> s = x.simplify(0.05, preserve_topology=False)
>>> s.area
3.061467458920719
>>> len(s.exterior.coords)
17

7.5.3. 二元操作

相减操作

object.difference(other)

返回组成这个几何对象的点的一个代表, 且这些点不组成其它对象。

>>> a = Point(1, 1).buffer(1.5)
>>> b = Point(2, 1).buffer(1.5)
>>> a.difference(b)
_images/sec5_sa_47_0.svg

图6.1为两个近似圆形多边形的 difference()

注意:

Shapely不能代表一个对象和作为单个对象的低维数对象的不同 (例如一个多边形与一条线或一个点的差异) 在这些例子中,不同的方法返回 一个叫做‘self’的对象的副本。

相交操作

object.intersection(other) , 返回一个对象与另一个几何对象交集的代表。

>>> a = Point(1, 1).buffer(1.5)
>>> b = Point(2, 1).buffer(1.5)
>>> a.intersection(b)
_images/sec5_sa_49_0.svg

object.symmetric_difference(other) 返回一个在这个对象不在另一个几何对象的点的代表, 并且其它对象的点不在这个几何对象中。

>>> a = Point(1, 1).buffer(1.5)
>>> b = Point(2, 1).buffer(1.5)
>>> a.symmetric_difference(b)
_images/sec5_sa_51_0.svg

object.union(other)

返回这个对象和其它几何对象间的点的集合的代表。

返回对象的类型取决于运算对象之间的关系。 例如多边形的合并,取决于它们是否相交, 结果则是一个多边形或者是一个的多元多边形。

>>> a = Point(1, 1).buffer(1.5)
>>> b = Point(2, 1).buffer(1.5)
>>> a.union(b)
_images/sec5_sa_53_0.svg

这些操作的语义随着几何对象类型的不同而变化。例如, 比较合并之后多边形的边界,与多边形边界的合并结果。

>>> a.union(b).boundary
_images/sec5_sa_55_0.svg
>>> a.boundary.union(b.boundary)
_images/sec5_sa_56_0.svg

注意:

union() 是一个找到许多对象的累积集合的昂贵的方式。 shapely.ops.cascaded_union() 是一个更有效的方法。