使用Haversine软件包比较经度和纬度的csv列表的所有距离可能性

时间:2018-07-24 00:42:39

标签: python python-3.x distance haversine

我有一个lon和lat的csv文件列表,我想计算所有距离,该文件具有类似(-18.4392969, -40.2688762) (-8.3905896, -37.4957717) (-19.952862, -33.173232)的参数

编辑:我想编写一个代码,给我这样的东西:A点到B点和C点之间的距离 和B到A和C,C到A和B,并保存到列表距离。

1 个答案:

答案 0 :(得分:0)

这是一个python函数,用于根据给定的(纬度,经度)元组来计算地球上两个位置之间的大圆距离:

import math

def distance(p1, p2):
    R = 6371 # mean radius of Earth in km

    cphi_1 = math.cos(math.radians(p1[0]))
    cphi_2 = math.cos(math.radians(p2[0]))

    d_phi = math.radians(p2[0] - p1[0])
    d_lam = math.radians(p2[1] - p1[1])

    a =  math.sin(d_phi / 2) ** 2
    a += math.sin(d_lam / 2) ** 2 * cphi_1 * cphi_2
    return 2 * R * math.atan2(abs(a) ** 0.5, abs(1 - a) ** 0.5)


if __name__ == "__main__":
    print(distance((40.2342, -17.9834), (13.3298, -53.1698)))

我已针对this online calculator进行了测试,但是您可能希望在自己的代码中使用上面的代码之前进行自己的测试。

编辑以计算所有唯一的成对距离而无重复的距离,请使用

from itertools import combinations
distances = list(map(distance, *zip(*combinations(lat_lon_list, 2))))