【问题标题】:Shapely: Split LineString at arbitrary point along edge匀称:沿边缘在任意点分割 LineString
【发布时间】:2018-10-24 05:30:00
【问题描述】:

我正在尝试将 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))

谢谢!

【问题讨论】:

    标签: python shapely geopandas


    【解决方案1】:

    事实证明,我正在寻找的答案在 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)
    

    【讨论】:

      猜你喜欢
      • 2021-04-24
      • 2019-09-05
      • 2016-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-09
      • 2013-08-28
      相关资源
      最近更新 更多