Skip to content

Utilities

Geographic distance

API Description Example
haversine Compute elementwise great-circle distances between coordinate pairs.

Straight-line distance between coordinates

Use fastmob.utils.haversine to calculate straight-line surface distances between matching latitude/longitude pairs. The coordinates are in degrees. Choose metres or kilometres with unit, and a NumPy array, pandas Series, or Arrow array with output. Calculation uses the native parallel batch kernel, so input rows are not processed by a Python loop.

For columns from a pandas DataFrame:

from fastmob.utils import haversine

trips["distance_m"] = haversine(
    trips["latitude_source"], trips["longitude_source"],
    trips["latitude_target"], trips["longitude_target"],
    unit="m", output="numpy",
)

Results follow input row order, regardless of pandas index labels. Use unit="km" for kilometres and output="pandas" or output="arrow" for other containers. For routing along roads, see Network.

haversine(lat1, lng1, lat2, lng2, *, unit='m', output='numpy')

Compute elementwise great-circle distances between coordinate pairs.

Parameters:

Name Type Description Default
lat1 array - like

Origin latitudes in degrees, as a one-dimensional numeric array or Series.

required
lng1 array - like

Origin longitudes in degrees.

required
lat2 array - like

Destination latitudes in degrees.

required
lng2 array - like

Destination longitudes in degrees. All inputs must have the same length.

required
unit (m, km)

Output distance unit. Defaults to metres ("m").

"m"
output (numpy, pandas, arrow)

Output container. Defaults to a NumPy array. The pandas option returns a Series with a default index; results are positional, not index-aligned.

"numpy"

Returns:

Type Description
numpy.ndarray, pandas.Series, or pyarrow.Array

One distance per input row. NaN coordinates produce NaN distances.

Notes

Uses the spherical Haversine formula and a parallel native batch kernel. Results are straight-line surface distances, not road-network distances.

Examples:

>>> from fastmob.utils import haversine
>>> distances = haversine([48.8566], [2.3522], [51.5074], [-0.1278], unit="km")
>>> round(distances[0])
344