ST_Distance_Sphere() 在 Python 中?
ST_Distance_Sphere() in Python?
我正在做一个 Python 项目,我有两个 lat/long 对,我想计算它们之间的距离。在其他项目中,我使用 ST_Distance_Sphere(a.loc_point, b.loc_point) 在 Postgres 中计算了距离,但我想避免将所有数据加载到 Postgres 中,以便我可以计算距离差异。我已经搜索过,但没能找到我想要的,这纯粹是 Python 的实现,这样我就不必将数据加载到 Postgres 中。
我知道还有其他距离计算将地球视为一个完美的球体,但由于精度差,这些计算还不够好,这就是为什么我想使用 PostGIS ST_Distance_Sphere()函数(或等价物)。
这里有几个示例 Lat/Longs,我想计算它们的距离:
Lat, Long 1: (49.8755, 6.07594)
Lat, Long 2: (49.87257, 6.0784)
我无法想象我是第一个问这个问题的人,但是有没有人知道使用 ST_Distance_Sphere() 来纯粹从 [=29] 中进行 lat/long 距离计算的方法=] 脚本?
这是一个基本函数,用于计算半径 = 地球半径的完美球体上两个坐标之间的距离
from math import pi , acos , sin , cos
def calcd(y1,x1, y2,x2):
#
y1 = float(y1)
x1 = float(x1)
y2 = float(y2)
x2 = float(x2)
#
R = 3958.76 # miles
#
y1 *= pi/180.0
x1 *= pi/180.0
y2 *= pi/180.0
x2 *= pi/180.0
#
# approximate great circle distance with law of cosines
#
x = sin(y1)*sin(y2) + cos(y1)*cos(y2)*cos(x2-x1)
if x > 1:
x = 1
return acos( x ) * R
希望对您有所帮助!
看到这个How can I quickly estimate the distance between two (latitude, longitude) points?
from math import radians, cos, sin, asin, sqrt
def haversine(lon1, lat1, lon2, lat2):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
"""
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
km = 6367 * c
return km
亚伦 D
您可以通过添加 miles = km * 0.621371
将其修改为 return 英里
我会推荐 geopy 包 - 请参阅文档中的 Measuring Distance 部分...
对于您的具体情况:
from geopy.distance import great_circle
p1 = (49.8755, 6.07594)
p2 = (49.87257, 6.0784)
print(great_circle(p1, p2).kilometers)
除了这里提供的答案之外,我还找到了另一种方法。使用 python haversine 模块。
from haversine import haversine as h
# Return results in meters (*1000)
print '{0:30}{1:12}'.format("haversine module:", h(a, b)*1000)
我测试了所有三个答案加上 haversine 模块与我在 Postgres 中使用 ST_Distance_Sphere(a, b) 得到的结果。所有答案都非常好(谢谢),但 Sishaar Rao 的所有数学答案(计算)是最接近的。以下是结果:
# Short Distance Test
ST_Distance_Sphere(a, b): 370.43790478
vincenty: 370.778186438
great_circle: 370.541763803
calcd: 370.437386736
haversine function: 370.20481753
haversine module: 370.437394767
#Long Distance test:
ST_Distance_Sphere(a, b): 1011734.50495159
vincenty: 1013450.40832
great_circle: 1012018.16318
calcd: 1011733.11203
haversine function: 1011097.90053
haversine module: 1011733.11203
我正在做一个 Python 项目,我有两个 lat/long 对,我想计算它们之间的距离。在其他项目中,我使用 ST_Distance_Sphere(a.loc_point, b.loc_point) 在 Postgres 中计算了距离,但我想避免将所有数据加载到 Postgres 中,以便我可以计算距离差异。我已经搜索过,但没能找到我想要的,这纯粹是 Python 的实现,这样我就不必将数据加载到 Postgres 中。
我知道还有其他距离计算将地球视为一个完美的球体,但由于精度差,这些计算还不够好,这就是为什么我想使用 PostGIS ST_Distance_Sphere()函数(或等价物)。
这里有几个示例 Lat/Longs,我想计算它们的距离:
Lat, Long 1: (49.8755, 6.07594)
Lat, Long 2: (49.87257, 6.0784)
我无法想象我是第一个问这个问题的人,但是有没有人知道使用 ST_Distance_Sphere() 来纯粹从 [=29] 中进行 lat/long 距离计算的方法=] 脚本?
这是一个基本函数,用于计算半径 = 地球半径的完美球体上两个坐标之间的距离
from math import pi , acos , sin , cos
def calcd(y1,x1, y2,x2):
#
y1 = float(y1)
x1 = float(x1)
y2 = float(y2)
x2 = float(x2)
#
R = 3958.76 # miles
#
y1 *= pi/180.0
x1 *= pi/180.0
y2 *= pi/180.0
x2 *= pi/180.0
#
# approximate great circle distance with law of cosines
#
x = sin(y1)*sin(y2) + cos(y1)*cos(y2)*cos(x2-x1)
if x > 1:
x = 1
return acos( x ) * R
希望对您有所帮助!
看到这个How can I quickly estimate the distance between two (latitude, longitude) points?
from math import radians, cos, sin, asin, sqrt
def haversine(lon1, lat1, lon2, lat2):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
"""
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
km = 6367 * c
return km
亚伦 D
您可以通过添加 miles = km * 0.621371
我会推荐 geopy 包 - 请参阅文档中的 Measuring Distance 部分...
对于您的具体情况:
from geopy.distance import great_circle
p1 = (49.8755, 6.07594)
p2 = (49.87257, 6.0784)
print(great_circle(p1, p2).kilometers)
除了这里提供的答案之外,我还找到了另一种方法。使用 python haversine 模块。
from haversine import haversine as h
# Return results in meters (*1000)
print '{0:30}{1:12}'.format("haversine module:", h(a, b)*1000)
我测试了所有三个答案加上 haversine 模块与我在 Postgres 中使用 ST_Distance_Sphere(a, b) 得到的结果。所有答案都非常好(谢谢),但 Sishaar Rao 的所有数学答案(计算)是最接近的。以下是结果:
# Short Distance Test
ST_Distance_Sphere(a, b): 370.43790478
vincenty: 370.778186438
great_circle: 370.541763803
calcd: 370.437386736
haversine function: 370.20481753
haversine module: 370.437394767
#Long Distance test:
ST_Distance_Sphere(a, b): 1011734.50495159
vincenty: 1013450.40832
great_circle: 1012018.16318
calcd: 1011733.11203
haversine function: 1011097.90053
haversine module: 1011733.11203