Search code examples
matplotlib-basemapcartopypyproj

Why do the inverse function from both cartopy and basemap have different results to calculate distance?


I want to calculate the distance between two points on surface of earth in meteres

I have tried with both basemap and cartopy but both result in different numbers.

Basemap:

import mpl_toolkits.basemap.pyproj as pyproj

k = pyproj.Geod(ellps="WGS84")
distance = k.inv(c0[1], c0[0], c1[1], c1[0])[-1]/1000.

Cartopy:

import cartopy.geodesic as gd

k = gd.Geodesic() // defaults to WGS84
distance = k.inverse(c0, c1).base[0,0]/1000

where both coord0 and coord1 are numpy arrays of size 2 having lat and lon of a coordinate.

c0 = numpy.array([77.343750, 22.593726])
c1 = numpy.array([86.945801, 23.684774])

Cartopy Output: 990.6094719605074

Basemap Output: 1072.3456344712142


Solution

  • With Basemap, you must use proper order of (long, lat):

    distance = k.inv(c0[0], c0[1], c1[0], c1[1])[-1]/1000.
    

    and the result will agree with Cartopy's, which is the correct result:

    990.6094719605074