gpt4 book ai didi

python - Shapely - 从复杂的线串中获取交叉点的坐标

转载 作者:太空宇宙 更新时间:2023-11-03 14:22:06 28 4
gpt4 key购买 nike

我创建了复杂的线串,如下所示:

from shapely.geometry import LineString

complex_line = LineString([(-1, -1), (1, 1), (1, 0), (-1, 0)])

该行有效,但并不简单。我怎样才能得到交叉点(这里是[0,0])?

最佳答案

您希望在自相交之后的某个点分割线。然后你可以执行isect_pnt = first.intersection(second)。诀窍是在自相交后找到该点。

因此,我将循环遍历从 0.1 到 0.9 的“分割分数”,并在总长度的每个分数处分割几何图形,看看 shapely 是否可以找到有效的交点。如果可以的话,我们就可以跳出循环并停止搜索。对于自相交之前的分割,isect_pnt = first.intersects(second) 将返回 False,我们可以继续搜索。

这里的一个细微差别是我将使用 interpolate 方法以更精细的分辨率创建新的几何图形

import numpy
from shapely.geometry import LineString, Point

complex_line = LineString([(-1, -1), (1, 1), (1, 0), (-1, 0)])

# initial, non-result
intersection = None

# fraction of total distance at which we'll split the line
for split in numpy.arange(0.1, 1, 0.1):

full_len = complex_line.length
split_len = full_len * split

# endpoint = False to make sure we don't get a false-positive
# at the split point
first = LineString([
complex_line.interpolate(d)
for d in numpy.linspace(0, split_len, num=25, endpoint=False)
])

second = LineString([
complex_line.interpolate(d)
for d in numpy.linspace(split_len, full_len, num=25)
])

if first.intersects(second):
intersection = first.intersection(second)
break

print(intersection)

在我的机器上,输出:

POINT (0 0)

这不是很好的优化。在大型、复杂的几何形状上,我可以想象这可能需要对线的每一半进行更解析的插值最终速度相当慢。

关于python - Shapely - 从复杂的线串中获取交叉点的坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47873465/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com