I want to be able to get a estimate of the distance between two (latitude, longitude) points. I want to undershoot, as this will be for A* graph search and I want it to be fast. The points will be at most 800 km apart.
我想要得到两个点(纬度,经度)之间的距离的估计值。我想要拍下,因为这将是一个*图搜索,我希望它快速。这两个点之间的距离最多为800公里。
5 个解决方案
#1
93
The answers to Haversine Formula in Python (Bearing and Distance between two GPS points) provide Python implementations that answer your question.
Python中的Haversine公式(两个GPS点之间的方位和距离)的答案提供了Python实现,可以回答您的问题。
Using the implementation below I performed 100,000 iterations in less than 1 second on an older laptop. I think for your purposes this should be sufficient. However, you should profile anything before you optimize for performance.
使用下面的实现,我在不到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
To underestimate haversine(lat1, long1, lat2, long2) * 0.90
or whatever factor you want. I don't see how introducing error to your underestimation is useful.
低估haversine(lat1、long1、lat2、long2) * 0.90或任何你想要的因素。我看不出给你的低估加上误差有多大用处。
#2
29
Since the distance is relatively small, you can use the equirectangular distance approximation. This approximation is faster than using the Haversine formula. So, to get the distance from your reference point (lat1/lon1) to the point your are testing (lat2/lon2), use the formula below. Important Note: you need to convert all lat/lon points to radians:
由于距离相对较小,可以使用等距距离近似。这个近似比用哈夫辛公式快。因此,要获取从参考点(lat1/lon1)到测试点(lat2/lon2)的距离,请使用下面的公式。重要提示:您需要将所有lat/lon点转换为弧度:
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 )
Since 'R' is in km, the distance 'd' will be in km.
因为R是以km为单位的,所以d的距离是km。
Reference: http://www.movable-type.co.uk/scripts/latlong.html
参考:http://www.movable-type.co.uk/scripts/latlong.html
#3
7
One idea for speed is to transform the long/lat coordinated into 3D (x,y,z) coordinates. After preprocessing the points, use the Euclidean distance between the points as a quickly computed undershoot of the actual distance.
速度的一个概念是将长/低的坐标转换为3D (x,y,z)坐标。在对这些点进行预处理之后,使用这些点之间的欧几里得距离作为实际距离的快速计算不足。
#4
3
For maximal speed, you could create something like a rainbow table for coordinate distances. It sounds like you already know the area that you are working with, so it seems like pre-computing them might be feasible. Then, you could load the nearest combination and just use that.
对于最大速度,您可以为坐标距离创建彩虹表之类的东西。听起来好像你已经知道了你正在研究的领域,所以似乎预先计算它们是可行的。然后,您可以加载最近的组合并使用它。
For example, in the continental United States, the longitude is a 55 degree span and latitude is 20, which would be 1100 whole number points. The distance between all the possible combinations is a handshake problem which is answered by (n-1)(n)/2 or about 600k combinations. That seems pretty feasible to store and retrieve. If you provide more information about your requirements, I could be more specific.
例如,在美国大陆,经度是55度跨度,纬度是20度,也就是1100个整数点。所有可能的组合之间的距离是一个握手问题,可以用(n-1)(n) (n)/2或大约600k的组合来回答。这似乎是相当可行的存储和检索。如果你提供更多关于你的需求的信息,我可以更具体。
#5
0
Please use the following code.
请使用以下代码。
def distance(lat1, lng1, lat2, lng2):
#return distance as meter if you want km distance, remove "* 1000"
radius = 6371 * 1000
dLat = (lat2-lat1) * math.pi / 180
dLng = (lng2-lng1) * math.pi / 180
lat1 = lat1 * math.pi / 180
lat2 = lat2 * math.pi / 180
val = sin(dLat/2) * sin(dLat/2) + sin(dLng/2) * sin(dLng/2) * cos(lat1) * cos(lat2)
ang = 2 * atan2(sqrt(val), sqrt(1-val))
return radius * ang
#1
93
The answers to Haversine Formula in Python (Bearing and Distance between two GPS points) provide Python implementations that answer your question.
Python中的Haversine公式(两个GPS点之间的方位和距离)的答案提供了Python实现,可以回答您的问题。
Using the implementation below I performed 100,000 iterations in less than 1 second on an older laptop. I think for your purposes this should be sufficient. However, you should profile anything before you optimize for performance.
使用下面的实现,我在不到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
To underestimate haversine(lat1, long1, lat2, long2) * 0.90
or whatever factor you want. I don't see how introducing error to your underestimation is useful.
低估haversine(lat1、long1、lat2、long2) * 0.90或任何你想要的因素。我看不出给你的低估加上误差有多大用处。
#2
29
Since the distance is relatively small, you can use the equirectangular distance approximation. This approximation is faster than using the Haversine formula. So, to get the distance from your reference point (lat1/lon1) to the point your are testing (lat2/lon2), use the formula below. Important Note: you need to convert all lat/lon points to radians:
由于距离相对较小,可以使用等距距离近似。这个近似比用哈夫辛公式快。因此,要获取从参考点(lat1/lon1)到测试点(lat2/lon2)的距离,请使用下面的公式。重要提示:您需要将所有lat/lon点转换为弧度:
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 )
Since 'R' is in km, the distance 'd' will be in km.
因为R是以km为单位的,所以d的距离是km。
Reference: http://www.movable-type.co.uk/scripts/latlong.html
参考:http://www.movable-type.co.uk/scripts/latlong.html
#3
7
One idea for speed is to transform the long/lat coordinated into 3D (x,y,z) coordinates. After preprocessing the points, use the Euclidean distance between the points as a quickly computed undershoot of the actual distance.
速度的一个概念是将长/低的坐标转换为3D (x,y,z)坐标。在对这些点进行预处理之后,使用这些点之间的欧几里得距离作为实际距离的快速计算不足。
#4
3
For maximal speed, you could create something like a rainbow table for coordinate distances. It sounds like you already know the area that you are working with, so it seems like pre-computing them might be feasible. Then, you could load the nearest combination and just use that.
对于最大速度,您可以为坐标距离创建彩虹表之类的东西。听起来好像你已经知道了你正在研究的领域,所以似乎预先计算它们是可行的。然后,您可以加载最近的组合并使用它。
For example, in the continental United States, the longitude is a 55 degree span and latitude is 20, which would be 1100 whole number points. The distance between all the possible combinations is a handshake problem which is answered by (n-1)(n)/2 or about 600k combinations. That seems pretty feasible to store and retrieve. If you provide more information about your requirements, I could be more specific.
例如,在美国大陆,经度是55度跨度,纬度是20度,也就是1100个整数点。所有可能的组合之间的距离是一个握手问题,可以用(n-1)(n) (n)/2或大约600k的组合来回答。这似乎是相当可行的存储和检索。如果你提供更多关于你的需求的信息,我可以更具体。
#5
0
Please use the following code.
请使用以下代码。
def distance(lat1, lng1, lat2, lng2):
#return distance as meter if you want km distance, remove "* 1000"
radius = 6371 * 1000
dLat = (lat2-lat1) * math.pi / 180
dLng = (lng2-lng1) * math.pi / 180
lat1 = lat1 * math.pi / 180
lat2 = lat2 * math.pi / 180
val = sin(dLat/2) * sin(dLat/2) + sin(dLng/2) * sin(dLng/2) * cos(lat1) * cos(lat2)
ang = 2 * atan2(sqrt(val), sqrt(1-val))
return radius * ang