Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import osmnx as ox
- import os
- import glob
- import pandas as pd
- from osmnx._errors import InsufficientResponseError
- from tqdm import tqdm
- from shapely.geometry import Polygon
- base_dir = os.getcwd()
- # Construct the relative path to data_tools
- # Step 1: Set the directory containing the .csv files
- poi_path = os.path.join(base_dir,"../../data/amenities")
- keys= ["amenity"]
- # Define the multi_polygon for the region of interest (e.g., Portugal)
- multi_polygon = ox.geocoder.geocode_to_gdf('Portugal').geometry.iloc[0]
- # Loop through each key and fetch the features
- for key in tqdm(keys, desc="Processing keys"): # missed_keys
- # Create the tags dictionary for the current key
- tags = {key: True}
- # Fetch features from OSMnx
- try:
- gdf = ox.features.features_from_polygon(multi_polygon, tags=tags)
- # Define the filename dynamically based on the key
- filename = f"../../data/amenities/{key.replace(':', '_')}.csv"
- # Save the GeoDataFrame to a CSV file
- gdf.to_csv(filename, index=False)
- print(f"Saved {key} to {filename}")
- except Exception as e:
- print(f"Error processing {key}: {e}")
Advertisement
Add Comment
Please, Sign In to add comment