简体   繁体   English

如何快速估计两个(纬度、经度)点之间的距离?

[英]How can I quickly estimate the distance between two (latitude, longitude) points?

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 .我想低于,因为这将用于 A* 图形搜索,并且我希望它很快 The points will be at most 800 km apart.这些点最多相距 800 公里。

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.使用下面的实现,我在较旧的笔记本电脑上在不到 1 秒的时间内执行了 100,000 次迭代 I think for your purposes this should be sufficient.我认为就您的目的而言,这应该足够了。 However, you should profile anything before you optimize for performance.但是,在优化性能之前,您应该分析任何内容。

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.低估haversine(lat1, long1, lat2, long2) * 0.90或您想要的任何因素。 I don't see how introducing error to your underestimation is useful.我不明白在你的低估中引入错误有什么用。

Since the distance is relatively small, you can use the equirectangular distance approximation.由于距离相对较小,您可以使用等距柱状距离近似值。 This approximation is faster than using the Haversine formula.这种近似比使用Haversine 公式更快。 So, to get the distance from your reference point (lat1/lon1) to the point your are testing (lat2/lon2), use the formula below.因此,要获得从参考点 (lat1/lon1) 到您要测试的点 (lat2/lon2) 的距离,请使用以下公式。 Important Note: you need to convert all lat/lon points to radians:重要提示:您需要将所有经纬度点转换为弧度:

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”以公里为单位,因此距离“d”将以公里为单位。

Reference: http://www.movable-type.co.uk/scripts/latlong.html参考: http : //www.movable-type.co.uk/scripts/latlong.html

One idea for speed is to transform the long/lat coordinated into 3D (x,y,z) coordinates.速度的一种想法是将经纬度坐标转换为 3D (x,y,z) 坐标。 After preprocessing the points, use the Euclidean distance between the points as a quickly computed undershoot of the actual distance.对点进行预处理后,使用点之间的欧几里得距离作为实际距离的快速计算下冲。

If the distance between points is relatively small (meters to few km range) then one of the fast approaches could be如果点之间的距离相对较小(几米到几公里的范围),那么快速方法之一可能是

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)

Lat, Long are in radians, distance in km. Lat, Long 以弧度为单位,距离以公里为单位。

Deviation from Haversine distance is in the order of 1%, while the speed gain is more than ~10x.与 Haversine 距离的偏差约为 1%,而速度增益超过 ~10 倍。

0.00872664626 = 0.5 * pi/180, 0.00872664626 = 0.5 * pi/180,

111.319 - is the distance that corresponds to 1degree at Equator, you could replace it with your median value like here https://www.cartographyunchained.com/cgsta1/ or replace it with a simple lookup table. 111.319 - 是对应于赤道 1 度的距离,你可以用你的中值替换它,就像这里https://www.cartographyunchained.com/cgsta1/或用一个简单的查找表替换它。

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.例如,在美国大陆,经度为 55 度跨度,纬度为 20,这将是 1100 个整数点。 The distance between all the possible combinations is a handshake problem which is answered by (n-1)(n)/2 or about 600k combinations.所有可能组合之间的距离是一个握手问题,由 (n-1)(n)/2 或大约 600k 组合回答。 That seems pretty feasible to store and retrieve.存储和检索这似乎非常可行。 If you provide more information about your requirements, I could be more specific.如果您提供有关您的要求的更多信息,我可以更具体。

You can use cdist from scipy spacial distance class:您可以使用cdist空间距离类中的scipy

For example :例如

from scipy.spatial.distance import cdist 
df1_latlon = df1[['lat','lon']]
df2_latlon = df2[['lat', 'lon']]
distanceCalc = cdist(df1_latlon, df2_latlon, metric=haversine)

To calculate a haversine distance between 2 points u can simply use mpu.haversine_distance() library, like this:要计算两点之间的半正弦距离,您可以简单地使用mpu.haversine_distance()库,如下所示:

>>> import mpu
>>> munich = (48.1372, 11.5756)
>>> berlin = (52.5186, 13.4083)
>>> round(mpu.haversine_distance(munich, berlin), 1)
>>> 504.2

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 最小化两个经度点之间的距离? - Minimize distance between two latitude-longitude points? 使用纬度经度和高度(海拔)计算两点之间的距离 - Calculating distance between two points using latitude longitude and altitude (elevation) 根据纬度/经度获取两点之间的距离 - Getting distance between two points based on latitude/longitude 如何在不做循环的情况下找到给定经度和纬度的两点之间的距离? - How to find the distance between two points given longitude and latitude without do loop? 如何在python-error中找到在给定经度和纬度的两点之间的距离? - How to find the distance between two points given longitude and latitude in python-error? 基于纬度/经度的点列表之间的距离 - Distance between list of points based on latitude/longitude 具有纬度和经度的点集合之间的成对距离 - Pairwise distance between collections of points with latitude and longitude 使用距离矩阵在Python中计算经纬度点之间的距离 - Using distance matrix to calculate distance between points with latitude and longitude in Python Pandas 经纬度:计算两点与select最近的距离 - Pandas Latitude-Longitude: Calculate the distance between two points and select the nearest one GeoDjango:我怎样才能得到两点之间的距离? - GeoDjango: How can I get the distance between two points?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM