Skip to content

Commit

Permalink
add normalize_output to inverse_haversine_vector
Browse files Browse the repository at this point in the history
  • Loading branch information
jdeniau committed Nov 28, 2024
1 parent 71b283c commit 02ed076
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
11 changes: 9 additions & 2 deletions haversine/haversine.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,13 +277,15 @@ def inverse_haversine(point, distance, direction: Union[Direction, float], unit=
lat, lng = point
r = get_avg_earth_radius(unit)
outLat, outLng = _inverse_haversine_kernel(lat, lng, direction, distance / r)
print(outLat, outLng)

if normalize_output:
return _normalize(outLat, outLng)
else:
return (outLat, outLng)


def inverse_haversine_vector(array, distance, direction, unit=Unit.KILOMETERS):
def inverse_haversine_vector(array, distance, direction, unit=Unit.KILOMETERS, normalize_output=False): # -> Tuple["numpy.ndarray", "numpy.ndarray"]:
if not has_numpy:
raise RuntimeError('Error, unable to import Numpy, '
'consider using inverse_haversine instead of inverse_haversine_vector.')
Expand All @@ -303,4 +305,9 @@ def inverse_haversine_vector(array, distance, direction, unit=Unit.KILOMETERS):
lat, lng = array[:, 0], array[:, 1]

r = get_avg_earth_radius(unit)
return _inverse_haversine_kernel_vector(lat, lng, direction, distance/r)
outLatArray, outLngArray = _inverse_haversine_kernel_vector(lat, lng, direction, distance/r)

if normalize_output:
return _normalize_vector(outLatArray, outLngArray)

return (outLatArray, outLngArray)
31 changes: 31 additions & 0 deletions tests/test_inverse_haversine_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,34 @@
def test_inverse_kilometers(point, dir, dist, result):
assert isclose(inverse_haversine_vector([point], [dist], [dir]),
([result[0]], [result[1]]), rtol=1e-5).all()

def test_inverse_normalization():
twoDegreesAtEquator = 222390.1
point = (0.0, -179.0)

# non-breaking behavior without normalization
result = inverse_haversine_vector(
[point, LONDON], [twoDegreesAtEquator, 50], [Direction.WEST, Direction.WEST], Unit.METERS)

# assert result is a Tuple
assert isinstance(result, tuple)

# assert result is a Tuple of array
latArray, lngArray = result

assert isclose(lngArray[0], -181.0, rtol=1e-5,)

# behavior with normalization
result = inverse_haversine_vector(
[point, LONDON], [twoDegreesAtEquator, 50000], direction=[Direction.WEST, Direction.WEST],
unit=Unit.METERS, normalize_output=True,)


# assert result is a Tuple
assert isinstance(result, tuple)

# assert result is a Tuple of array
latArray, lngArray = result

assert isclose(lngArray[0], 179.0, rtol=1e-5,).all()
assert isclose(lngArray[1], -0.840556, rtol=1e-5,).all()

0 comments on commit 02ed076

Please sign in to comment.