I'm not sure if this is the right place but I am trying to develop a Django site that (among other things) shows public projects within 30 km of a given French commune. It mostly works fine but for some municipalities (for example Marseille) I have an error when trying to display the page, which after investigation seems to be related not to Django or PostgreSQL but to the actual coordinates I have in the database (those coordinates are from a public dataset provided by the French government)
As you can see below, I tried to isolate the problem : it occurs when trying to do the distance calculation for the projects located inside Marseille (line 172). If I change the latitude by a few meters (line 173), it is now able to correctly calculate that the distance is zero:
In [166]: paris.longitude
Out[166]: 2.347
In [167]: paris.latitude
Out[167]: 48.8589
In [168]: marseille.longitude = 5.3806
In [169]: marseille.latitude = 43.2803
In [170]: marseille.save()
In [171]: acos(cos(radians(marseille.latitude))
...: * cos(radians(paris.latitude))
...: * cos(radians(paris.longitude) - radians(marseille.longitude))
...: + sin(radians(marseille.latitude))
...: * sin(radians(paris.latitude))
...: ) * 6371
Out[171]: 662.847013493097
In [172]: acos(cos(radians(marseille.latitude))
...: * cos(radians(marseille.latitude))
...: * cos(radians(marseille.longitude) - radians(marseille.longitude))
...: + sin(radians(marseille.latitude))
...: * sin(radians(marseille.latitude))
...: ) * 6371
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[172], line 1
----> 1 acos(cos(radians(marseille.latitude))
2 * cos(radians(marseille.latitude))
3 * cos(radians(marseille.longitude) - radians(marseille.longitude))
4 + sin(radians(marseille.latitude))
5 * sin(radians(marseille.latitude))
6 ) * 6371
ValueError: math domain error
In [173]: marseille.latitude = 43.2804
In [174]: marseille.save()
In [175]: acos(cos(radians(marseille.latitude))
...: * cos(radians(marseille.latitude))
...: * cos(radians(marseille.longitude) - radians(marseille.longitude))
...: + sin(radians(marseille.latitude))
...: * sin(radians(marseille.latitude))
...: ) * 6371
Out[175]: 0.0
I'm not a mathematician, nor used to manipulate geographic information. What is the cause of the problem? Can the same error occur when calculating the distance between two different cities? Can I fix it without having to manually change the coordinates for an unknown number of the 36000 French municipalities?
Note : it is a simplification of the actual code used, which is here: https://github.com/MTES-MCT/aides-territoires/blob/7742d9f69aa60255e674542b9faa595b61b3ed20/src/projects/forms.py#L441