Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

geo_distance crashes with specific coordinates #222

Open
llybin opened this issue Mar 31, 2021 · 0 comments
Open

geo_distance crashes with specific coordinates #222

llybin opened this issue Mar 31, 2021 · 0 comments

Comments

@llybin
Copy link

llybin commented Mar 31, 2021

a = b = Point(113.9192428, 22.4876533)
a_y = radians(a.y)
b_y = radians(b.y)
delta_x = radians(a.x - b.x)
cos_x = sin(a_y) * sin(b_y) + cos(a_y) * cos(b_y) * cos(delta_x)
cos_x
1.0000000000000002

acos(cos_x) couldn't be higher 1

I fixed that:

def geo_distance(a, b):
    """
    Distance between two geo points in km. (p.x = long, p.y = lat)
    :param a: (Point)
    :param b: (Point)
    :return: float
    """
    if a == b:
        return 0.

    a_y = radians(a.y)
    b_y = radians(b.y)
    delta_x = radians(a.x - b.x)
    cos_x = sin(a_y) * sin(b_y) + cos(a_y) * cos(b_y) * cos(delta_x)
    # fix python float precision 1.0000000000000002
    cos_x = cos_x if cos_x < 1 else 1
    return acos(cos_x) * 6370.986  # PostGIS earth radius
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
@llybin and others