Repository URL to install this package:
|
Version:
2.0.5 ▾
|
from enum import Enum
import haversine as hs
class DistanceUnit(Enum):
"""
Enumeration of supported units.
"""
KILOMETERS = "km"
METERS = "m"
MILES = "mi"
def calculate_geo_distance(point1, point2, unit=DistanceUnit.KILOMETERS):
"""Calculate the great-circle distance between two points on the Earth surface.
Takes two 2-tuples, containing the latitude and longitude of each point in decimal degrees,
and, optionally, a unit of length.
:param point1: first point; tuple of (latitude, longitude) in decimal degrees
:param point2: second point; tuple of (latitude, longitude) in decimal degrees
:param unit: a member of geo_utils.DistanceUnit, or, equivalently, a string containing the
initials of its corresponding unit of measurement (i.e. miles = mi)
default 'km' (kilometers).
Example: ``calculate_geo_distance((45.7597, 4.8422), (48.8567, 2.3508), unit=Unit.METERS)``
Precondition: ``unit`` is a supported unit (supported units are listed in the `DistanceUnit` enum)
:return: the distance between the two points in the requested unit, as a float.
The default returned unit is kilometers. The default unit can be changed by
setting the unit parameter to a member of ``geo_utils.DistanceUnit``
(e.g. ``geo_utils.DistanceUnit.MILES``), or, equivalently, to a string containing the
corresponding abbreviation (e.g. 'in'). All available units can be found in the ``DistanceUnit`` enum.
"""
return hs.haversine(point1, point2, unit.value)