Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.
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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

Your comment says (latitude, longitude) but actually they go the other way around in geopy.distance.geodesic().

from geopy.distance import geodesic
lyon = (4.8422, 45.7597) # (latitude, longitude)
paris = (2.3508, 48.8567)
geodesic(lyon, paris, ellipsoid="WGS-84").km
# 440.76257985857796
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading