如何计算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
我想知道第二点位于哪个方向(例如:北,西北等)尊重第一个方面...
非常感谢
使用我的python pacakgeGeographiclib。
之后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度=西北;参见指南针的点。
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 ")
为了清楚起见:这是一个近似值,可以告诉一般方向。使用下面的链接进行简单的在线计算器。否则请按照提供的其他答案。
坐标系中的第一个值代表北/南方,第二个代表东/西。简单的减法将提供一般方向。例如,从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