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