在Python中从GPS坐标计算基数方向

时间:2017-12-05 17:22:02

标签: python gps coordinates directions bearing

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

示例: 工作:coord 41.4107628,2.1745004 home:coord 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

我想知道第二点所在的方向,(例如:北方,西北方等)对第一点的尊重......

非常感谢提前

3 个答案:

答案 0 :(得分:2)

使用我的python pacakge geographiclib

pip install geographiclib

DO

$ 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

答案 1 :(得分:1)

坐标系中的第一个值表示北/南方向,第二个值表示东/西方向。简单的减法将提供一般方向。例如,从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

答案 2 :(得分:0)

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 ")