Created
August 15, 2018 14:43
-
-
Save sam-thecoder/7f9ae487d64566b6a81fd8c7456a6559 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from geopy.geocoders import Nominatim | |
geolocator = Nominatim(user_agent="semi") | |
from geopy.distance import geodesic | |
def distance_calc(): | |
''' | |
Calculate distance between two places | |
''' | |
loc_1, loc_2 = False, False | |
while loc_1 == False or loc_2 == False: | |
if loc_1 == False: | |
print('Please provide location 1 info, type 1 to provide it in lat, long or 2 to provide the location name') | |
if sys.version.startswith('3'): | |
input_type = input('Input Type: 1 or 2? ') | |
elif sys.version.startswith('2'): | |
input_type = raw_input('Input Type: 1 or 2? ') | |
if input_type == '1' or input_type == '2': | |
if input_type == '1': | |
if sys.version.startswith('3'): | |
lat = input('Enter Latitude: ') | |
longitude = input('Enter Longitude: ') | |
elif sys.version.startswith('2'): | |
lat = raw_input('Enter Latitude: ') | |
longitude = raw_input('Enter Longitude: ') | |
try: | |
lat, longitude = float(lat), float(longitude) | |
loc_1 = True | |
except Exception as e: | |
print('Converting input to valid lat-long failed') | |
print(e) | |
continue | |
else: | |
if sys.version.startswith('3'): | |
location_name = input("Enter the Location's name: ") | |
elif sys.version.startswith('2'): | |
location_name = raw_input("Enter the location's name: ") | |
location = geolocator.geocode(location_name) | |
if type(location) == None.__class__: | |
print('Location Not Found') | |
continue | |
else: | |
print(location.address) | |
lat, longitude = location.latitude, location.longitude | |
loc_1 = True | |
else: | |
print('Invalid Input Type provided: {0}'.format(input_type)) | |
continue | |
if loc_2 == False: | |
print('Please provide location 2 info, type 1 to provide it in lat, long or 2 to provide the location name') | |
if sys.version.startswith('3'): | |
input_type = input('Input Type: 1 or 2? ') | |
elif sys.version.startswith('2'): | |
input_type = raw_input('Input Type: 1 or 2? ') | |
if input_type == '1' or input_type == '2': | |
if input_type == '1': | |
if sys.version.startswith('3'): | |
lat2 = input('Enter Latitude: ') | |
longitude2 = input('Enter Longitude: ') | |
elif sys.version.startswith('2'): | |
lat2 = raw_input('Enter Latitude: ') | |
longitude2 = raw_input('Enter Longitude: ') | |
try: | |
lat2, longitude2 = float(lat), float(longitude) | |
loc_2 = True | |
except Exception as e: | |
print('Converting input to valid lat-long failed') | |
print(e) | |
continue | |
else: | |
if sys.version.startswith('3'): | |
location_name2 = input("Enter the Location's name: ") | |
elif sys.version.startswith('2'): | |
location_name2 = raw_input("Enter the location's name: ") | |
location2 = geolocator.geocode(location_name2) | |
if type(location2) == None.__class__: | |
print('Location Not Found') | |
continue | |
else: | |
print(location2.address) | |
lat2, longitude2 = location2.latitude, location2.longitude | |
loc_2 = True | |
else: | |
print('Invalid Input Type provided: {0}'.format(input_type)) | |
continue | |
get_type = False | |
while get_type == False: | |
if sys.version.startswith('3'): | |
distance_type = input('Would you like the distance in KM or Miles? 1 or 2: ') | |
elif sys.version.startswith('2'): | |
distance_type = raw_input('Would you like the distance in KM or Miles? 1 or 2: ') | |
if distance_type == '1' or distance_type == '2': | |
get_type = True | |
if get_type == '1': | |
print( "{0} KM".format(round(geodesic((lat, longitude), (lat2, longitude2)).km, 2)) ) | |
else: | |
print( "{0} Miles".format(round(geodesic((lat, longitude), (lat2, longitude2)).miles, 2)) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment