Created
July 9, 2024 21:48
-
-
Save gisfromscratch/5ac13676e836c19fb026b7f293d9feb7 to your computer and use it in GitHub Desktop.
Exploring Walking Areas for EURO 2024 Stadiums Using ArcGIS Location Platform
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from arcgis.gis import GIS | |
from arcgis.features import FeatureLayer, FeatureSet | |
from arcgis.mapping import WebMap | |
from geopedestrian.categories import CategoryRegistry | |
from geopedestrian.services import drive_from, solve_walking, walk_to | |
from georapid.client import GeoRapidClient | |
from georapid.factory import EnvironmentClientFactory | |
import os | |
import pandas as pd | |
def connect_gis() -> GIS: | |
""" | |
Authenticate against ArcGIS Location Platform using the current environment. | |
""" | |
return GIS(api_key=os.environ.get('arcgis_em2024_api_key')) | |
def connect_rapidapi() -> GeoRapidClient: | |
""" | |
Authenticate againg Rapid API using the current environment | |
""" | |
host = 'geopedestrian.p.rapidapi.com' | |
return EnvironmentClientFactory.create_client_with_host(host) | |
def get_stadium_webmap(gis: GIS): | |
""" | |
Returns the web map of all EURO 2024 stadiums. | |
""" | |
return WebMap(gis.content.get('24f45630bf3846e5a59fbc1e8bed0bdd')) | |
def query_locations(stadium_webmap: WebMap): | |
""" | |
Queries the locations of interest and returns a spatially-enabled dataframe. | |
""" | |
WGS84 = 4326 | |
locations_layer = FeatureLayer(stadium_webmap.layers[0].url) | |
locations_fset = locations_layer.query(out_sr=WGS84) | |
return locations_fset.sdf | |
def solve_walking_areas(client: GeoRapidClient, locations_sdf): | |
""" | |
Solve walking areas which are accessible by pedestrians using the specified locations. | |
""" | |
walking_results = locations_sdf.SHAPE.apply(lambda shape: solve_walking(client, shape.y, shape.x)) | |
walking_areas_sdf = None | |
for walking_result in walking_results: | |
if None is walking_areas_sdf: | |
walking_areas_sdf = FeatureSet.from_geojson(walking_result).sdf | |
else: | |
walking_areas_sdf = pd.concat([walking_areas_sdf, FeatureSet.from_geojson(walking_result).sdf], ignore_index=True) | |
return walking_areas_sdf | |
def walk_to_bars(client: GeoRapidClient, locations_sdf): | |
""" | |
Determines which bars are accessible by pedestrian from the specified locations. | |
""" | |
category_registry = CategoryRegistry() | |
category_id = category_registry.find_id('Bar') | |
walking_results = locations_sdf.SHAPE.apply(lambda shape: walk_to(client, shape.y, shape.x, category_id)) | |
walking_places_sdf = None | |
walking_routes_sdf = None | |
for walking_result in walking_results: | |
places_geojson = walking_result['places'] | |
if None is walking_places_sdf: | |
walking_places_sdf = FeatureSet.from_geojson(places_geojson).sdf | |
else: | |
walking_places_sdf = pd.concat([walking_places_sdf, FeatureSet.from_geojson(places_geojson).sdf], ignore_index=True) | |
routes_geojson = walking_result['routes'] | |
if None is walking_routes_sdf: | |
walking_routes_sdf = FeatureSet.from_geojson(routes_geojson).sdf | |
else: | |
walking_routes_sdf = pd.concat([walking_routes_sdf, FeatureSet.from_geojson(routes_geojson).sdf], ignore_index=True) | |
return (walking_places_sdf, walking_routes_sdf) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment