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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from google.maps import areainsights_v1
|
| 2 |
+
from google.maps.areainsights_v1.types import ComputeInsightsRequest, Filter, LocationFilter, Insight
|
| 3 |
+
from google.type import latlng_pb2
|
| 4 |
+
import asyncio
|
| 5 |
+
|
| 6 |
+
# DATASET_COLUMNS = [
|
| 7 |
+
# 'Dining and Drinking', 'Community and Government', 'Retail',
|
| 8 |
+
# 'Business and Professional Services', 'Landmarks and Outdoors',
|
| 9 |
+
# 'Arts and Entertainment', 'Health and Medicine',
|
| 10 |
+
# 'Travel and Transportation', 'Sports and Recreation',
|
| 11 |
+
# 'Event'
|
| 12 |
+
# ]
|
| 13 |
+
|
| 14 |
+
DATASET_COLUMNS = [
|
| 15 |
+
'restaurant',
|
| 16 |
+
'bookstore',
|
| 17 |
+
'fast_food',
|
| 18 |
+
'clinic',
|
| 19 |
+
'school',
|
| 20 |
+
'bakery',
|
| 21 |
+
'convenience_store',
|
| 22 |
+
'shopping_mall',
|
| 23 |
+
'atm',
|
| 24 |
+
'bank',
|
| 25 |
+
'dentist',
|
| 26 |
+
'pharmacy',
|
| 27 |
+
'cafe',
|
| 28 |
+
'supermarket',
|
| 29 |
+
'hospital',
|
| 30 |
+
'gym',
|
| 31 |
+
'jewelry_store',
|
| 32 |
+
'charging_station',
|
| 33 |
+
'electronics_store',
|
| 34 |
+
'clothing_store',
|
| 35 |
+
'department_store',
|
| 36 |
+
'hotel',
|
| 37 |
+
'yoga',
|
| 38 |
+
'attraction',
|
| 39 |
+
'college',
|
| 40 |
+
'co_working',
|
| 41 |
+
'university',
|
| 42 |
+
'hostel',
|
| 43 |
+
'viewpoint'
|
| 44 |
+
]
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
GOOGLE_PLACE_TYPE_MAPPING = [
|
| 48 |
+
# Dining and Drinking
|
| 49 |
+
[
|
| 50 |
+
'restaurant', 'bar', 'cafe', 'bakery', 'night_club'
|
| 51 |
+
],
|
| 52 |
+
# Community and Government
|
| 53 |
+
[
|
| 54 |
+
'government_office', 'local_government_office', 'city_hall',
|
| 55 |
+
'courthouse', 'police', 'fire_station', 'post_office', 'library'
|
| 56 |
+
],
|
| 57 |
+
# Retail
|
| 58 |
+
[
|
| 59 |
+
'store', 'shopping_mall', 'grocery_store', 'pharmacy',
|
| 60 |
+
'supermarket', 'drugstore'
|
| 61 |
+
],
|
| 62 |
+
# Business and Professional Services
|
| 63 |
+
[
|
| 64 |
+
'bank', 'atm', 'corporate_office', 'accounting', 'lawyer',
|
| 65 |
+
# 'establishment'
|
| 66 |
+
],
|
| 67 |
+
# Landmarks and Outdoors
|
| 68 |
+
[
|
| 69 |
+
'park', 'tourist_attraction', 'national_park',
|
| 70 |
+
'historical_landmark', 'cultural_landmark'
|
| 71 |
+
],
|
| 72 |
+
# Arts and Entertainment
|
| 73 |
+
[
|
| 74 |
+
'movie_theater', 'museum', 'art_gallery',
|
| 75 |
+
'performing_arts_theater', 'amusement_park', 'aquarium', 'zoo'
|
| 76 |
+
],
|
| 77 |
+
# Health and Medicine
|
| 78 |
+
[
|
| 79 |
+
'hospital', 'doctor', 'dentist', 'pharmacy',
|
| 80 |
+
'physiotherapist', 'spa'
|
| 81 |
+
],
|
| 82 |
+
# Travel and Transportation
|
| 83 |
+
[
|
| 84 |
+
'airport', 'bus_station', 'train_station', 'transit_station',
|
| 85 |
+
'subway_station', 'parking', 'lodging'
|
| 86 |
+
],
|
| 87 |
+
# Sports and Recreation
|
| 88 |
+
[
|
| 89 |
+
'gym', 'stadium', 'bowling_alley', 'fitness_center',
|
| 90 |
+
'park', 'amusement_center'
|
| 91 |
+
],
|
| 92 |
+
# Event (Mapped to common event venues)
|
| 93 |
+
[
|
| 94 |
+
'event_venue', 'convention_center', 'banquet_hall', 'stadium'
|
| 95 |
+
]
|
| 96 |
+
]
|
| 97 |
+
|
| 98 |
+
# import os
|
| 99 |
+
|
| 100 |
+
GOOGLE_PLACE_TYPE_MAPPING = [
|
| 101 |
+
# Original Column: 'restaurant'
|
| 102 |
+
['restaurant'],
|
| 103 |
+
# Original Column: 'bookstore'
|
| 104 |
+
['book_store'],
|
| 105 |
+
# Original Column: 'fast_food'
|
| 106 |
+
['fast_food_restaurant'],
|
| 107 |
+
# Original Column: 'clinic'
|
| 108 |
+
['dental_clinic', 'doctor', 'medical_lab', 'skin_care_clinic'],
|
| 109 |
+
# Original Column: 'school'
|
| 110 |
+
['school', 'primary_school', 'secondary_school', 'preschool'],
|
| 111 |
+
# Original Column: 'bakery'
|
| 112 |
+
['bakery'],
|
| 113 |
+
# Original Column: 'convenience_store'
|
| 114 |
+
['convenience_store'],
|
| 115 |
+
# Original Column: 'shopping_mall'
|
| 116 |
+
['shopping_mall'],
|
| 117 |
+
# Original Column: 'atm'
|
| 118 |
+
['atm'],
|
| 119 |
+
# Original Column: 'bank'
|
| 120 |
+
['bank'],
|
| 121 |
+
# Original Column: 'dentist'
|
| 122 |
+
['dentist', 'dental_clinic'],
|
| 123 |
+
# Original Column: 'pharmacy'
|
| 124 |
+
['pharmacy', 'drugstore'],
|
| 125 |
+
# Original Column: 'cafe'
|
| 126 |
+
['cafe', 'coffee_shop'],
|
| 127 |
+
# Original Column: 'supermarket'
|
| 128 |
+
['supermarket'],
|
| 129 |
+
# Original Column: 'hospital'
|
| 130 |
+
['hospital'],
|
| 131 |
+
# Original Column: 'gym'
|
| 132 |
+
['gym', 'wellness_center'],
|
| 133 |
+
# Original Column: 'jewelry_store'
|
| 134 |
+
['jewelry_store'],
|
| 135 |
+
# Original Column: 'charging_station'
|
| 136 |
+
['electric_vehicle_charging_station'],
|
| 137 |
+
# Original Column: 'electronics_store'
|
| 138 |
+
['electronics_store'],
|
| 139 |
+
# Original Column: 'clothing_store'
|
| 140 |
+
['clothing_store', 'department_store'],
|
| 141 |
+
# Original Column: 'department_store'
|
| 142 |
+
['department_store'],
|
| 143 |
+
# Original Column: 'hotel'
|
| 144 |
+
['hotel', 'motel', 'resort_hotel'],
|
| 145 |
+
# Original Column: 'yoga'
|
| 146 |
+
['yoga_studio'],
|
| 147 |
+
# Original Column: 'attraction'
|
| 148 |
+
['tourist_attraction', 'amusement_park', 'museum', 'cultural_landmark'],
|
| 149 |
+
# Original Column: 'college'
|
| 150 |
+
['university'],
|
| 151 |
+
# Original Column: 'co_working' (No direct Place Type exists, using closest fit)
|
| 152 |
+
['corporate_office'],
|
| 153 |
+
# Original Column: 'university'
|
| 154 |
+
['university'],
|
| 155 |
+
# Original Column: 'hostel'
|
| 156 |
+
['hostel', 'budget_japanese_inn'],
|
| 157 |
+
# Original Column: 'viewpoint' (No direct Place Type exists, using closest fit)
|
| 158 |
+
['tourist_attraction', 'observation_deck']
|
| 159 |
+
]
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
async def compute_places_count_with_api_key(api_key, lat, lng, radius, places):
|
| 164 |
+
try:
|
| 165 |
+
client = areainsights_v1.AreaInsightsAsyncClient(
|
| 166 |
+
client_options={"api_key": api_key}
|
| 167 |
+
)
|
| 168 |
+
|
| 169 |
+
# 1. Define the geographic filter (a circle)
|
| 170 |
+
location_filter = LocationFilter(
|
| 171 |
+
circle=LocationFilter.Circle(
|
| 172 |
+
lat_lng=latlng_pb2.LatLng(latitude=lat, longitude=lng),
|
| 173 |
+
radius=radius
|
| 174 |
+
)
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
# 2. Define the place type filter
|
| 178 |
+
type_filter = areainsights_v1.TypeFilter(
|
| 179 |
+
included_types=places
|
| 180 |
+
)
|
| 181 |
+
|
| 182 |
+
# 3. Assemble the main request body
|
| 183 |
+
request = ComputeInsightsRequest(
|
| 184 |
+
# We want the total count of matching places
|
| 185 |
+
insights=[Insight.INSIGHT_COUNT],
|
| 186 |
+
filter=Filter(
|
| 187 |
+
location_filter=location_filter,
|
| 188 |
+
type_filter=type_filter
|
| 189 |
+
)
|
| 190 |
+
)
|
| 191 |
+
|
| 192 |
+
response = await client.compute_insights(request=request)
|
| 193 |
+
|
| 194 |
+
count = int(response.count)
|
| 195 |
+
|
| 196 |
+
return count
|
| 197 |
+
except Exception as e:
|
| 198 |
+
print(f"An error occurred: {e}")
|
| 199 |
+
return None
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
def compute_features(candidate_point, api_key, radius=5000):
|
| 203 |
+
lat, lon = candidate_point
|
| 204 |
+
|
| 205 |
+
features = {
|
| 206 |
+
'num_banks_in_radius':0,
|
| 207 |
+
# 'total_amenities':0,
|
| 208 |
+
# 'category_diversity':0
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
for i,places in enumerate(GOOGLE_PLACE_TYPE_MAPPING):
|
| 212 |
+
total_count = asyncio.run(compute_places_count_with_api_key(
|
| 213 |
+
api_key,
|
| 214 |
+
lat,
|
| 215 |
+
lon,
|
| 216 |
+
radius,
|
| 217 |
+
places
|
| 218 |
+
))
|
| 219 |
+
|
| 220 |
+
features[f'num_{DATASET_COLUMNS[i]}'] = total_count
|
| 221 |
+
|
| 222 |
+
|
| 223 |
+
n_banks = asyncio.run(compute_places_count_with_api_key(
|
| 224 |
+
api_key,
|
| 225 |
+
lat,
|
| 226 |
+
lon,
|
| 227 |
+
radius,
|
| 228 |
+
['atm']
|
| 229 |
+
))
|
| 230 |
+
|
| 231 |
+
|
| 232 |
+
features.update({
|
| 233 |
+
'num_banks_in_radius': n_banks,
|
| 234 |
+
# 'total_amenities': sum(v for v in features.values()),
|
| 235 |
+
# 'category_diversity': sum(bool(v) for v in features.values())
|
| 236 |
+
})
|
| 237 |
+
|
| 238 |
+
print(features)
|
| 239 |
+
|
| 240 |
+
return features
|