Skip to content

Instantly share code, notes, and snippets.

@SethCalkins
Forked from yevrah/distances.py
Last active August 29, 2015 14:11
Show Gist options
  • Save SethCalkins/43a1361d260b63f00a5d to your computer and use it in GitHub Desktop.
Save SethCalkins/43a1361d260b63f00a5d to your computer and use it in GitHub Desktop.
import requests
import math
from BeautifulSoup import BeautifulSoup
def distance_on_unit_sphere(lat1, long1, lat2, long2):
"src: 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))
arc = math.acos( cos )
return arc * 3960 # To get the distance in kilometers, multiply by 6373 instead
def main():
r = requests.get('http://www.freemaptools.com/ajax/getaandb.php?a=Sydney_Australia&b=Melbourne_Australia&c=1317')
xml = BeautifulSoup(r.text)
lat1 = float(xml.markers.findAll('marker')[0]['lat']);
lng1 = float(xml.markers.findAll('marker')[0]['lng']);
lat2 = float(xml.markers.findAll('marker')[1]['lat']);
lng2 = float(xml.markers.findAll('marker')[1]['lng']);
print distance_on_unit_sphere(lat1, lng1, lat2, lng2)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment