Python教程-如何使用GEOPY计算两点之间的距离
geopy是一个能够计算地理距离的Python库。在本教程中,我们将讨论用户如何使用不同方法计算地球上两个地点之间的距离。
首先,用户需要使用以下命令安装geopy:
pip install geopy
安装成功后,我们就可以开始使用geopy库了。
计算两点之间的距离
以下是用于计算两点之间距离的重要方法。
方法一:使用大地测量距离
大地测量距离是地球表面上两点之间最短路径的长度。在以下示例中,我们将展示用户如何根据纬度和经度数据计算大地测量距离。
示例:
# 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
方法二:使用大圆距离
大圆距离是球体上两点之间的最短路径。在这种情况下,我们假设地球是一个完美的球体。以下示例展示了如何使用两个点的经度和纬度数据来计算大圆距离。
示例:
# 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
方法三:使用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库计算地球表面上两点之间距离的不同方法,并提供了每种方法的示例。