Need help calculating geographical distance

5k views Asked by At

I'm setting up a small program to take 2 geographical coordinates from a user and then calculate the distance between them(taking into account the curvature of the earth). So I looked up wikipedia on what the formula is here.

I basically set up my python function based on that and this is what I came up with:

def geocalc(start_lat, start_long, end_lat, end_long):
    start_lat = math.radians(start_lat)
    start_long = math.radians(start_long)
    end_lat = math.radians(end_long)
    end_long = math.radians(end_long)

    d_lat = start_lat - end_lat
    d_long = start_long - end_long

    EARTH_R = 6372.8

    c = math.atan((math.sqrt( (math.cos(end_lat)*d_long)**2 +( (math.cos(start_lat)*math.sin(end_lat)) - (math.sin(start_lat)*math.cos(end_lat)*math.cos(d_long)))**2)) / ((math.sin(start_lat)*math.sin(end_lat)) + (math.cos(start_lat)*math.cos(end_lat)*math.cos(d_long))) )

    return EARTH_R*c

The problem is that the results come out really inaccurate. I'm new to python so some help or advice would be greatly appreciated!

4

There are 4 answers

1
John Machin On BEST ANSWER

You've got 4 or 5 or 6 problems:

(1) end_lat = math.radians(end_long) should be end_lat = math.radians(end_lat)

(2) you are missing some stuff as somebody already mentioned, most probably because

(3) your code is illegible (line far too long, redundant parentheses, 17 pointless instances of "math.")

(4) you didn't notice the remark in the Wikipedia article about using atan2()

(5) You may have been swapping lat and lon when entering your coordinates

(6) delta(latitude) is computed unnecessarily; it doesn't appear in the formula

Putting it all together:

from math import radians, sqrt, sin, cos, atan2

def geocalc(lat1, lon1, lat2, lon2):
    lat1 = radians(lat1)
    lon1 = radians(lon1)
    lat2 = radians(lat2)
    lon2 = radians(lon2)

    dlon = lon1 - lon2

    EARTH_R = 6372.8

    y = sqrt(
        (cos(lat2) * sin(dlon)) ** 2
        + (cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dlon)) ** 2
        )
    x = sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(dlon)
    c = atan2(y, x)
    return EARTH_R * c



>>> geocalc(36.12, -86.67, 33.94, -118.40)
2887.2599506071115
>>> geocalc(-6.508, 55.071, -8.886, 51.622)
463.09798886300376
>>> geocalc(55.071, -6.508, 51.622, -8.886)
414.7830891822618
1
vasek1 On

You can use the geopy module which has a built-in function for distance calculations, scroll down to "Calculating Distances" in the link below: https://pypi.python.org/pypi/geopy

1
nmjohn On

I think you missed a math.sin(d_long) towards the beginning, should maybe be this:

 c = math.atan((math.sqrt( (math.cos(end_lat)*math.sin(d_long))**2 +( (math.cos(start_lat)*math.sin(end_lat)) - (math.sin(start_lat)*math.cos(end_lat)*math.cos(d_long)))**2)) / ((math.sin(start_lat)*math.sin(end_lat)) + (math.cos(start_lat)*math.cos(end_lat)*math.cos(d_long))) )
5
sgallen On

This works (print f returns 2887.26 km as per the worked example @ http://en.wikipedia.org/wiki/Great-circle_distance):

import math

def geocalc(start_lat, start_long, end_lat, end_long):

    start_lat = math.radians(start_lat)
    start_long = math.radians(start_long)
    end_lat = math.radians(end_lat)
    end_long = math.radians(end_long)

    d_lat = math.fabs(start_lat - end_lat)
    d_long = math.fabs(start_long - end_long)

    EARTH_R = 6372.8

    y = ((math.sin(start_lat)*math.sin(end_lat)) + (math.cos(start_lat)*math.cos(end_lat)*math.cos(d_long)))

    x = math.sqrt((math.cos(end_lat)*math.sin(d_long))**2 + ( (math.cos(start_lat)*math.sin(end_lat)) - (math.sin(start_lat)*math.cos(end_lat)*math.cos(d_long)))**2)

    c = math.atan(x/y)

    return EARTH_R*c

f = geocalc(36.12, -86.67, 33.94, -118.40)
print f

Notice this line in your submission: end_lat = math.radians(end_long)