lair.geo.haversine#

lair.geo.haversine(lat1, lon1, lat2, lon2, R=6371, deg=True)[source]#

Great-circle distance between two points on a sphere (haversine formula).

Formula from https://www.movable-type.co.uk/scripts/latlong.html. Vectorized: any argument can be an array (or pandas Series), broadcast together with numpy rules, so a fixed point against many points works.

Parameters:
  • lat1 (float or array-like) – First point.

  • lon1 (float or array-like) – First point.

  • lat2 (float or array-like) – Second point.

  • lon2 (float or array-like) – Second point.

  • R (float, default 6371) – Sphere radius. The result is in the same units (default: mean Earth radius in km, so the result is in km).

  • deg (bool, default True) – Whether the inputs are in degrees (False: radians).

Returns:

float or np.ndarray – Distance in the units of R.

See also

bearing

The direction from one point to the other.

Examples

From Salt Lake City to Provo and to Ogden, in km:

>>> import numpy as np
>>> from lair.geo import haversine
>>> round(float(haversine(40.7608, -111.8910, 40.2338, -111.6585)), 1)
61.8
>>> haversine(
...     40.7608,
...     -111.8910,
...     np.array([40.2338, 41.2230]),
...     np.array([-111.6585, -111.9738]),
... ).round(1)
array([61.8, 51.9])