Python 如何使用GEOPY计算两点之间的距离
geopy是一个帮助计算地理距离的Python库。在本教程中,我们将讨论用户如何计算地球上两个地点之间距离的不同方法。
首先,用户需要使用以下命令安装geopy:
pip install geopy
安装成功后,我们准备使用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
方法3:使用Haversine公式
正位角距离用于计算地球表面上两个纬度和经度点之间的最短距离。
使用该方法,用户需要知道两个点的坐标 (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)]
因此,用户可以使用Haversine公式计算地球上两个给定点之间的最短距离。
示例如下:
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
结论
在本教程中,我们讨论了使用geopy库计算地球表面上两点之间距离的各种方法。我们已经展示了每种方法的示例。