计算坐标列表(纬度,经度)之间的地理距离 [英] Calculating geographic distance between a list of coordinates (lat, lng)

查看:349
本文介绍了计算坐标列表(纬度,经度)之间的地理距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用从GPS传感器提取的一些数据编写烧瓶应用程序.我能够在地图上绘制路线,并且想要计算GPS传感器行进的距离.一种方法可能只是获取开始和结束坐标,但是由于传感器的行进方式,这是非常不准确的.因此,我对每个50个传感器样本进行采样.如果实际的传感器样本量为1000,我现在将有20个样本(通过提取50个样本).

I'm writing a flask application, using some data extracted from a GPS sensor. I am able to draw the route on a Map and I want to calculate the distance the GPS sensor traveled. 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. If the real sensor sample size was 1000 I will now have 20 samples (by extracting each 50 sample).

现在,我希望能够将样本列表通过一个函数来计算距离.到目前为止,我已经能够使用geopy包,但是当我获取较大的gps样本集时,确实会收到太多请求"错误,更不用说处理请求了,我将得到额外的处理时间,这不是我所需要的想要.

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.

是否有更好的方法来计算包含纬度和经度坐标的列表元素的累积距离?

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)]

我找到了仅使用2个坐标(lat1,lng1和lat2和lng2)来计算距离的许多不同数学方法的方法,但是都不支持坐标列表.

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.

这是我当前使用geopy的代码:

Here's my current code using 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是一个列表元素,包含与行程有关的键值对信息字典(持续时间,距离,起点和终点坐标等),行程内的positions对象是元组坐标的列表,如上图所示

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 = [{data_1}, {data_2}, ..., {data_n}]

推荐答案

这是我最终使用的解决方案.如果您想查找自己的功能,则称为Haversine(距离)功能.

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.

我也改变了我的方法.我的输入(positions)是元组坐标的列表:

I changed my approach a little as well. My input (positions) is a list of tuple coordinates:

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

这篇关于计算坐标列表(纬度,经度)之间的地理距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆