forked from karan/Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistance.py
55 lines (43 loc) · 1.53 KB
/
distance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
"""
Distance Between Two Cities - Calculates the distance between
two cities and allows the user to specify a unit of distance.
This program may require finding coordinates for the cities
like latitude and longitude.
Uses the Haversine formula
(http://www.movable-type.co.uk/scripts/latlong.html)
Dependencies:
geopy
pip install geopy
"""
from geopy import geocoders # to find lat/lon for the city
import math
R = 6373 # km
city1 = raw_input('Enter city 1: ')
city2 = raw_input('Enter city 2: ')
unit = raw_input('Enter unit of distance (Enter "K" for KM or "M" for MI): ').lower()
if unit in 'km':
g = geocoders.GoogleV3()
try:
city1, (lat1, lon1) = g.geocode(city1)
city2, (lat2, lon2) = g.geocode(city2)
except:
raise Exception('Unable to locate the citites. Check the city names.')
# convert decimal locations to radians
lat1 = math.radians(lat1)
lon1 = math.radians(lon1)
lat2 = math.radians(lat2)
lon2 = math.radians(lon2)
# start haversne formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = (math.sin(dlat/2) ** 2) + math.cos(lat1) * math.cos(lat2) * \
(math.sin(dlon/2) ** 2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
d = R * c
if unit == 'k':
print 'Distance between %s and %s is %.04f km' % (city1, city2, d)
else:
print 'Distance between %s and %s is %.04f mi' % (city1, city2, d / 1.60934)
else:
print 'Invalid unit input!'