Spaces:
Runtime error
Runtime error
| from collections import Counter | |
| import pandas as pd | |
| import numpy as np | |
| # from scipy.spatial import cKDTree | |
| # df_amenities = pd.read_csv("df_indonesia.csv").rename( | |
| # columns={"latitude":"lat", "longitude":"lon"} | |
| # ) | |
| # df_banks = pd.read_csv("df_bank_indonesia.csv").rename( | |
| # columns={"latitude":"lat", "longitude":"lon"} | |
| # ) | |
| # df_amenities["fsq_category_labels"] = df_amenities["fsq_category_labels"].apply( | |
| # lambda x: eval(x) | |
| # ) | |
| # bank_coords = df_banks[['lat','lon']].values | |
| # tree_banks = cKDTree(bank_coords) | |
| # amenity_coords = df_amenities[['lat','lon']].values | |
| # tree_amenities = cKDTree(amenity_coords) | |
| 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' | |
| ] | |
| import os | |
| 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 | |
| async def compute_places_count_with_api_key(api_key, lat, lng, radius, place_type): | |
| 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=[place_type] | |
| included_types=place | |
| ) | |
| # 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,place in enumerate(GOOGLE_PLACE_TYPE_MAPPING): | |
| total_count = await compute_places_count_with_api_key( | |
| api_key, | |
| lat, | |
| lon, | |
| radius, | |
| place | |
| ) | |
| features[f'num_{DATASET_COLUMNS[i]}'] = total_count | |
| n_banks = 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()) | |
| }) | |
| return features |