@@ -105,8 +105,27 @@ def search(api_key, term, location):
105
105
}
106
106
return request (API_HOST , SEARCH_PATH , api_key , url_params = url_params )
107
107
108
+ def lat_lon_search (api_key , lat , lon , radius ):
109
+ """Query Search API using latitude and longitude.
110
+ Args:
111
+ lat (float) : latitude
112
+ lon (float) : longitude
113
+ radius (int) : radius of search in meters
114
+ Returns:
115
+ dict: The JSON response form the request.
116
+ """
117
+ url_params = {
118
+ 'latitude' : lat ,
119
+ 'longitude' : lon ,
120
+ 'radius' : radius ,
121
+ 'limit' : SEARCH_LIMIT ,
122
+ 'sort_by' : 'distance' ,
123
+ 'categories' : 'food,restaurants,shopping,hotels,beautysvc,auto,education,collegeuniv,financialservices,publicservicesgovt'
124
+ }
125
+ return request (API_HOST , SEARCH_PATH , api_key , url_params = url_params )
126
+
108
127
"""
109
- YELP API: Function to retrieve all reviews related to the business.
128
+ YELP API: Function to retrieve all reviews related to the business.
110
129
"""
111
130
def business_reviews (api_key , business_id ):
112
131
business_path = BUSINESS_PATH + business_id
@@ -129,7 +148,7 @@ def get_business_id(api_key, lat, lon):
129
148
return request (API_HOST , SEARCH_PATH , api_key , url_params = url_params )
130
149
131
150
"""
132
- NOMINATIM API: Creates a Nominatim API Call, returns address in string form and dictionary form separated by streetname,
151
+ NOMINATIM API: Creates a Nominatim API Call, returns address in string form and dictionary form separated by streetname,
133
152
road, neighborhood, etc
134
153
"""
135
154
def return_address_from_location_nominatim (lat , lon ):
@@ -164,37 +183,51 @@ def is_service_nominatim(business):
164
183
return True
165
184
166
185
'''
167
- NOMINATIM VERS: Function that RETURNS a list of categories that the business falls into
186
+ NOMINATIM VERS: Function that RETURNS a list of categories that the business falls into
168
187
169
- Using the Google reverse lookup in the except clause, in case Nominatim's results are too vague.
170
- Will first try Nominatim's reverse lookup, but if Nominatim returns a broad "address"
171
- of the street and the city, without a full address with a specific location
188
+ Using the Google reverse lookup in the except clause, in case Nominatim's results are too vague.
189
+ Will first try Nominatim's reverse lookup, but if Nominatim returns a broad "address"
190
+ of the street and the city, without a full address with a specific location
172
191
Such as Piedmont Ave, Berkeley, CA
173
192
174
- Then the function will enter the Google reverse lookup and choose a business that is closest to
193
+ Then the function will enter the Google reverse lookup and choose a business that is closest to
175
194
latitude and longitude given
176
195
'''
177
196
197
+
178
198
### BEGIN: Pulled out candidate functions so that we can evaluate individual accuracies
179
199
def find_destination_business_google (lat , lon ):
180
200
return return_address_from_google_nomfile (lat , lon )
181
201
182
202
def find_destination_business_yelp (lat , lon ):
183
- return (None , None , None , None )
203
+ yelp_from_lat_lon = lat_lon_search (YELP_API_KEY , lat , lon , 250 )
204
+ if yelp_from_lat_lon == {}:
205
+ return (None , None , None , False )
206
+ businesses = yelp_from_lat_lon ['businesses' ]
207
+ if businesses == []:
208
+ return find_destination_business (lat , lon )
209
+ business_name = businesses [0 ]['name' ]
210
+ address = businesses [0 ]['location' ]['address1' ]
211
+ city = businesses [0 ]['location' ]['city' ]
212
+ #If there is no commercial establishment in a 50 meter (1/2 block) radius of coordinate
213
+ #It is safe to assume the area is not a commercial establishment
214
+ location_is_service = True
215
+ print ((business_name , address , city , location_is_service ))
216
+ return (business_name , address , city , location_is_service )
184
217
185
218
def find_destination_business_nominatim (lat , lon ):
186
219
string_address , address_dict = return_address_from_location_nominatim (lat , lon )
187
220
business_key = list (address_dict .keys ())[0 ]
188
221
business_name = address_dict [business_key ]
189
222
try :
190
223
city = address_dict ['city' ]
191
- except :
224
+ except :
192
225
try :
193
226
city = address_dict ["town" ]
194
227
except :
195
228
try :
196
229
zipcode = address_dict ["postcode" ]
197
- city = zipcode_to_city (zipcode )
230
+ city = zipcode_to_city (zipcode )
198
231
except :
199
232
city = ''
200
233
@@ -222,6 +255,7 @@ def find_destination_business(lat, lon):
222
255
% str (return_tuple ))
223
256
return return_tuple
224
257
258
+
225
259
### BEGIN: Pulled out candidate functions so that we can evaluate individual accuracies
226
260
def category_of_business_awesome (lat , lon ):
227
261
return []
@@ -313,8 +347,8 @@ def geojson_to_lat_lon_separated(geojson):
313
347
return lat , lon
314
348
315
349
'''
316
- REWRITE def check_mode_from_trip(cleaned_trip, cleaned_sections, section_counter, trip_counter):
317
- Mode number correspondence:
350
+ REWRITE def check_mode_from_trip(cleaned_trip, cleaned_sections, section_counter, trip_counter):
351
+ Mode number correspondence:
318
352
0: "IN_VEHICLE"
319
353
1: "BIKING"
320
354
2: "ON_FOOT"
@@ -364,7 +398,7 @@ def dummy_starter_suggestion(uuid):
364
398
return modes_from_trips
365
399
366
400
'''
367
- NOMINATIM VERSION: Function to find the review of the original location of the end point of a trip
401
+ NOMINATIM VERSION: Function to find the review of the original location of the end point of a trip
368
402
'''
369
403
def review_start_loc_nominatim (lat , lon ):
370
404
try :
@@ -391,35 +425,35 @@ def review_start_loc_nominatim(lat, lon):
391
425
392
426
As nominatim sometimes is unable to provide a specific location with the city and instead returns
393
427
a postcode (zipcode) and the country name. For the suggestions that we built, the suggestions
394
- require which city (city name) it is in order to look for other similar categoried services
395
- in the area. Thus, this function takes in the INPUT of a zipcode, and RETURNS the name of the city.
428
+ require which city (city name) it is in order to look for other similar categoried services
429
+ in the area. Thus, this function takes in the INPUT of a zipcode, and RETURNS the name of the city.
396
430
397
431
'''
398
432
def zipcode_retrieval (zipcode ):
399
-
400
- # Use this API key first.
433
+
434
+ # Use this API key first.
401
435
url = ZIP_HOST_URL + ZIPCODE_API_KEY + ZIP_FORMAT + zipcode + ZIP_DEGREE
402
436
response = requests .request ('GET' , url = url )
403
437
results = response .json ()
404
438
405
439
if "error_code" in results :
406
- # In case the first API key runs out of requests per hour.
440
+ # In case the first API key runs out of requests per hour.
407
441
url = ZIP_HOST_URL + BACKUP_ZIP_KEY + ZIP_FORMAT + zipcode + ZIP_DEGREE
408
442
response = requests .request ('GET' , url = url )
409
443
return response .json ()
410
444
else :
411
445
return results
412
446
413
-
414
-
447
+
448
+
415
449
def zipcode_to_city (zipcode ):
416
450
response = zipcode_retrieval (zipcode )
417
451
return response ['city' ]
418
452
'''
419
453
NOMINATIM
420
454
In progress-nominatim yelp server suggestion function, first just trying to make end-to-end work before robustifying this function.
421
455
422
- Mode number correspondence:
456
+ Mode number correspondence:
423
457
0: "IN_VEHICLE"
424
458
1: "BIKING"
425
459
2: "ON_FOOT"
@@ -444,7 +478,7 @@ def calculate_yelp_server_suggestion_singletrip_nominatim(uuid, tripidstr):
444
478
start_location = cleaned_trips .data .start_loc
445
479
end_location = cleaned_trips .data .end_loc
446
480
'''
447
- Distance in miles because the current calculated distances is through MapQuest which uses miles,
481
+ Distance in miles because the current calculated distances is through MapQuest which uses miles,
448
482
still working on changing those functions, because haven't found any functions through nominatim
449
483
that calculates distance between points.
450
484
'''
@@ -463,7 +497,7 @@ def calculate_yelp_server_suggestion_for_locations(start_location, end_location,
463
497
464
498
begin_string_address , begin_address_dict = return_address_from_location_nominatim (start_lat , start_lon )
465
499
end_string_address , end_address_dict = return_address_from_location_nominatim (end_lat , end_lon )
466
- try :
500
+ try :
467
501
city = end_address_dict ["city" ]
468
502
except :
469
503
try :
@@ -491,12 +525,12 @@ def calculate_yelp_server_suggestion_for_locations(start_location, end_location,
491
525
if q ['rating' ] >= location_review :
492
526
#'Coordinates' come out as two elements, latitude and longitude
493
527
ratings_bus [q ['name' ]] = (q ['rating' ], q ['alias' ])
494
- obtained = q ['location' ]['display_address' ][0 ] + q ['location' ]['display_address' ][1 ]
528
+ obtained = q ['location' ]['display_address' ][0 ] + q ['location' ]['display_address' ][1 ]
495
529
obtained .replace (' ' , '+' )
496
530
business_locations [q ['name' ]] = obtained
497
531
else :
498
532
return {'message' : error_message_categor , 'question' : None , 'suggested_loc' : None , 'method' : 'bike' , 'rating' : None , 'businessid' : None }
499
- except :
533
+ except :
500
534
return {'message' : error_message_categor , 'question' : None , 'suggested_loc' : None , 'method' : 'bike' , 'rating' : None , 'businessid' : None }
501
535
502
536
#THIS PART WILL BE FIXED ACCODRING TO NOMINATIM AND GET RID OF MAPQUEST (find some other way to calculate distance)
@@ -509,8 +543,8 @@ def calculate_yelp_server_suggestion_for_locations(start_location, end_location,
509
543
if calculate_distance < distance_in_miles and calculate_distance < 5 and calculate_distance >= 1 :
510
544
try :
511
545
question = "How about this location?"
512
- new_message = "We saw that you took a vehicle from" + begin_string_address + "to" + address
513
- suggested_loc = "Instead, there is " + a + "which has better reviews and closer to your original starting point"
546
+ new_message = "We saw that you took a vehicle from" + begin_string_address + "to" + address
547
+ suggested_loc = "Instead, there is " + a + "which has better reviews and closer to your original starting point"
514
548
rating_mess = "Rating of " + str (ratings_bus [a ][0 ])
515
549
#Not sure to include the amount of carbon saved
516
550
#Still looking to see what to return with this message, because currently my latitude and longitudes are stacked together in one string
@@ -519,20 +553,20 @@ def calculate_yelp_server_suggestion_for_locations(start_location, end_location,
519
553
except ValueError as e :
520
554
continue
521
555
elif calculate_distance < distance_in_miles and calculate_distance < 1 :
522
- try :
556
+ try :
523
557
question = "How about this location?"
524
- new_message = "We saw that you took a vehicle from" + begin_string_address + "to" + address
525
- suggested_loc = "Instead, there is " + a + "which has better reviews and closer to your original starting point"
558
+ new_message = "We saw that you took a vehicle from" + begin_string_address + "to" + address
559
+ suggested_loc = "Instead, there is " + a + "which has better reviews and closer to your original starting point"
526
560
rating_mess = "Rating of " + str (ratings_bus [a ][0 ])
527
561
# insert_into_db(tripDict, i, yelp_suggestion_trips, uuid)
528
562
return {'message' : new_message , 'question' : question , 'suggested_loc' : suggested_loc , 'method' : 'walk' , 'rating' : str (ratings_bus [a ][0 ]), 'businessid' : ratings_bus [a ][1 ]}
529
563
except ValueError as e :
530
564
continue
531
565
elif calculate_distance < distance_in_miles and calculate_distance >= 5 and calculate_distance <= 15 :
532
- try :
566
+ try :
533
567
question = "How about this location?"
534
- new_message = "We saw that you took a vehicle from" + begin_string_address + "to" + address
535
- suggested_loc = "Instead, there is " + a + "which has better reviews and closer to your original starting point"
568
+ new_message = "We saw that you took a vehicle from" + begin_string_address + "to" + address
569
+ suggested_loc = "Instead, there is " + a + "which has better reviews and closer to your original starting point"
536
570
rating_mess = "Rating of " + str (ratings_bus [a ][0 ])
537
571
# insert_into_db(tripDict, i, yelp_suggestion_trips, uuid)
538
572
return {'message' : new_message , 'question' : question , 'suggested_loc' : suggested_loc , 'method' : 'public' , 'rating' : str (ratings_bus [a ][0 ]), 'businessid' : ratings_bus [a ][1 ]}
0 commit comments