gpt4 book ai didi

python - Shapely:沿边缘在任意点拆分 LineString

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

我正在尝试在离其他坐标最近的点处拆分 Shapely LineString。我可以使用 projectinterpolate 获得直线上的最近点,但此时我无法拆分直线,因为它不是顶点。

我需要沿着边分割线,而不是捕捉到最近的顶点,这样最近的点就成为线上的新顶点。

这是我到目前为止所做的:

from shapely.ops import split
from shapely.geometry import Point, LineString

line = LineString([(0, 0), (5,8)])
point = Point(2,3)

# Find coordinate of closest point on line to point
d = line.project(point)
p = line.interpolate(d)
print(p)
# >>> POINT (1.910112359550562 3.056179775280899)

# Split the line at the point
result = split(line, p)
print(result)
# >>> GEOMETRYCOLLECTION (LINESTRING (0 0, 5 8))

谢谢!

最佳答案

事实证明,我正在寻找的答案已在 documentation 中列出。作为 cut 方法:

def cut(line, distance):
# Cuts a line in two at a distance from its starting point
if distance <= 0.0 or distance >= line.length:
return [LineString(line)]
coords = list(line.coords)
for i, p in enumerate(coords):
pd = line.project(Point(p))
if pd == distance:
return [
LineString(coords[:i+1]),
LineString(coords[i:])]
if pd > distance:
cp = line.interpolate(distance)
return [
LineString(coords[:i] + [(cp.x, cp.y)]),
LineString([(cp.x, cp.y)] + coords[i:])]

现在我可以使用 projected 距离来切割 LineString:

...
d = line.project(point)
# print(d) 3.6039927920216237

cut(line, d)
# LINESTRING (0 0, 1.910112359550562 3.056179775280899)
# LINESTRING (1.910112359550562 3.056179775280899, 5 8)

关于python - Shapely:沿边缘在任意点拆分 LineString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50332273/

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