def output_nearest_place(latitude, longitude, poi):
    """ Given a lat, long and place of interest,
    it prints the nearest place """
    global progress
    curr_location = str(latitude) + "," + str(longitude)

    incrementm = distance_on_unit_sphere(increment, 0, 0, 0)
    search_radius = str(int(ceil(sqrt(incrementm**2/2)*1000)))

    url = "https://maps.googleapis.com/maps/api"
    url += "/place/search/json?location="
    url += curr_location + "&sensor=false&key="
    url += GOOGLE_API_KEY + "&radius=" + search_radius + "&types=" + poi

    # Ping the API, and take a break if Google is angry
    while True:
        try:
            response = requests.get(url)
            break
        except requests.ConnectionError:
            time.sleep(randrange(3, 9))

    d = response.json()

    # Output the nearest grocery store
    if len(d['results']) > 0:

        least_miles = 999999999
        for g in d['results']:
            near_lat = g['geometry']['location']['lat']
            near_lng = g['geometry']['location']['lng']
            dist_miles = (distance_on_unit_sphere(latitude,
                          longitude, near_lat, near_lng))
            if dist_miles < least_miles:
                least_miles = dist_miles
                nearest_lat = near_lat
                nearest_lng = near_lng
        # Prevents issues with file writing
        print_lock.acquire()
        try:
            print(poi + ',' + curr_location + ','
                  + str(nearest_lat) + ',' + str(nearest_lng)
                  + ',' + str(least_miles))
            progress += 1
            progress_bar.update(progress)
        finally:
            print_lock.release()
    # Or, no location found
    else:
        print_lock.acquire()
        try:
            print(poi + ',' + curr_location + ',,,')
            progress += 1
            progress_bar.update(progress)
        finally:
            print_lock.release()
# Fredericton
# lat_NW = 45.998931
# lng_NW = -66.748867
# lat_SE = 45.870202
# lng_SE = -66.550709

if gridsize == "small":
    incr = 0.0035961575091
else:
    incr = .2697118131790

# Start in the northwest and iterate to the southeast
print("lat,lng,latnear,lngnear,dist_km")

lat_dist = str(distance_on_unit_sphere(1, 0, 0, 0))
lng_dist = str(distance_on_unit_sphere(approx_longitude, 1,
               approx_longitude, 0))

if scantype == "grid":
    locations = create_area_grid(lat_NW, lng_NW, lat_SE,
                                 lng_SE, increment=incr)
elif scantype == "city_list":
    locations = cities_list(listname)
else:
    locations = create_buildings_list(listname)

for location in locations:
    # Equirectangular approximation at 49th parallel:
    # http://www.movable-type.co.uk/scripts/latlong.html
    sql = "SELECT latitude, longitude,SQRT(POW(" + lat_dist + " * ("
        # If still no result, radius 50,000 meters
        if len(d['results']) == 0:
            url = 'https://maps.googleapis.com/maps/api/place/search/json?location=' + curr_location + '&sensor=false&key='+GOOGLE_API_KEY+'&radius=50000&types=grocery_or_supermarket'
            response = urllib2.urlopen(url)
            result = response.read()
            d = simplejson.loads(result)
            total_calls += 1
        
        # Output nearest grocery store
        if len(d['results']) > 0:
            
            least_miles = 999999999
            for g in d['results']:
                near_lat = g['geometry']['location']['lat']
                near_lng = g['geometry']['location']['lng']
                dist_miles = distance_on_unit_sphere(lat_curr, lng_curr, near_lat, near_lng)
                if dist_miles < least_miles:
                    least_miles = dist_miles
                    nearest_lat = near_lat
                    nearest_lng = near_lng
            print curr_location + ',' + str(nearest_lat) + ',' + str(nearest_lng) + ',' + str(least_miles)
        
        # Or no location found
        else:
            print curr_location + ',,,'
        
        lng_curr += lng_incr
    lat_curr += lat_incr

# DEBUG 
# print 'Total calls: ' + str(total_calls)
Example #4
0
        # If still no result, radius 50,000 meters
        if len(d['results']) == 0:
            url = 'https://maps.googleapis.com/maps/api/place/search/json?location=' + curr_location + '&sensor=false&key=' + GOOGLE_API_KEY + '&radius=50000&types=grocery_or_supermarket'
            response = urllib2.urlopen(url)
            result = response.read()
            d = simplejson.loads(result)
            total_calls += 1

        # Output nearest grocery store
        if len(d['results']) > 0:

            least_miles = 999999999
            for g in d['results']:
                near_lat = g['geometry']['location']['lat']
                near_lng = g['geometry']['location']['lng']
                dist_miles = distance_on_unit_sphere(lat_curr, lng_curr,
                                                     near_lat, near_lng)
                if dist_miles < least_miles:
                    least_miles = dist_miles
                    nearest_lat = near_lat
                    nearest_lng = near_lng
            print curr_location + ',' + str(nearest_lat) + ',' + str(
                nearest_lng) + ',' + str(least_miles)

        # Or no location found
        else:
            print curr_location + ',,,'

        lng_curr += lng_incr
    lat_curr += lat_incr

# DEBUG