Repository: mie-lab/trackintel (language: python)

Issue:
BUG: `point_haversine_dist` is not precise enough
I am currently rewriting #592 and there a test on the haversine distance keeps failing because our implementation of the haversine function is not precise enough. If we compare it to the sklearn implementation: (distance Buenos Aires to Paris)

````python
import trackintel as ti
from sklearn.metrics.pairwise import haversine_distances
from math import radians
bsas = [-34.83333, -58.5166646]
paris = [49.0083899664, 2.53844117956]
bsas_in_radians = [radians(_) for _ in bsas]
paris_in_radians = [radians(_) for _ in paris]
result = (haversine_distances([bsas_in_radians, paris_in_radians]) * 6371000)[0, 1]
test = ti.geogr.point_haversine_dist(*bsas, *paris)[0]
print(result)
print(test)
print(test/result)
````
Result: 
````
11099540.35581966
9891586.042330354
0.8911707805219199
````
For smaller distances e.g. in a city that is not really observable but over bigger distances it is quite dramatic. 
Here our result is 11% smaller than the sklearn implementation.



Discussion / hints:
It's great that you find it out! I have always used this function, and it would be great to ensure its precision...

Fix patch:
diff --git a/trackintel/geogr/distances.py b/trackintel/geogr/distances.py
index ab4beb60..a8c0fe7d 100644
--- a/trackintel/geogr/distances.py
+++ b/trackintel/geogr/distances.py
@@ -1,18 +1,15 @@
 import itertools
 import math
-import multiprocessing
 import warnings
-from functools import partial
 from math import cos, pi
 
 import numpy as np
 import pandas as pd
 import shapely
 import similaritymeasures
-from scipy.spatial.distance import cdist
 from sklearn.metrics import pairwise_distances
 
-from trackintel import Positionfixes, Triplegs
+from trackintel import Triplegs
 
 
 def point_haversine_dist(lon_1, lat_1, lon_2, lat_2, r=6371000, float_flag=False):


Test patch:
diff --git a/tests/geogr/test_distances.py b/tests/geogr/test_distances.py
index 2cf3f779..0d4e7d2a 100644
--- a/tests/geogr/test_distances.py
+++ b/tests/geogr/test_distances.py
@@ -231,10 +231,11 @@ def test_compare_haversine_to_scikit_xy_with_Y(self, two_pfs):
         res01 = calculate_distance_matrix(X=pfs0, Y=pfs1, dist_metric="haversine")
         rad0 = np.radians(shapely.get_coordinates(pfs0.geometry))
         rad1 = np.radians(shapely.get_coordinates(pfs1.geometry))
+        # pairwise distance takes lat, lon input. Points are lon, lat
+        rad0 = np.roll(rad0, 1, axis=1)
+        rad1 = np.roll(rad1, 1, axis=1)
         sol01 = pairwise_distances(rad0, rad1, metric="haversine") * 6371000
-        # TODO: increase precision of haversine dist such we can lower the tolerance
-        # see issue #593 for more information
-        assert np.allclose(res01, sol01, rtol=1e-2)
+        assert np.allclose(res01, sol01)
 
     def test_known_euclidean_distance(self, two_pfs):
         """Test the result comparing to known euclidean distances"""