简体   繁体   English

计算坐标列表之间的地理距离(lat,lng)

[英]Calculating geographic distance between a list of coordinates (lat, lng)

I'm writing a flask application, using some data extracted from a GPS sensor. 我正在编写一个烧瓶应用程序,使用从GPS传感器中提取的一些数据。 I am able to draw the route on a Map and I want to calculate the distance the GPS sensor traveled. 我能够在地图上绘制路线,我想计算GPS传感器行进的距离。 One way could be to just get the start and end coordinates, however due to the way the sensor travels this is quite inaccurate. 一种方法是获得开始和结束坐标,但是由于传感器行进的方式,这是非常不准确的。 Therefore I do sampling of each 50 sensor samples. 因此,我对每50个传感器样本进行采样。 If the real sensor sample size was 1000 I will now have 20 samples (by extracting each 50 sample). 如果实际传感器样本大小为1000,我现在将有20个样本(通过提取每50个样本)。

Now I want to be able to put my list of samples through a function to calculate distance. 现在我希望能够通过函数放置我的样本列表来计算距离。 So far I've been able to use the package geopy, but when I take large gps sample sets I do get "too many requests" errors, not to mention I will have extra processing time from processing the requests, which is not what I want. 到目前为止,我已经能够使用包的geopy,但是当我采用大型gps样本集时,我确实会收到“请求太多”的错误,更不用说处理请求会有额外的处理时间,这不是我的意思。想。

Is there a better approach to calculating the cumulative distance of a list element containing latitude and longitude coordinates? 有没有更好的方法来计算包含纬度和经度坐标的列表元素的累积距离?

positions = [(lat_1, lng_1), (lat_2, lng_2), ..., (lat_n, lng_n)]

I found methods for lots of different mathematical ways of calculating distance using just 2 coordinates (lat1, lng1 and lat2 and lng2), but none supporting a list of coordinates. 我找到了许多不同数学方法的方法,只用2个坐标(lat1,lng1和lat2和lng2)计算距离,但没有一个支持坐标列表。

Here's my current code using geopy: 这是我使用geopy的当前代码:

from geopy.distance import vincenty

def calculate_distances(trips):
    temp = {}
    distance = 0
    for trip in trips:
        positions = trip['positions']
        for i in range(1, len(positions)):
            distance += ((vincenty(positions[i-1], positions[i]).meters) / 1000)
            if i == len(positions):
                temp = {'distance': distance}
                trip.update(temp)
                distance = 0

trips is a list element containing dictionaries of key-value pairs of information about a trip (duration, distance, start and stop coordinates and so forth) and the positions object inside trips is a list of tuple coordinates as visualized above. trips是含有约一个行程的信息的键-值对(持续时间,距离,启动和停止的坐标等)的字典的列表元素和位置对象内部行程是元组的坐标的列表如上述可视化。

trips = [{data_1}, {data_2}, ..., {data_n}]

I'd recommend transform your (x, y) coordinates into complex, as it is computational much easier to calculate distances. 我建议将你的(x,y)坐标转换成复数,因为计算距离更容易计算。 Thus, the following function should work: 因此,以下功能应该起作用:

def calculate_distances(trips):
    for trip in trips:
        positions = trip['positions']
        c_pos = [complex(c[0],c[1]) for c in positions]
        distance = 0
        for i in range(1, len(c_pos)):
            distance += abs(c_pos[i] - c_pos[i-1])
        trip.update({'distance': distance})

What I'm doing is converting every (lat_1, lng_1) touple into a single complex number c1 = lat_1 + j*lng_1 , and creates a list formed by [c1, c2, ... , cn] . 我正在做的是将每个(lat_1, lng_1) touple转换为单个复数c1 = lat_1 + j*lng_1 ,并创建一个由[c1, c2, ... , cn]组成的列表。

A complex number is, all in all, a 2-dimensional number and, therefore, you can make this if you have 2D coordinates, which is perfect for geolocalization, but wouldn't be possible for 3D space coordinates, for instance. 总的来说,复数是一个二维数,因此,如果你有二维坐标,你就可以做到这一点,这对于地理定位来说是完美的,但对于三维空间坐标是不可能的。

Once you got this, you can easily compute the distance between two complex numbers c1 and c2 as dist12 = abs(c2 - c1) . 一旦你得到了这个,你就可以很容易地计算出两个复数c1c2之间的距离,因为dist12 = abs(c2 - c1) Doing this recursively you obtain the total distance. 递归地执行此操作可获得总距离。

Hope this helped! 希望这有帮助!

Here's the solution I ended up using. 这是我最终使用的解决方案。 It's called the Haversine (distance) function if you want to look up what it does for yourself. 如果你想查找它为自己做的事情,它被称为Haversine(距离)功能。

I changed my approach a little as well. 我也改变了一点方法。 My input ( positions ) is a list of tuple coordinates: 我的输入( positions )是元组坐标列表:

def calculate_distance(positions):
    results = []
    for i in range(1, len(positions)):
        loc1 = positions[i - 1]
        loc2 = positions[i]

        lat1 = loc1[0]
        lng1 = loc1[1]

        lat2 = loc2[0]
        lng2 = loc2[1]

        degreesToRadians = (math.pi / 180)
        latrad1 = lat1 * degreesToRadians
        latrad2 = lat2 * degreesToRadians
        dlat = (lat2 - lat1) * degreesToRadians
        dlng = (lng2 - lng1) * degreesToRadians

        a = math.sin(dlat / 2) * math.sin(dlat / 2) + math.cos(latrad1) * \
        math.cos(latrad2) * math.sin(dlng / 2) * math.sin(dlng / 2)
        c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
        r = 6371000

        results.append(r * c)

    return (sum(results) / 1000)  # Converting from m to km

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM