I'm trying to implement algorithm which will calculate distance between 2 points using great circle distance formula according to guide
My function:
from math import sin, cos, radians
def get_great_circle_distance(first_coords, second_coords, radius):
"""
:param first_coords: (first_client_latitude, first_client_longitude) <- degrees
:param second_coords: (second_client_latitude, second_client_longitude) <- degrees
:param radius: planet radius
:return: distance
"""
la_1, lo_1 = map(radians, first_coords)
la_2, lo_2 = map(radians, second_coords)
denominator = cos(
cos(la_1) * cos(la_2) * cos(lo_1 - lo_2) +
sin(la_1) * sin(la_2)
)
distance = radius / denominator
return distance
For example I call:
distance = get_great_circle_distance((55, 28), (86, 70), 7)
and expecting ~3.93km
, but I get 10.56897.
The linked formula uses cos-1 which means the inverse of the cosine function. cos-1(x) is not the same as 1/cos(x).
It is also known as arccos. In Python it is called acos.