Simple Location Based Sevice

This program returns nearby places of interest given the location of a vehicle.


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)


What services would you like to search for?
1. Restaurants
2. Bus Stops
3. Parks
4. Police Station
5. Fire Stations
Please choose an option : 1
Please input the nearby distance : 2
name :  Athithi Grand Restaurant  latitude :  12.8913821986  longitude :  77.5649088621
name :  Munch  latitude :  12.8852685278  longitude :  77.5824967772
name :  Domino's Pizza  latitude :  12.894064  longitude :  77.56816
name :  Delhi Sweets  latitude :  12.8862019622  longitude :  77.5821360201
name :  Just Bake  latitude :  12.8834058974  longitude :  77.5831069797
name :  New Kabab Zone  latitude :  12.8718418523  longitude :  77.5834047049
name :  Gangadkar's  latitude :  12.890763  longitude :  77.564793
name :  New Chicken Day  latitude :  12.8895777778  longitude :  77.5634527778
name :  SV Juice Corner  latitude :  12.871425  longitude :  77.5833666667
name :  Sree Kanya  latitude :  12.8717621007  longitude :  77.5830932334
name :  Atithi Biryani Corner  latitude :  12.8767154788  longitude :  77.5839555636
name :  Kaggis  latitude :  12.8833833333  longitude :  77.5673055556
name :  Delicacy  latitude :  12.8825584107  longitude :  77.5835324451