fre*_*281 55 python mapping math maps latitude-longitude
我希望能够估计两个(纬度,经度)点之间的距离.我想低估,因为这将是A*图搜索,我希望它快.这些点最多相距800公里.
Aar*_*n D 107
Python中Haversine公式的答案(两个GPS点之间的方位和距离)提供了回答您问题的Python实现.
使用下面的实现,我在一台旧笔记本电脑上在不到1秒的时间内完成了100,000次迭代.我认为,为了你的目的,这应该足够了.但是,在优化性能之前,您应该分析任何内容.
你想要的任何因素.我没有看到如何将错误引入低估是有用的.from math import radians, cos, sin, asin, sqrt
def haversine(lon1, lat1, lon2, lat2):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
"""
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
# Radius of earth in kilometers is 6371
km = 6371* c
return km
Tre*_*eyA 36
由于距离相对较小,因此可以使用等距离近似.这种近似比使用Haversine公式更快.因此,要获得从参考点(lat1/lon1)到您正在测试的点(lat2/lon2)的距离,请使用下面的公式.重要说明:您需要将所有纬度/经度点转换为弧度:
R = 6371 // radius of the earth in km
x = (lon2 - lon1) * cos( 0.5*(lat2+lat1) )
y = lat2 - lat1
d = R * sqrt( x*x + y*y )
Run Code Online (Sandbox Code Playgroud)
由于'R'以km为单位,因此距离'd'将以km为单位.
参考:http://www.movable-type.co.uk/scripts/latlong.html
如果点之间的距离相对较小(米到几公里范围),那么快速方法之一可能是
from math import cos, sqrt
def qick_distance(Lat1, Long1, Lat2, Long2):
x = Lat2 - Lat1
y = (Long2 - Long1) * cos((Lat2 + Lat1)*0.00872664626)
return 111.319 * sqrt(x*x + y*y)
Run Code Online (Sandbox Code Playgroud)
纬度、经度以弧度为单位,距离以公里为单位。
与半正矢距离的偏差约为 1%,而速度增益则超过约 10 倍。
0.00872664626 = 0.5 * 圆周率/180,
111.319 - 是对应于赤道 1 度的距离,您可以将其替换为您的中值,如下所示 https://www.cartographyunchained.com/cgsta1/ 或将其替换为简单的查找表。
| 归档时间: |
|
| 查看次数: |
75958 次 |
| 最近记录: |