简体   繁体   English

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

[英]How to create a line in Shapely using starting point, length, and an angle

I found this code but it requires both first and second point to create a line.我找到了这段代码,但它需要第一个和第二个点来创建一条线。 How can I change it so it works with only first point, length of the line, and an angle?如何更改它以使其仅适用于第一个点、线的长度和角度?

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)

You have several options:你有几个选择:

  1. Calculate the coordinates of the second point using basic trigonometry:使用基本三角法计算第二个点的坐标:
     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)
    If your angle is not in radians but in degrees, you should convert it first:如果你的角度不是弧度而是度数,你应该先转换它:
     angle = 60 angle = math.radians(angle)
  1. Make a horizontal line with the given start point and length, and then rotate it by given angle around the first point:用给定的起点和长度制作一条水平线,然后围绕第一个点将其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)

    Note that by default rotate function expects angles in degrees, so if we want to pass an angle in radians to it, we have to use the use_radians=True as shown above.请注意,默认情况下rotate function 需要以度为单位的角度,所以如果我们想将一个以弧度表示的角度传递给它,我们必须使用use_radians=True ,如上所示。

    Alternatively, we could also use translate function to get the endpoint:或者,我们也可以使用translate function 来获取端点:

     from shapely.affinity import translate end = translate(start, length)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM