如何使用Python中的Geopy计算两点之间的距离

792 阅读4分钟

如何使用Python中的Geopy计算两点之间的距离

Geopy是一个Python库,它简化了两点之间地理距离的计算。

它使开发者更容易使用第三方地理编码器以及其他数据源检索各种地点的坐标。

在这篇文章中,我们将研究计算任何两点之间距离的其他方法。

我们将解释如何使用geopy实现这一过程。这是计算距离的主要Python库。

在安装geopy 之前,有一系列的步骤。

首先,打开你的终端,确保它是以管理员身份运行的。通过运行cd.. 命令,确保你是从根目录下操作的。

现在你可以运行下面的命令,在你的电脑上安装geopy

pip install geopy

我们可以用Geopy中的以下方法计算或找到两个位置之间的距离。

  • 大地测量法。
  • 大圆距离。
  • Herversine公式。

大地测量法

大地测量法是用来确定地球上任何两点之间的最短路径。

然而,它与任何两个给定地点之间的最短曲线不完全相同,尽管它们很相似。

我们将在下面的例子中说明如何使用经纬度数据来计算测地距离。

例1:

# Import the geodesic module from geopy library 
from geopy.distance import geodesic as GD
 # For the specified locations, load their latitude and longitude data.
Abuja =(9.072264 , 7.491302)
Dakar =(14.716677 , -17.467686)
#Finally, print the distance between the two sites in kilometers.
print("The distance between Abuja and Dakar is: ", GD(Abuja,Dakar).km)

输出。

The distance between Abuja and Dakar is: 2787.8012928541466

例2:

# Geopy distance module is first imported for computation
from geopy.distance import geodesic as GD
# Next, input the latitude and longitude data for Nairobi and Cairo.  
Nairobi=(36.817223,-1.286389 )
Cairo=( 31.233334,30.033333, )
# Finally, print the distance between the two locations in kilometers. 
print("The distance between Nairobi and Cairo is :",GD(Nairobi,Cairo).km)

输出。

The distance between Nairobi and Cairo is: 2944.261368793268

大圆距离公式

大圆被认为是球体或地球表面上任何两个地方或点之间的最短路径。在这个例子中,我们假设地球是一个完美的球体。

下面的例子显示了如何使用两个地点的经度和纬度数据计算大圆距离。

大圆导航的一些问题包括端点和中间航点的方位角的计算。

大圆是由球体上任何两个不直接相对的点形成的。

大圆被这两点划分为两个弧。任何两个地点之间较短的弧线等于大圆的距离。

例3:

# First, import the geopy library's great circle module.
from geopy.distance import great_circle as GRC
# Abuja and Dakar latitude and longitude data.
Abuja=(9.072264 , 7.491302)
Dakar=(14.716677 , -17.467686)
# Finally print the distance between the two points in km
print("The distance between Abuja and Dakar is:", GRC(Abuja,Dakar).km) 

输出。

The distance between Abuja and Dakar is: 2785.186971064666

计算距离的Haversine公式

Haversine公式利用经纬度计算球体上任意两个地点之间的大圆距离。

Haversine方法提供了一种精确的方法来确定任何指定经度和纬度之间的距离。

它也是对球面余弦律的重新调整。然而,它对微小的角度和距离更有用。

用户必须有两个点的坐标(X和Y)才能利用这个方法。

他们必须使用180/π 公式将纬度和经度转换为弧度。

公式

为了将经度和纬度转换为弧度,我们使用以下公式。

以弧度为单位,纬度的值是。

Latitude (LaA) = LaA / (180/π ) or Latitude (LaA) = LaA / 57.29577.

经度的值为:。

Longitude (LoA) = LoA / (180/π ) or Longitude (LoA) = LoA / 57.29577.

我们使用下面的公式来计算以英里为单位的距离。

距离(D)=3963.0 * arccos[(sin(LaA) * sin(LaB)) + cos(LaA) * cos(LaB) * cos(LoB - LoA)]

要计算以公里为单位的距离。

距离(D) = 3963.0 * arccos[(sin(LaA) * sin(LaB)) + cos(LaA) * cos(LaB) * cos(LoB - LoA)]

例4:

from math import radians, cos, sin, asin, sqrt
# Implement the formula below
def distance_d(LaA, LaB, LoA, LoB):
# The function "radians" is found in the math module, It's also used to convert radians to degrees.  
LoA = radians(LoA)  
LoB = radians(LoB)  
LaA= radians(LaA)  
LaB = radians(LaB) 
# The "Haversine formula" is used.
D_Lo = LoB - LoA 
D_La = LaB - LaA 
P = sin(D_La / 2)**2 + cos(LaA) * cos(LaB) * sin(D_Lo / 2)**2  
   
Q = 2 * asin(sqrt(P))   
    # The earth's radius in kilometers.
R_km = 6371  
# Then we'll compute the outcome.
return(Q * R km).
 
LaA = 9.072264
LaB = 14.716677
LoA = 7.491302
LoB = -17.467686
print ("The distance between Abuja and Dakar is: ", distance_d(LaA, LaB, LoA, LoB), "K.M")  

输出。

The distance between Abuja and Dakar is:  2785.183036572855 K.M

结论

在本教程中,我们讨论了如何使用geopy包来确定地球表面上两点之间的距离。

因此,你可以利用这些知识来制作其他高质量的应用程序。