如何使用起点、长度和角度在 Shapely 中创建线条

use*_*383 5 python geometry shapely

我找到了这段代码,但它需要第一点和第二点来创建一条线。我怎样才能改变它,使它只适用于第一个点、线的长度和角度?

from shapely.geometry import LineString
from shapely.geometry import Point

p = Point(5,5)
c = p.buffer(3).boundary
l = LineString([(0,0), (10, 10)])
i = c.intersection(l)

print i.geoms[0].coords[0]
(2.8786796564403576, 2.8786796564403576)

print i.geoms[1].coords[0]
(7.121320343559642, 7.121320343559642)
Run Code Online (Sandbox Code Playgroud)

Geo*_*rgy 8

您有多种选择:

  1. 使用基本三角函数计算第二个点的坐标:
    import math
    from shapely.geometry import LineString, Point
    
    start = Point(0, 0)
    length = 1
    angle = math.pi / 3
    
    end = Point(start.x + length * math.cos(angle),
                start.y + length * math.sin(angle))
    line = LineString([start, end])
    print(line)
    # LINESTRING (0 0, 0.5000000000000001 0.8660254037844386)
    
    Run Code Online (Sandbox Code Playgroud) 如果你的角度不是弧度而是度数,你应该先转换它:
    angle = 60
    angle = math.radians(angle)
    
    Run Code Online (Sandbox Code Playgroud)
  1. 使用给定的起点和长度绘制一条水平线,然后rotate围绕第一个点按给定的角度:

    from shapely.affinity import rotate
    from shapely.geometry import LineString, Point
    
    start = Point(0, 0)
    length = 1
    angle = math.pi / 3
    
    end = Point(start.x + length, start.y)
    line = LineString([start, end])
    line = rotate(line, angle, origin=start, use_radians=True)
    print(line)
    # LINESTRING (0 0, 0.5000000000000001 0.8660254037844386)
    
    Run Code Online (Sandbox Code Playgroud)

    请注意,默认情况下rotate函数需要以度为单位的角度,因此如果我们想向其传递以弧度为单位的角度,我们必须使用use_radians=True如上所示的。

    或者,我们也可以使用translate函数来获取端点:

    from shapely.affinity import translate
    end = translate(start, length)
    
    Run Code Online (Sandbox Code Playgroud)