Introduction:
With the increasing need for efficient urban planning and accessibility analysis, spatial data processing has become more vital than ever. In this blog post, I’ll guide you through analyzing walking paths between metro stations and shopping centers in Tehran using Python’s robust geospatial libraries like OSMnx, GeoPandas, Folium, Shapely, and NetworkX. By the end, you’ll be able to extract geographic data, calculate shortest walking paths, and visualize the results on an interactive map.
Step 1: Setting Up Your Environment
To get started, ensure you have Python installed along with the following libraries:
pip install osmnx geopandas folium shapely networkx
These libraries enable seamless spatial data extraction, processing, and visualization.
Step 2: Extracting Spatial Data
We use the OSMnx library to fetch data for Tehran. Specifically, we extract metro station locations and shopping centers.
import osmnx as ox
place_name = "Tehran, Iran"
metro_stations = ox.features_from_place(place_name, tags={'railway': 'station'})
shopping_centers = ox.features_from_place(place_name, tags={'shop': 'mall'})
After extraction, we process the geometries to ensure compatibility for analysis.
Step 3: Building a Walking Network
With OSMnx, we create a walkable network graph:
G = ox.graph_from_place(place_name, network_type='walk')
This graph represents walkable paths in Tehran, enabling pathfinding between locations.
Step 4: Calculating Shortest Paths
To calculate the shortest walking path between a metro station and its nearest shopping center:
import networkx as nx
from shapely.geometry import LineString
def shortest_path(start_point, end_point, graph):
start_node = ox.distance.nearest_nodes(graph, start_point.x, start_point.y)
end_node = ox.distance.nearest_nodes(graph, end_point.x, end_point.y)
return nx.shortest_path(graph, start_node, end_node, weight='length')
We loop through metro stations and find the shortest path to the nearest shopping center.
Step 5: Visualizing on an Interactive Map
Using Folium, we create an interactive map showing metro stations, shopping centers, and the calculated walking paths:
import folium
m = folium.Map(location=[35.6892, 51.3890], zoom_start=14) # Centered on Tehran
# Add metro stations and shopping centers
for idx, row in metro_gdf.iterrows():
folium.Marker(location=[row.geometry.y, row.geometry.x], popup=f"Metro: {row['name']}").add_to(m)
for idx, row in shopping_gdf.iterrows():
folium.Marker(location=[row.geometry.y, row.geometry.x], popup=f"Shopping: {row['name']}").add_to(m)
# Add walking paths
for idx, row in paths_gdf.iterrows():
folium.PolyLine(locations=[(point[1], point[0]) for point in row.geometry.coords], color="red").add_to(m)
m.save("walking_paths_map.html")
The result is a detailed, interactive map that you can view in your browser.
Step 6: Optimizing Path Analysis
For further analysis, filter paths based on specific criteria like distance or accessibility.
paths_proj = paths_gdf.to_crs("EPSG:32639") # Reproject to calculate lengths
paths_proj['length'] = paths_proj['geometry'].length
short_paths = paths_proj[paths_proj['length'] < 300]
This helps identify the shortest or most feasible paths for pedestrians.
Conclusion:
Analyzing urban walking paths can provide valuable insights for urban planners, businesses, and citizens alike. This Python-based approach offers a practical, reproducible way to process spatial data and create visually engaging maps.