英文:
Equivalent of R geosphere::distGeo in Python
问题
I am translating R code to Python. I can't find a function to match the output of R function geosphere::distGeo
in Python.
我正在将R代码翻译成Python。我找不到与R函数 geosphere::distGeo
相匹配的Python函数。
I have looked at a lot of answers here and it seems the Python equivalent is geopy.distance.geodesic
, but the results don't match. R code gives 440km and Python code gives 392km.
我在这里查看了很多答案,似乎Python的等价物是 geopy.distance.geodesic
,但结果不匹配。R代码给出了440公里,而Python代码给出了392公里。
I am looking for a Python function (or maybe just parameters the good parameters ?) to match the 440km given by R.
我正在寻找一个Python函数(或者可能只是正确的参数?)以匹配R给出的440公里。
I have tried this:
我尝试过这个:
R code
R代码
lyon = c(45.7597, 4.8422) # (latitude, longitude)
lyon = c(45.7597, 4.8422) # (纬度,经度)
paris = c(48.8567, 2.3508)
paris = c(48.8567, 2.3508)
geosphere::distGeo(lyon, paris) / 1000 # default is WGS84 and meters
geosphere::distGeo(lyon, paris) / 1000 # 默认为WGS84和米
440.7626 km
Python code
Python代码
from geopy.distance import geodesic
from geopy.distance import geodesic
lyon = (45.7597, 4.8422) # (latitude, longitude)
lyon = (45.7597, 4.8422) # (纬度,经度)
paris = (48.8567, 2.3508)
paris = (48.8567, 2.3508)
geodesic(lyon, paris, ellipsoid="WGS-84").km
geodesic(lyon, paris, ellipsoid="WGS-84").km
392.4315 km
英文:
I am translating R code to Python. I can't find a function to match the output of R function geosphere::distGeo
in Python.
I have looked at a lot of answers here and it seems the Python equivalent is geopy.distance.geodesic
, but the results don't match. R code gives 440km and Python code give 392km.
I am looking for a Python function (or maybe just parameters the good parameters ?) to match the 440km given by R.
I have tried this :
R code
lyon = c(45.7597, 4.8422) # (latitude, longitude)
paris = c(48.8567, 2.3508)
geosphere::distGeo(lyon, paris) / 1000 # default is WGS84 and meters
# 440.7626 km
Python code
from geopy.distance import geodesic
lyon = (45.7597, 4.8422) # (latitude, longitude)
paris = (48.8567, 2.3508)
geodesic(lyon, paris, ellipsoid="WGS-84").km
# 392.4315 km
答案1
得分: 2
R geosphere::distGeo()
函数
期望 c(lon, lat)
,而 geopy.distance.geodesic()
函数
期望 (lat, lon)
。
奇怪的是文档中没有明确说明这一点,但您可以在源代码中看到。
无论如何,只需交换顺序:
from geopy.distance import geodesic
lyon = (4.8422, 45.7597)
paris = (2.3508, 48.8567)
geodesic(lyon, paris, ellipsoid="WGS-84").km
# 440.76257985857796
英文:
R geosphere::distGeo()
expects c(lon, lat)
and geopy.distance.geodesic()
expects (lat, lon)
.
Strangely this is not made explicit in the docs but you can see it in the source.
In any case just switch the order:
from geopy.distance import geodesic
lyon = (4.8422, 45.7597)
paris = (2.3508, 48.8567)
geodesic(lyon, paris, ellipsoid="WGS-84").km
# 440.76257985857796
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论