You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Shapely计算与给定角度垂直的线段端点问题排查

问题:垂直线段计算后QGIS视觉与理论结果不符的排查

我需要用Shapely和三角函数计算一条过指定点、与给定角度垂直的线段端点,比如给定角度为90°时,线段的方位角应为0°或180°。

我编写了代码完成计算,将结果保存为shapefile导入QGIS后出现矛盾现象:

  • 视觉上线段与给定角度并不垂直
  • 计算线段的方位角得到的结果显示二者理论上是垂直的

想排查是QGIS可视化问题还是计算逻辑错误,寻求帮助。

代码实现

核心计算函数

import math
from shapely import Point, LineString
import geopandas as gpd


def calculate_endpoints(start_point, azimuth_deg, distance = 1000):

    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操作及现象

  1. 导入点shapefile,将点符号改为线段并旋转288.7°
  2. 导入线段shapefile,视觉上二者不垂直
  3. 计算线段方位角得到-140°,理论上该角度与原角度288.7°垂直

内容的提问来源于stack exchange,提问作者La Cordillera

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 03:48:24