python - 访问 Shapely MultiPoint 中的各个点

标签 python shapely

我正在使用 Python 中的 Shapely 库。我找到两条线的交点,返回值作为 MultiPoint 对象给出。

如何解构对象以获得交点中的各个点?

代码如下:

from shapely.geometry import LineString, MultiLineString
a = LineString([(0, 1), (0, 2), (1, 1), (2, 0)])
b = LineString([(0, 0), (1, 1), (2, 1), (2, 0)])
x = a.intersection(b)

输出:

print(x) 
MULTIPOINT (1 1, 2 0)

因此,在这种情况下,我会寻找一种方法来提取交点 (1,1) 和 (2,0)。

最佳答案

您可以索引生成的多点:

>>> str(x)
'MULTIPOINT (1 1, 2 0)'
>>> print(len(x))
2
>>> print(x[0].x)
1.0
>>> print(x[0].y)
1.0

如果你想要一个带有坐标的新列表,你可以使用:

>>> [(p.x, p.y) for p in x]
[(1.0, 1.0), (2.0, 0.0)]

关于python - 访问 Shapely MultiPoint 中的各个点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51060842/

相关文章:

python - 如何更新 Mac 上的 Mercurial 以使用 CAC 身份验证

python - 在另一条线的一点上绘制固定长度的垂直线

python - 如何在 pyspark 中获取 Python 库?

python - gis 计算点和多边形/边界之间的距离

Python(也许是Shapely)创建缓冲区并计数点

python - 为什么 'all' 实现比写 for 循环慢

python - Pandas 分为两个多索引系列

python - 在 python 2.7.10、Mac OSX 下机械化的 SSLv3 握手失败

Python找出编译模块的内容?

python - 使用 ccrs.epsg() 使用 EPSG 4326 坐标系绘制邮政编码周长形状文件