我正在尝试使用形状和三角学来计算穿过一点、垂直于给定角度的线的端点。例如,如果我的角度为 90 度,则该线的方位角应为 0 或 180 度。端点顺序并不重要,因此两个值都可以正常工作。
这是代码的最小可重现示例:
import math
from shapely import Point, LineString
import geopandas as gpd
def calculate_endpoints(start_point, azimuth_deg, distance = 1000):
x, y = x, y = start_point.coords[0]
azimuth_rad = math.radians(azimuth_deg)
right_azimuth_rad = (azimuth_rad + math.pi / 2) % (2 * math.pi)
left_azimuth_rad = (azimuth_rad - math.pi / 2) % (2 * math.pi)
dx_right = distance * math.cos(right_azimuth_rad)
dy_right = distance * math.sin(right_azimuth_rad)
right_endpoint = (x + dx_right, y + dy_right)
dx_left = distance * math.cos(left_azimuth_rad)
dy_left = distance * math.sin(left_azimuth_rad)
left_endpoint = (x + dx_left, y + dy_left)
return LineString([right_endpoint, left_endpoint])
start_point = Point(8424, 1385)
azimuth = 288.7
如果我将结果保存到形状文件中:
gpd.GeoDataFrame( geometry=[calculate_endpoints(start_point, azimuth, 1000)]).to_file("line.shp")
gpd.GeoDataFrame( geometry=[start_point]).to_file("point.shp")
但是在QGIS中:
结果看起来两者并不成直角:
但是如果我计算线的方位角,
x1 = calculate_endpoints(start_point, azimuth, 1000).coords.xy[0][0]
y1 = calculate_endpoints(start_point, azimuth, 1000).coords.xy[0][1]
x2 = calculate_endpoints(start_point, azimuth, 1000).coords.xy[1][0]
y2 = calculate_endpoints(start_point, azimuth, 1000).coords.xy[1][1]
dx = x2-x1
dy = y2-y1
math.degrees(math.atan2(dy, dx))
结果是-140,大致垂直于原来的角度?那么我在 QGIS 中看到的这个错误是错误的,还是计算有问题? 如有任何帮助,我们将不胜感激。