Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quick fix #23

Merged
merged 2 commits into from
May 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ haversine(lyon, paris, unit=Units.NAUTICAL_MILES)
>> 211.78037755311516 # in nautical miles
```

The `haversine.Units` enum contains all supported units:
The `haversine.Unit` enum contains all supported units:

```python
import haversine

print(tuple(haversine.Units))
print(tuple(haversine.Unit))
```

outputs

```text
(<Units.FEET: 'ft'>, <Units.INCHES: 'in'>, <Units.KILOMETERS: 'km'>,
<Units.METERS: 'm'>, <Units.MILES: 'mi'>, <Units.NAUTICAL_MILES: 'nmi'>)
(<Unit.FEET: 'ft'>, <Unit.INCHES: 'in'>, <Unit.KILOMETERS: 'km'>,
<Unit.METERS: 'm'>, <Unit.MILES: 'mi'>, <Unit.NAUTICAL_MILES: 'nmi'>)
```

## Installation
Expand Down
2 changes: 1 addition & 1 deletion haversine/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .haversine import Units, haversine
from .haversine import Unit, haversine
34 changes: 17 additions & 17 deletions haversine/haversine.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
_AVG_EARTH_RADIUS_KM = 6371.0088


class Units(Enum):
class Unit(Enum):
"""
Enumeration of supported units.
The full list can be checked by iterating over the class; e.g.
the expression `tuple(Units)`.
the expression `tuple(Unit)`.
"""

KILOMETERS = 'km'
Expand All @@ -21,41 +21,41 @@ class Units(Enum):
INCHES = 'in'


# Units values taken from http://www.unitconversion.org/unit_converter/length.html
_CONVERSIONS = {Units.KILOMETERS.value: 1.0,
Units.METERS.value: 1000.0,
Units.MILES.value: 0.621371192,
Units.NAUTICAL_MILES.value: 0.539956803,
Units.FEET.value: 3280.839895013,
Units.INCHES.value: 39370.078740158}
# Unit values taken from http://www.unitconversion.org/unit_converter/length.html
_CONVERSIONS = {Unit.KILOMETERS: 1.0,
Unit.METERS: 1000.0,
Unit.MILES: 0.621371192,
Unit.NAUTICAL_MILES: 0.539956803,
Unit.FEET: 3280.839895013,
Unit.INCHES: 39370.078740158}


def haversine(point1, point2, unit=Units.KILOMETERS):
def haversine(point1, point2, unit=Unit.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 haversine.Units, or, equivalently, a string containing the
:param unit: a member of haversine.Unit, or, equivalently, a string containing the
initials of its corresponding unit of measurement (i.e. miles = mi)
default 'km' (kilometers).

Example: ``haversine((45.7597, 4.8422), (48.8567, 2.3508), unit=Units.METERS)``
Example: ``haversine((45.7597, 4.8422), (48.8567, 2.3508), unit=Unit.METERS)``

Precondition: ``unit`` is a supported unit (supported units are listed in the `Units` enum)
Precondition: ``unit`` is a supported unit (supported units are listed in the `Unit` 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 ``haversine.Units``
(e.g. ``haversine.Units.INCHES``), or, equivalently, to a string containing the
corresponding abbreviation (e.g. 'in'). All available units can be found in the ``Units`` enum.
setting the unit parameter to a member of ``haversine.Unit``
(e.g. ``haversine.Unit.INCHES``), or, equivalently, to a string containing the
corresponding abbreviation (e.g. 'in'). All available units can be found in the ``Unit`` enum.
"""

# get earth radius in required units
unit = unit.value if isinstance(unit, Units) else unit
unit = Unit(unit)
avg_earth_radius = _AVG_EARTH_RADIUS_KM * _CONVERSIONS[unit]

# unpack latitude/longitude
Expand Down
28 changes: 14 additions & 14 deletions tests/test_haversine.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from haversine import haversine, Units
from haversine import haversine, Unit

LYON = (45.7597, 4.8422)
PARIS = (48.8567, 2.3508)

EXPECTED = {Units.KILOMETERS: 392.2172595594006,
Units.METERS: 392217.2595594006,
Units.MILES: 243.71250609539814,
Units.NAUTICAL_MILES: 211.78037755311516,
Units.FEET: 1286802.0326751503,
Units.INCHES: 15441624.392102592}
EXPECTED = {Unit.KILOMETERS: 392.2172595594006,
Unit.METERS: 392217.2595594006,
Unit.MILES: 243.71250609539814,
Unit.NAUTICAL_MILES: 211.78037755311516,
Unit.FEET: 1286802.0326751503,
Unit.INCHES: 15441624.392102592}


def haversine_test_factory(unit):
Expand All @@ -21,15 +21,15 @@ def test():
return test


test_kilometers = haversine_test_factory(Units.KILOMETERS)
test_meters = haversine_test_factory(Units.METERS)
test_miles = haversine_test_factory(Units.MILES)
test_nautical_miles = haversine_test_factory(Units.NAUTICAL_MILES)
test_feet = haversine_test_factory(Units.FEET)
test_inches = haversine_test_factory(Units.INCHES)
test_kilometers = haversine_test_factory(Unit.KILOMETERS)
test_meters = haversine_test_factory(Unit.METERS)
test_miles = haversine_test_factory(Unit.MILES)
test_nautical_miles = haversine_test_factory(Unit.NAUTICAL_MILES)
test_feet = haversine_test_factory(Unit.FEET)
test_inches = haversine_test_factory(Unit.INCHES)


def test_units_enum():
from haversine.haversine import _CONVERSIONS
assert all(unit.value in _CONVERSIONS for unit in Units)
assert all(unit in _CONVERSIONS for unit in Unit)