计算垂直于给定角度的点的线串端点

问题描述 投票:0回答:1

我正在尝试使用形状和三角学来计算穿过一点、垂直于给定角度的线的端点。例如,如果我的角度为 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中:

  • 导入点shapefile并将点符号系统更改为线,并将其旋转288.7度
  • 添加线形状文件

结果看起来两者并不成直角:

但是如果我计算线的方位角,

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 中看到的这个错误是错误的,还是计算有问题? 如有任何帮助,我们将不胜感激。

geopandas shapefile qgis shapely azimuth
1个回答
0
投票

你的计算是正确的。然而,相对于 X 轴的 288.7° 角应该如下所示:

根据QGIS文档,点的标记可以是一条垂直线。另外,QGIS 中相对于北向的旋转是顺时针的,请参阅这篇文章,所以结果是这样的:

如果您希望标记正确,您需要将 QGIS 中的标记旋转角度更改为 90 + (360-277.8) = 161.3°

© www.soinside.com 2019 - 2024. All rights reserved.