Skip to content
Araz Shah
Menu
  • Home
  • About me
  • Contact me
  • CV
  • Online Courses
    • Apply Now !
    • In-Depth
    • Courses
      • Concepts
      • Python Course
      • GIS Developer Course
    • Price
Menu

How to Analyze Walking Paths Between Metro Stations and Shopping Centers in Tehran Using Python

Posted on January 7, 2025January 7, 2025 by admin

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.

Category: GIS, programming, python, Tutorials

Post navigation

← Comparing Geospatial Data Formats
Exploring Spatial Density with Python: KDE Analysis of Schools in Tehran →

Recent Posts

  • Geospatial Risk Assessment: A Python Approach
  • Analyzing Employee Arrival Patterns and Delays Using Geospatial Data
  • Real-Time GPS Tracking on a Web Map using FastAPI & Leaflet
  • How to Create a Simple WebGIS with FastAPI, PostGIS, and Leaflet.js
  • Graph Coloring: How Many Colors Do You Need?

Archives

  • May 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • September 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • September 2023
  • August 2023
  • April 2023

Categories

  • Courses
  • Events
  • GIS
  • Linux
  • News
  • programming
  • python
  • Tutorials
  • Videos
  • May 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • September 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • September 2023
  • August 2023
  • April 2023
  • Courses
  • Events
  • GIS
  • Linux
  • News
  • programming
  • python
  • Tutorials
  • Videos

Araz Shahkarami

I’m a software enthusiast with a deep love for crafting robust and efficient solutions. My journey into the world of programming began several years ago when I was introduced to the world of code. Since then, I’ve been on an exhilarating ride of learning, problem-solving, and continuous improvement.

© 2025 Araz Shah | Powered by Minimalist Blog WordPress Theme