Vectorised great-circle distance in metres between two coordinate arrays.
See Also
GeoKDTree : Projected nearest-neighbour queries.
find_nearest_stations : Nearest-station search.
Source code in src/gbfs_toolkit/spatial/geometry.py
| def haversine_m(lat1: Any, lon1: Any, lat2: Any, lon2: Any) -> np.ndarray:
"""Vectorised great-circle distance in metres between two coordinate arrays.
See Also
--------
[`GeoKDTree`][gbfs_toolkit.GeoKDTree] : Projected nearest-neighbour queries.
[`find_nearest_stations`][gbfs_toolkit.find_nearest_stations] : Nearest-station search.
"""
lat1, lon1, lat2, lon2 = (
np.radians(np.asarray(a, dtype="float64")) for a in (lat1, lon1, lat2, lon2)
)
dlat, dlon = lat2 - lat1, lon2 - lon1
a = np.sin(dlat / 2) ** 2 + np.cos(lat1) * np.cos(lat2) * np.sin(dlon / 2) ** 2
return EARTH_RADIUS_M * 2 * np.arctan2(np.sqrt(a), np.sqrt(1 - a))
|