I try to calculate distance between 2 locations but I have an error with this formula
acos(sin(latA)*sin(latB) + cos(latA)*cos(latB)*cos(abs(longB-longA)))
Here is my function to calculate distances :
def distanceGPS(latA, longA, latB, longB):
"""Retourne la distance en Kilomètres entre les 2 points A et B connus grâce à
leurs coordonnées GPS (en radians).
"""
latA, longA, latB, longB = deg2rad(latA, longA, latB, longB)
print(latA)
print(longA)
print(latB)
print(longB)
# Rayon de la terre en mètres (sphère IAG-GRS80)
RT = 6378.137
# angle en radians entre les 2 points
S = acos(sin(latA)*sin(latB) + cos(latA)*cos(latB)*cos(abs(longB-longA)))
# distance entre les 2 points, comptée sur un arc de grand cercle
return (S*RT)
Result of print :
lat 0.651706
dtype: float64
lon -0.079956
dtype: float64
0.6517058798628295
-0.07995634999527296
Here is my function to convert degrees to radian :
def deg2rad(latA, longA, latB, longB):
"""Convertit un angle "degrés décimaux" en "radians"
"""
return latA/180*pi, longA/180*pi, latB/180*pi, longB/180*pi
def test_prediction(corpus, lat_moy, long_moy, i):
"""
Cette fonction test si la localisation predite est a une distance inferieure a 100km de la localisation correct.
Si oui, alors on defini la prediction comme correct
Si non, alors on defini la prediction comme non correct
"""
for word in corpus.correct_localisation[i]:
loc = geolocator.geocode(word)
if loc is not None and distanceGPS(lat_moy, long_moy, loc.latitude, loc.longitude) <= 100:
corpus.at[i, "test_pred"] = "OK"
return
else:
corpus.at[i, "test_pred"] = "NOK"
return
My error is :
> --------------------------------------------------------------------------- ValueError Traceback (most recent call
> last) <ipython-input-18-75f41b231cbd> in <module>
> 11 df = coordonnees_article(corpus['article'][i])
> 12 lat_moy, long_moy = position_moyenne(outliers_coordonnees(df))
> ---> 13 test_prediction(corpus, lat_moy, long_moy, i)
> 14 print(i)
>
> <ipython-input-17-47270c034d33> in test_prediction(corpus, lat_moy,
> long_moy, i)
> 11 print(loc)
> 12 # try:
> ---> 13 if loc is not None and distanceGPS(lat_moy, long_moy, loc.latitude, loc.longitude) <= 100:
> 14 corpus.at[i, "test_pred"] = "OK"
> 15 return
>
> <ipython-input-16-129df29f0484> in distanceGPS(latA, longA, latB,
> longB)
> 13 RT = 6378.137
> 14 # angle en radians entre les 2 points
> ---> 15 S = acos(sin(latA)*sin(latB) + cos(latA)*cos(latB)*cos(abs(longB-longA)))
> 16
> 17 # distance entre les 2 points, comptée sur un arc de grand cercle
>
> ValueError: math domain error
Thank you for your help