pythonrspatialgeopygeosphere

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 :

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

  • 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