Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does my implementation of the Great Circle distance formula give wrong results?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading