Skip to content

Instantly share code, notes, and snippets.

@tyrelsouza
Created August 24, 2012 19:59
Show Gist options
  • Save tyrelsouza/3455038 to your computer and use it in GitHub Desktop.
Save tyrelsouza/3455038 to your computer and use it in GitHub Desktop.
calculate distance on unit sphere with lat long
def distance_on_unit_sphere(lat1, long1, lat2, long2):
#http://www.johndcook.com/python_longitude_latitude.html
degrees_to_radians = math.pi/180.0
phi1 = (90.0 - lat1)*degrees_to_radians
phi2 = (90.0 - lat2)*degrees_to_radians
theta1 = long1*degrees_to_radians
theta2 = long2*degrees_to_radians
cos = (math.sin(phi1)*math.sin(phi2)*math.cos(theta1 - theta2) +
math.cos(phi1)*math.cos(phi2))
try:
arc = math.acos(min(cos,1.0))
except ValueError:
print cos
print cos-1.0
print lat1,lat2
print long1,long2
import sys
sys.exit(1)
feet = arc * 3960 * 5280
feet = round(feet,0)
return feet #returns the distance between two gps locations, in feet.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment