From 02ed0768ef2e3cec32493762b2c948c75bdc023f Mon Sep 17 00:00:00 2001 From: Julien Deniau Date: Thu, 28 Nov 2024 10:03:09 +0100 Subject: [PATCH] add normalize_output to inverse_haversine_vector --- haversine/haversine.py | 11 +++++++-- tests/test_inverse_haversine_vector.py | 31 ++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/haversine/haversine.py b/haversine/haversine.py index 8a7bd93..69b2a12 100755 --- a/haversine/haversine.py +++ b/haversine/haversine.py @@ -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.') @@ -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) \ No newline at end of file diff --git a/tests/test_inverse_haversine_vector.py b/tests/test_inverse_haversine_vector.py index 4e3dd90..3d7b3f2 100644 --- a/tests/test_inverse_haversine_vector.py +++ b/tests/test_inverse_haversine_vector.py @@ -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() \ No newline at end of file