计算轴承的差异

Difference in calculating bearing

我正在尝试计算真北(38.16418244422394, -38.933453981070926)

海洋中的一个点(lat, long)之间的方位角

我阅读了 and this post 并看到了许多不同的建议。

使用 geographiclib,我得到 -38.806138500542176 的值。但是手动计算,我得到 297.9944573116836。为什么我得到两个不同的答案?哪一个是正确的?

最小示例:

import math
from geographiclib.geodesic import Geodesic

def get_bearing(lat1,lon1,lat2,lon2):
    dLon = lon2 - lon1
    y = math.sin(dLon) * math.cos(lat2)
    x = math.cos(lat1)*math.sin(lat2) - math.sin(lat1)*math.cos(lat2)*math.cos(dLon)
    brng = np.rad2deg(math.atan2(y, x))
    if brng < 0: brng+= 360
    return brng

def get_bearing_geodesic(lat1, lon1, lat2, lon2):
    Geodesic.WGS84.Inverse(lat1, lon1, lat2, lon2)
    return Geodesic.WGS84.Inverse(lat1, lon1, lat2, lon2)['azi1']

true_north_lat = 0
true_north_lon = 0
oil_rig_lat = 38.16418244422394
oil_rig_lon = -38.933453981070926

print("Manually: {}".format(get_bearing(true_north_lat, true_north_lon, oil_rig_lat, oil_rig_lon)))
print("Geodesic: {}".format(get_bearing_geodesic(true_north_lat, true_north_lon, oil_rig_lat, oil_rig_lon)))

正确答案是什么?他们为什么不同?哪个答案得出从真北到石油钻井平台的角度(以度为单位)?

math.sinmath.cos 以弧度为参数。把手动函数改成这样(最后返回度数):

def get_bearing(lat1,lon1,lat2,lon2):
    dLon = lon2 - lon1
    y = math.cos(math.radians(lat1)) * math.sin(math.radians(lat2)) - math.sin(math.radians(lat1)) * math.cos(math.radians(lat2)) * math.cos(math.radians(dLon))
    x = math.cos(math.radians(lat2)) * math.sin(math.radians(dLon))
    brng = math.degrees(math.atan2(x, y))
    if brng < 0: brng+= 360
    return brng

>>> get_bearing(true_north_lat, true_north_lon, oil_rig_lat, oil_rig_lon)
321.3540270387211