|
|
from google.maps import areainsights_v1 |
|
|
from google.maps.areainsights_v1.types import ComputeInsightsRequest, Filter, LocationFilter, Insight |
|
|
from google.type import latlng_pb2 |
|
|
import asyncio |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DATASET_COLUMNS = [ |
|
|
'restaurant', |
|
|
'bookstore', |
|
|
'fast_food', |
|
|
'clinic', |
|
|
'school', |
|
|
'bakery', |
|
|
'convenience_store', |
|
|
'shopping_mall', |
|
|
'atm', |
|
|
'bank', |
|
|
'dentist', |
|
|
'pharmacy', |
|
|
'cafe', |
|
|
'supermarket', |
|
|
'hospital', |
|
|
'gym', |
|
|
'jewelry_store', |
|
|
'charging_station', |
|
|
'electronics_store', |
|
|
'clothing_store', |
|
|
'department_store', |
|
|
'hotel', |
|
|
'yoga', |
|
|
'attraction', |
|
|
'college', |
|
|
'co_working', |
|
|
'university', |
|
|
'hostel', |
|
|
'viewpoint' |
|
|
] |
|
|
|
|
|
|
|
|
GOOGLE_PLACE_TYPE_MAPPING = [ |
|
|
|
|
|
[ |
|
|
'restaurant', 'bar', 'cafe', 'bakery', 'night_club' |
|
|
], |
|
|
|
|
|
[ |
|
|
'government_office', 'local_government_office', 'city_hall', |
|
|
'courthouse', 'police', 'fire_station', 'post_office', 'library' |
|
|
], |
|
|
|
|
|
[ |
|
|
'store', 'shopping_mall', 'grocery_store', 'pharmacy', |
|
|
'supermarket', 'drugstore' |
|
|
], |
|
|
|
|
|
[ |
|
|
'bank', 'atm', 'corporate_office', 'accounting', 'lawyer', |
|
|
|
|
|
], |
|
|
|
|
|
[ |
|
|
'park', 'tourist_attraction', 'national_park', |
|
|
'historical_landmark', 'cultural_landmark' |
|
|
], |
|
|
|
|
|
[ |
|
|
'movie_theater', 'museum', 'art_gallery', |
|
|
'performing_arts_theater', 'amusement_park', 'aquarium', 'zoo' |
|
|
], |
|
|
|
|
|
[ |
|
|
'hospital', 'doctor', 'dentist', 'pharmacy', |
|
|
'physiotherapist', 'spa' |
|
|
], |
|
|
|
|
|
[ |
|
|
'airport', 'bus_station', 'train_station', 'transit_station', |
|
|
'subway_station', 'parking', 'lodging' |
|
|
], |
|
|
|
|
|
[ |
|
|
'gym', 'stadium', 'bowling_alley', 'fitness_center', |
|
|
'park', 'amusement_center' |
|
|
], |
|
|
|
|
|
[ |
|
|
'event_venue', 'convention_center', 'banquet_hall', 'stadium' |
|
|
] |
|
|
] |
|
|
|
|
|
|
|
|
|
|
|
GOOGLE_PLACE_TYPE_MAPPING = [ |
|
|
|
|
|
['restaurant'], |
|
|
|
|
|
['book_store'], |
|
|
|
|
|
['fast_food_restaurant'], |
|
|
|
|
|
['dental_clinic', 'doctor', 'medical_lab', 'skin_care_clinic'], |
|
|
|
|
|
['school', 'primary_school', 'secondary_school', 'preschool'], |
|
|
|
|
|
['bakery'], |
|
|
|
|
|
['convenience_store'], |
|
|
|
|
|
['shopping_mall'], |
|
|
|
|
|
['atm'], |
|
|
|
|
|
['bank'], |
|
|
|
|
|
['dentist', 'dental_clinic'], |
|
|
|
|
|
['pharmacy', 'drugstore'], |
|
|
|
|
|
['cafe', 'coffee_shop'], |
|
|
|
|
|
['supermarket'], |
|
|
|
|
|
['hospital'], |
|
|
|
|
|
['gym', 'wellness_center'], |
|
|
|
|
|
['jewelry_store'], |
|
|
|
|
|
['electric_vehicle_charging_station'], |
|
|
|
|
|
['electronics_store'], |
|
|
|
|
|
['clothing_store', 'department_store'], |
|
|
|
|
|
['department_store'], |
|
|
|
|
|
['hotel', 'motel', 'resort_hotel'], |
|
|
|
|
|
['yoga_studio'], |
|
|
|
|
|
['tourist_attraction', 'amusement_park', 'museum', 'cultural_landmark'], |
|
|
|
|
|
['university'], |
|
|
|
|
|
['corporate_office'], |
|
|
|
|
|
['university'], |
|
|
|
|
|
['hostel', 'budget_japanese_inn'], |
|
|
|
|
|
['tourist_attraction', 'observation_deck'] |
|
|
] |
|
|
|
|
|
|
|
|
|
|
|
async def compute_places_count_with_api_key(api_key, lat, lng, radius, places): |
|
|
try: |
|
|
client = areainsights_v1.AreaInsightsAsyncClient( |
|
|
client_options={"api_key": api_key} |
|
|
) |
|
|
|
|
|
|
|
|
location_filter = LocationFilter( |
|
|
circle=LocationFilter.Circle( |
|
|
lat_lng=latlng_pb2.LatLng(latitude=lat, longitude=lng), |
|
|
radius=radius |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
type_filter = areainsights_v1.TypeFilter( |
|
|
included_types=places |
|
|
) |
|
|
|
|
|
|
|
|
request = ComputeInsightsRequest( |
|
|
|
|
|
insights=[Insight.INSIGHT_COUNT], |
|
|
filter=Filter( |
|
|
location_filter=location_filter, |
|
|
type_filter=type_filter |
|
|
) |
|
|
) |
|
|
|
|
|
response = await client.compute_insights(request=request) |
|
|
|
|
|
count = int(response.count) |
|
|
|
|
|
return count |
|
|
except Exception as e: |
|
|
print(f"An error occurred: {e}") |
|
|
return None |
|
|
|
|
|
|
|
|
def compute_features(candidate_point, api_key, radius=5000): |
|
|
lat, lon = candidate_point |
|
|
|
|
|
features = { |
|
|
'num_banks_in_radius':0, |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
for i,places in enumerate(GOOGLE_PLACE_TYPE_MAPPING): |
|
|
total_count = asyncio.run(compute_places_count_with_api_key( |
|
|
api_key, |
|
|
lat, |
|
|
lon, |
|
|
radius, |
|
|
places |
|
|
)) |
|
|
|
|
|
features[f'num_{DATASET_COLUMNS[i]}'] = total_count |
|
|
|
|
|
|
|
|
n_banks = asyncio.run(compute_places_count_with_api_key( |
|
|
api_key, |
|
|
lat, |
|
|
lon, |
|
|
radius, |
|
|
['atm'] |
|
|
)) |
|
|
|
|
|
|
|
|
features.update({ |
|
|
'num_banks_in_radius': n_banks, |
|
|
|
|
|
|
|
|
}) |
|
|
|
|
|
print(features) |
|
|
|
|
|
return features |