Skip to content

Instantly share code, notes, and snippets.

@colbyr
Created April 7, 2012 18:27
Show Gist options
  • Save colbyr/2331169 to your computer and use it in GitHub Desktop.
Save colbyr/2331169 to your computer and use it in GitHub Desktop.
Lat/Long Distance

I'm having an issue with assignment 3:

for the coordinates (41.721194054071, -73.934258235003) & (33.7199, -116.232), which a the first coordinates used in the test file, my script returns ~ 2353 mi. As far as I can tell that's correct.

Unfortunately, testtour.py seems to be expecting ~ 4705 mi for the first test.

2352.731305 != 4705.462610 ... Incorrect

I've verified that the coordinates are correct:

start_latitude = 41.721194054071
start_longitude = -73.934258235003

and from the graph

{
    id: "264100516989470",
    ...
    {
        city: "Indio",
        state: "California",
        country: "United States",
        latitude: 33.7199,
        longitude: -116.232
    },
    ...
}
import math
def lat_long_distance(c1, c2):
r = 3960 # miles
degrees_to_radians = ((2 * math.pi) / 360)
lat1 = (90.0 - c1[0]) * degrees_to_radians
lat2 = (90.0 - c2[0]) * degrees_to_radians
long1 = c1[1] * degrees_to_radians
long2 = c2[1] * degrees_to_radians
cos = (math.sin(lat1) * math.sin(lat2) * math.cos(long1 - long2) +
math.cos(lat1) * math.cos(lat2))
arc = math.acos( cos )
res = arc * r
return res
def main():
c1 = (41.721194054071, -73.934258235003)
c2 = (33.7199, -116.232)
print c1
print c2
print str(lat_long_distance(c1, c2)) + ' miles'
if __name__ == '__main__':
main()
(41.721194054071, -73.934258235003)
(33.7199, -116.232)
2352.73130502 miles
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment