根据 Python 中的 GPS 坐标计算基本方向

Calculate cardinal direction from GPS coordinates in Python

如何计算Python中地理坐标第二点的基本方向? 我需要知道两个不同位置的距离和方向。

示例: 工作:坐标 41.4107628,2.1745004 家:坐标 41.4126728,2.1704725

from geopy.distance import vincenty
work = geopy.Point(41.4107628,2.1745004)
home = geopy.Point(41.4126728,2.1704725)
print(vincenty(home, work))
0.398011015257 km

我想知道第二点相对于第一点的方向(例如:北、西北等)...

非常感谢

为清楚起见编辑:这是一个近似值,可以说明大致方向。使用下面的 link 获取简单的在线计算器。否则请遵循提供的其他答案。

坐标系中第一个值表示North/South方向,第二个值表示East/West。简单的减法将提供一个总体方向。例如从 A 中减去 B 我们得到:

41.4126728 - 41.4107628 = 0.00191

2.1704725 - 2.1745004 = - 0.0040279

这表示要从 A 点到达 B 点,您需要向北(正值)向西(负值)方向行驶。 可以使用三角函数找到精确的角度(将每个值视为直角三角形的边)。如评论中所述,三角函数不足以进行精确计算。

您可能会发现这个网站很有趣:https://www.movable-type.co.uk/scripts/latlong.html

使用我的 python 包 geographiclib.

之后

pip install geographiclib

$ python
Python 2.7.14 (default, Nov  2 2017, 18:42:05) 
>>> from geographiclib.geodesic import Geodesic
>>> geod = Geodesic.WGS84
>>> g = geod.Inverse(41.4107628,2.1745004, 41.4126728,2.1704725)
>>> print "The initial direction is {:.3f} degrees.".format(g['azi1'])
The initial direction is -57.792 degrees.

方向是从北向顺时针方向测量的。所以 -57.8 度 = 302.2 度 = 西北偏西;参见 Points of the compass

FWIW,我需要北、南、东、西等作为文本。 这是我的(旧式程序员)代码:

import math

def calcBearing (lat1, long1, lat2, long2):
    dLon = (long2 - long1)
    x = math.cos(math.radians(lat2)) * math.sin(math.radians(dLon))
    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))
    bearing = math.atan2(x,y)   # use atan2 to determine the quadrant
    bearing = math.degrees(bearing)

    return bearing

def calcNSEW(lat1, long1, lat2, long2):
    points = ["north", "north east", "east", "south east", "south", "south west", "west", "north west"]
    bearing = calcBearing(lat1, long1, lat2, long2)
    bearing += 22.5
    bearing = bearing % 360
    bearing = int(bearing / 45) # values 0 to 7
    NSEW = points [bearing]

    return NSEW

# White house 38.8977° N, 77.0365° W
lat1 = 38.8976763
long1 = -77.0365298
# Lincoln memorial 38.8893° N, 77.0506° W
lat2 = 38.8893
long2 = -77.0506

points = calcNSEW(lat1, long1, lat2, long2)
print ("The Lincoln memorial is " + points + " of the White House")
print ("Actually bearing of 231.88 degrees")

print ("Output says: The Lincoln memorial is south west of the White House ")