Skip to content

Instantly share code, notes, and snippets.

@djhume
Created July 8, 2013 04:25
Show Gist options
  • Save djhume/5946219 to your computer and use it in GitHub Desktop.
Save djhume/5946219 to your computer and use it in GitHub Desktop.
Example of how to calc the distance between lat/long pairs for yachting course, for Phil. Geopandas might be good for this...
import math
def distance(origin, destination): #https://gist.github.com/rochacbruno/2883505
lat1, lon1 = origin
lat2, lon2 = destination
radius = 6371 # km
dlat = math.radians(lat2-lat1)
dlon = math.radians(lon2-lon1)
a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) \
* math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
d = radius * c
return d
c="SJ-4-16-5-FJ"
c_list = c.split('-')
marker_lat_long = {'SJ':(41.3,175),'4':(41.2889,174.7772),'16':(41.4889,174.5772),'5':(41.3889,174.8772),'FJ':(41.3,175)}
total_dist=0
for i,marker in enumerate(c_list):
if i>0:
travelled = distance(marker_lat_long[c_list[i-1]],marker_lat_long[c_list[i]])
total_dist+=travelled
print "Distance from: " + c_list[i-1] + " to " + c_list[i] + " = %.2f km; Total=%.2f km" % (travelled,total_dist)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment