In [1]:
import json #to import json module
from math import radians, cos, sin, asin, sqrt
In [2]:
def haversine(lon1, lat1, lon2, lat2):
"""
Calculate the distance between two points on the earth in Kms
"""
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
r = 6371 # Radius of earth in kilometers. Use 3956 for miles
return c * r
Data about restaurants, parks, fire stations, police stations and bus stops are stored in json format.
In [3]:
with open('restaurants.json') as res_data:#reading json data of restaurants
restaurantsData = json.load(res_data)
In [4]:
with open('fireStations.json') as res_data:#reading json data of fire stations
fireStationsData = json.load(res_data)
In [5]:
with open('parks.json') as res_data:#reading json data of parks
parksData = json.load(res_data)
In [6]:
with open('policeStation.json') as res_data:#reading json data of police stations
policeStationData = json.load(res_data)
In [7]:
with open('busStops.json') as res_data:#reading json data of bus Stops
busStopsData = json.load(res_data)
Latitude and Longitudes of different points in Bangalore.
In [8]:
#lat = 12.961009 #domlur
#lon = 77.637938
#lat = 12.952240 #marathalli
#lon = 77.700233
#lat = 12.937037 #Sony world signal
#lon = 77.626488
lat = 12.876535
lon = 77.566612
Menu driven program to find services.
In [9]:
def returnFacilities(pref, dist):
if(pref == 1):#searching for restaurants
listOfFeasibleRestaurants = []
for eachRes in restaurantsData:#iterating over each result in restaurants data list
try:
restData = []
resLatitude = float(eachRes['latitude'])
resLongitude = float(eachRes['longitude'])
distFromYourLocation = haversine(lon, lat, resLongitude, resLatitude)
if(distFromYourLocation < dist):
#storing the restaurant's name, latitude and longitude
resName = eachRes['name']
restData.append(resName)
restData.append(resLatitude)
restData.append(resLongitude)
listOfFeasibleRestaurants.append(restData)
except Exception,e:
#print eachRes['id']
#print repr(e)
continue
if(len(listOfFeasibleRestaurants) != 0):
for eachRest in listOfFeasibleRestaurants:
print "name : ", eachRest[0], " latitude : ", eachRest[1], " longitude : ", eachRest[2]
else:
print "Please increase the radius of search distance!"
elif(pref == 2):#searching for bus stops
listOfFeasibleBusStops = []
for eachRes in busStopsData:#iterating over each result in busStop data list
try:
busStop = []
resLatitude = float(eachRes['latitude'])
resLongitude = float(eachRes['longitude'])
distFromYourLocation = haversine(lon, lat, resLongitude, resLatitude)
if(distFromYourLocation < dist):
#storing the bus stop's name, latitude and longitude
busStopName = eachRes['Bus Stops']
busStop.append(busStopName)
busStop.append(resLatitude)
busStop.append(resLongitude)
listOfFeasibleBusStops.append(busStop)
except Exception,e:
#print eachRes['id']
#print repr(e)
continue
if(len(listOfFeasibleBusStops) != 0):
for eachBusStop in listOfFeasibleBusStops:
print "name : ", eachBusStop[0], " latitude : ", eachBusStop[1], " longitude : ", eachBusStop[2]
else:
print "Please increase the radius of search distance!"
elif(pref == 3):#searching for Parks
listOfFeasibleParks = []
for eachRes in parksData:#iterating over each result in parks data list
try:
parks = []
resLatitude = float(eachRes['latitude'])
resLongitude = float(eachRes['longitude'])
distFromYourLocation = haversine(lon, lat, resLongitude, resLatitude)
if(distFromYourLocation < dist):
#storing the parks' address, latitude and longitude
parksAddress = eachRes['Address of the Park']
parks.append(parksAddress)
parks.append(resLatitude)
parks.append(resLongitude)
listOfFeasibleParks.append(parks)
except Exception,e:
#print eachRes['id']
#print repr(e)
continue
if(len(listOfFeasibleParks) != 0):
for eachPark in listOfFeasibleParks:
print "Address : ", eachPark[0], " latitude : ", eachPark[1], " longitude : ", eachPark[2]
else:
print "Please increase the radius of search distance!"
elif(pref == 4):#searching for Police stations
listOfFeasiblePoliceStations = []
for eachRes in policeStationData:#iterating over each result in police stations data list
try:
policeData = []
resLatitude = float(eachRes['latitude'])
resLongitude = float(eachRes['longitude'])
distFromYourLocation = haversine(lon, lat, resLongitude, resLatitude)
if(distFromYourLocation < dist):
#storing the police station's name, latitude and longitude
policeStationName = eachRes['Police Station Name']
policeData.append(policeStationName)
policeData.append(resLatitude)
policeData.append(resLongitude)
listOfFeasiblePoliceStations.append(policeData)
except Exception,e:
#print eachRes['id']
#print repr(e)
continue
if(len(listOfFeasiblePoliceStations) != 0):
for eachPoliceStation in listOfFeasiblePoliceStations:
print "name : ", eachPoliceStation[0], " latitude : ", eachPoliceStation[1], " longitude : ", eachPoliceStation[2]
else:
print "Please increase the radius of search distance!"
elif(pref == 5):#searching for Fire stations
listOfFeasibleFireStations = []
for eachRes in fireStationsData:#iterating over each result in fire stations data list
try:
fireStation = []
resLatitude = float(eachRes['latitude'])
resLongitude = float(eachRes['longitude'])
distFromYourLocation = haversine(lon, lat, resLongitude, resLatitude)
if(distFromYourLocation < dist):
#storing the fire stations's address, latitude and longitude
fireStationAddress = eachRes['Fire Station Address']
fireStation.append(fireStationAddress)
fireStation.append(resLatitude)
fireStation.append(resLongitude)
listOfFeasibleFireStations.append(fireStation)
except Exception,e:
#print eachRes['id']
#print repr(e)
continue
if(len(listOfFeasibleFireStations) != 0):
for eachFireStation in listOfFeasibleFireStations:
print "Address : ", eachFireStation[0], " latitude : ", eachFireStation[1], " longitude : ", eachFireStation[2]
else:
print "Please increase the radius of search distance!"
else:
print "invalid input. Please choose a valid option."
#askUser()
In [10]:
def askUser(lat, lon):
print "What services would you like to search for?"
print "1. Restaurants"
print "2. Bus Stops"
print "3. Parks"
print "4. Police Station"
print "5. Fire Stations"
pref = int(raw_input("Please choose an option : "))
dist = float(raw_input("Please input the nearby distance : "))
returnFacilities(pref, dist)
In [11]:
askUser(lat, lon)