如何用 GEOPY 计算两点之间的距离?

原文:https://www.javatpoint.com/how-to-calculate-distance-between-two-points-using-geopy

geopy是一个帮助计算地理距离的 Python 库。在本教程中,我们将讨论用户如何计算地球上两个地方之间距离的不同方法。

首先,用户必须使用以下命令安装geopy:


pip install geopy

安装成功后,我们准备与地质图书馆合作。

计算两点之间的距离

下面是用来计算两点之间距离的重要方法。

方法 1:使用测地线距离

测地线距离是地球任何表面上两点之间最短路径的长度。在下面的示例中,我们将展示用户如何根据纬度和经度数据计算测地线距离。

示例:


# First, import the geodesic module from the geopy library
from geopy.distance import geodesic as GD

# Then, load the latitude and longitude data for New York & Texas
New_York = (40.7128, 74.0060)
Texas = (31.9686, 99.9018)

# At last, print the distance between two points calculated in kilo-metre
print ("The distance between New York and Texas is: ", GD(New_York, Texas).km)

输出:

The distance between New York and Texas is:  2507.14797665193

方法 2:使用大圆距离

大圆距离是球面上两点之间的最短路径。在这种情况下,我们将假设地球是完美的球体。以下示例显示了用户如何使用两点的经度和纬度数据来计算大圆距离。

示例:


# First, import the great_circle module from the geopy library
from geopy.distance import great_circle as GC

# Then, load the latitude and longitude data for New York & Texas
New_York = (40.7128, 74.0060)
Texas = (31.9686, 99.9018)

# At last, print the distance between two points calculated in kilo-metre
print ("The distance between New York and Texas is: ", GC(New_York, Texas).km)

输出:

The distance between New York and Texas is:  2503.045970189156

方法三:利用哈弗斯公式

正交距离用于计算地球表面两个纬度和经度点之间的最短距离。

使用这种方法,用户需要有两个点的坐标 (P 和 Q)。

首先,他们必须将纬度和经度点的值从十进制度转换为弧度,然后将纬度和经度的值除以(180/π)。用户应该使用“π = 22/7”的值。那么,(180/π)的值将是“57.29577”。如果用户想要计算以英里为单位的距离,他们可以使用地球半径的值,即“3,963”。如果用户想以千米为单位计算距离,他们可以使用值“6,378.80”。

公式:


How to calculate the value of latitude in radians:
The value of Latitude in Radian: Latitude (La1) = La1 / (180/?)
OR
The value of Latitude in Radian: Latitude (La1) = La1 / 57.29577
How to calculate the value of longitude in radians:
The value of Longitude in Radian: Longitude (Lo1) = Lo1 / (180/?)
OR
The value of Longitude in Radian: Longitude (Lo1) = Lo1 / 57.29577

用户需要用经纬度表示 P 点和 Q 点的坐标,然后用上面的公式转换成弧度。

现在,使用以下公式计算两点之间的距离。

公式:

英里:


Distance (D) = 3963.0 * arccos[(sin(La1) * sin(La2)) + cos(La1) * cos(La2) * cos(Lo2 - Lo1)]

公里:


Distance (D) = 3963.0 * arccos[(sin(La1) * sin(La2)) + cos(La1) * cos(La2) * cos(Lo2 - Lo1)]

因此,用户可以使用哈弗辛公式计算地球上两个给定点之间的最短距离。

示例:


from math import radians, cos, sin, asin, sqrt
# For calculating the distance in Kilometres
def distance_1(La1, La2, Lo1, Lo2):

# The math module contains the function name "radians" which is used for converting the degrees value into radians.
Lo1 = radians(Lo1)
Lo2 = radians(Lo2)
La1 = radians(La1)
La2 = radians(La2)

# Using the "Haversine formula"
D_Lo = Lo2 - Lo1
D_La = La2 - La1
P = sin(D_La / 2)**2 + cos(La1) * cos(La2) * sin(D_Lo / 2)**2

Q = 2 * asin(sqrt(P))

# The radius of earth in kilometres.
R_km = 6371

# Then, we will calculate the result
return(Q * R_km)

# driver code
La1 = 40.7128
La2 = 31.9686
Lo1 = -74.0060
Lo2 = -99.9018
print ("The distance between New York and Texas is: ", distance_1(La1, La2, Lo1, Lo2), "K.M")
# For calculating the distance in Miles
def distance_2(La1, La2, Lo1, Lo2):

# The math module contains the function name "radians" which is used for converting the degrees value into radians.
Lo1 = radians(Lo1)
Lo2 = radians(Lo2)
La1 = radians(La1)
La2 = radians(La2)

# Using the "Haversine formula"
D_Lo = Lo2 - Lo1
D_La = La2 - La1
P = sin(D_La / 2)**2 + cos(La1) * cos(La2) * sin(D_Lo / 2)**2

Q = 2 * asin(sqrt(P))
# The radius of earth in Miles.
R_Mi = 3963

# Then, we will calculate the result
return(Q * R_Mi)
print ("The distance between New York and Texas is: ", distance_2(La1, La2, Lo1, Lo2), "Miles")

输出:

The distance between New York and Texas is:  2503.04243426357 K.M
The distance between New York and Texas is: 1556.985899699659 Miles

结论

在本教程中,我们已经讨论了使用地质库计算地球表面两点之间距离的各种方法。我们已经展示了每种方法的示例。