在Python中计算经度和纬度之间的距离? [英] Calculating distance between latitude and longitude in python?

查看:141
本文介绍了在Python中计算经度和纬度之间的距离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助计算两个点之间的距离-在这种情况下,这两个点是经度和纬度.我有一个.txt文件,其中包含像这样的列中的经度和纬度:

I need help calculating the distance between two points-- in this case, the two points are longitude and latitude. I have a .txt file that contains longitude and latitude in columns like this:

-116.148000 32.585000
-116.154000 32.587000
-116.159000 32.584000

这些列没有标题.我还有更多的纬度和经度.

The columns do not have headers. I have many more latitudes and longitudes.

到目前为止,我想出了以下代码:

So far, i have come up with this code:

from math import sin, cos, sqrt, atan2, radians
R = 6370
lat1 = radians()  #insert value
lon1 = radians()
lat2 = radians()
lon2 = radians()

dlon = lon2 - lon1
dlat = lat2- lat1

a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
c = 2 * atan2(sqrt(a), sqrt(1-a))
distance = R * c
print (distance)

我在堆栈溢出中看到的许多用于计算经度和纬度之间距离的答案/代码已将经度和纬度指定为特定值.

Many of the answers/code i've seen on stack overflow for calculating the distance between longitude and latitude have had longitude and latitude assigned as specific values.

我希望经度和纬度等于它们所在列中的值,并且等式可以遍历所有经度和纬度并计算距离.

I would like the longitude and latitude to equal the values in the columns they are in and for the equation to go through all of the longitudes and latitudes and calculate the distance.

我无法提出一些解决方案.任何帮助将不胜感激

I have not been able to come up with something to do this. Any help would be appreciated

推荐答案

基于这个问题,听起来您想计算所有成对的点之间的距离.Scipy内置了执行此操作的功能.

Based on the question it sounds like you would like to calculate the distance between all pairs of points. Scipy has built in functionality to do this.

我的建议是首先编写一个计算距离的函数.或使用另一个答案中提到的存在于geopy中的人.

My suggestion is to first write a function that calcuates distance. Or use an exisitng one like the one in geopy mentioned in another answer.

def get_distance(point1, point2):
    R = 6370
    lat1 = radians(point1[0])  #insert value
    lon1 = radians(point1[1])
    lat2 = radians(point2[0])
    lon2 = radians(point2[1])

    dlon = lon2 - lon1
    dlat = lat2- lat1

    a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
    c = 2 * atan2(sqrt(a), sqrt(1-a))
    distance = R * c
    return distance

然后,您可以将此函数传递到 scipy.spatial.distance.cdist

Then you can pass this function into scipy.spatial.distance.cdist

all_points = df[[latitude_column, longitude_column]].values

dm = scipy.spatial.distance.cdist(all_points,
                                  all_points,
                                  get_distance)

如果希望将索引添加到每个点,则可以将距离矩阵转换为数据框作为奖励:

As a bonus you can convert the distance matrix to a data frame if you wish to add the index to each point:

pd.DataFrame(dm, index=df.index, columns=df.index)

注意::我意识到我正在假设您可能正在错误地使用熊猫

NOTE: I realized I am assuming, possibly incorrectly that you are using pandas

这篇关于在Python中计算经度和纬度之间的距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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