def __query_INFO(search_parameter): gmaps = client(key = Extractor.__API_KEY) #Query google API by parameter_type query_DATA = [] query_results = client.find_place(gmaps, ('+1' + search_parameter) , 'phonenumber', ['name', 'types']) for item in query_results.items(): query_DATA.append(item[1][0]['name']) query_DATA.append(item[1][0]['types'][0]) return query_DATA
def perform_create(self, serializer): gmaps = Client(key=os.environ['GOOGLE_MAPS_KEY']) origin_city = f"{serializer.validated_data['origin_city']}, {serializer.validated_data['origin_state']}" destination_city = f"{serializer.validated_data['destination_city']}, {serializer.validated_data['destination_state']}" origin_gmaps = gmaps.find_place( origin_city, 'textquery', fields=['geometry', 'formatted_address']) destination_gmaps = gmaps.find_place( destination_city, 'textquery', fields=['geometry', 'formatted_address']) serializer.save( owner=self.request.user, origin_lat=origin_gmaps['candidates'][0]['geometry']['location'] ['lat'], origin_lon=origin_gmaps['candidates'][0]['geometry']['location'] ['lng'], destination_lat=destination_gmaps['candidates'][0]['geometry'] ['location']['lat'], destination_lon=destination_gmaps['candidates'][0]['geometry'] ['location']['lng'], )
# First try Geocoder to extract lat long # If that fails, do a places search # If that fails, Then the business is probably shut down. for idx, row in crematory_df.iterrows(): results = Geocoder.geocode(row[1]) time.sleep(2) if results.valid_address == True: # If address is true pulls lat long and updates address with Goog formatted one cords = str(results.coordinates).strip("()") Lat.append(float(cords.split(',')[0])) Long.append(float(cords.split(',')[1])) crematory_df['Address'][idx] = results.formatted_address else: local = gmaps.find_place( input=str(row[0] + ' ' + row[1]), input_type='textquery', fields=['name', 'geometry', 'formatted_address']) if local['status'] == 'OK': # If status is OK, pulls lat long and updates address with Goog formatted one. crematory_df['Address'][idx] = local['candidates'][0][ 'formatted_address'] Lat.append(local['candidates'][0]['geometry']['location']['lat']) Long.append(local['candidates'][0]['geometry']['location']['lng']) else: # If both fail, well then maybe that funeral home is out of business. print("There is no hope for {} Neither API could find it".format( row[1])) bad_rows.append(row[1]) Lat.append(np.NAN) Long.append(np.NAN)