CamolaZ

OSM Extract key amenities

Jul 10th, 2025
794
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.18 KB | None | 0 0
  1. import osmnx as ox
  2. import os
  3. import glob
  4. import pandas as pd
  5. from osmnx._errors import InsufficientResponseError
  6. from tqdm import tqdm
  7. from shapely.geometry import Polygon
  8.  
  9. base_dir = os.getcwd()
  10.  
  11. # Construct the relative path to data_tools
  12. # Step 1: Set the directory containing the .csv files
  13. poi_path = os.path.join(base_dir,"../../data/amenities")
  14.  
  15. keys= ["amenity"]
  16.  
  17. # Define the multi_polygon for the region of interest (e.g., Portugal)
  18. multi_polygon = ox.geocoder.geocode_to_gdf('Portugal').geometry.iloc[0]
  19.  
  20.  
  21. # Loop through each key and fetch the features
  22. for key in tqdm(keys, desc="Processing keys"): # missed_keys
  23.     # Create the tags dictionary for the current key
  24.     tags = {key: True}
  25.    
  26.     # Fetch features from OSMnx
  27.     try:
  28.         gdf = ox.features.features_from_polygon(multi_polygon, tags=tags)
  29.        
  30.         # Define the filename dynamically based on the key
  31.         filename = f"../../data/amenities/{key.replace(':', '_')}.csv"
  32.        
  33.         # Save the GeoDataFrame to a CSV file
  34.         gdf.to_csv(filename, index=False)
  35.         print(f"Saved {key} to {filename}")
  36.     except Exception as e:
  37.         print(f"Error processing {key}: {e}")
Advertisement
Add Comment
Please, Sign In to add comment