Network Analysis
Last updated on 2025-12-05 | Edit this page
Overview
Questions
- How do we download and visualize road networks with OSM data?
- What is a graph network and how is it represented in Python?
- How can we compute shortest paths and network distances?
Objectives
- Learn how to retrieve OpenStreetMap road data using OSMnx
- Convert road networks into graphs for routing and analysis
- Visualize networks and shortest paths on a map
- Compute route distances and travel time across a network
Overview
This tutorial provides a practical introduction to performing road
network analysis using Python, focusing on analyzing road networks in a
specified area (e.g., West Lafayette, Indiana) to study food deserts. It
uses libraries such as networkx, osmnx,
folium, pandas, geopandas, and
matplotlib to fetch, visualize, and analyze road networks,
compute centroid nodes, and calculate the shortest path based on travel
time. The tutorial also applies this analysis to food accessibility data
in Indiana.
The coordinates of grocery stores in Indiana were fetched using OpenStreetMap (OSM). Network analysis is used to calculate distances and times from grocery store locations to the center of areas of interest, considering factors such as the number of supermarkets, income, and vehicle accessibility. Distances are classified as good (around 1 mile) or low-accessible (over 5 miles), depending on rural or urban settings. Low Income and Low Access maps were created for each census tract in Indiana and compared to the USDA food desert dataset.
Why Network Analysis for Food Deserts?
- Mapping Accessibility: Models connections between grocery stores and transportation systems to identify areas with limited healthy food access due to distance or lack of transportation.
- Area Development: Helps improve accessibility and quality of life in underserved regions.
- Promotes Equity: Highlights disparities to create solutions for equitable access to nutritious food.
- Optimization of Resources: Ensures equal distribution of resources for all individuals.
Environment Setup
Libraries imported for this tutorial: - osmnx: Fetches
and processes OpenStreetMap road network data. - networkx:
Performs graph-based computations, such as shortest path calculations. -
folium: Enables interactive map visualizations. -
geopandas and shapely: Handle geospatial data
and geometry operations. - matplotlib: Generates static
plots, including network visualization. - geopy: Calculates
geodesic distances for spatial analysis.
Data Acquisition
The road network for West Lafayette, Indiana, is fetched using
ox.graph.from_place("West Lafayette, Indiana", network_type="drive"),
retrieving the drivable road network from OpenStreetMap as a graph
(nodes as intersections, edges as road segments). The graph can be saved
as a GraphML file (e.g.,
westlafayette_indiana_network.graphml) using
ox.save_graphml to avoid redundant downloads. This can be
adapted for any U.S. area with a single line of code.
Applications
- Urban Planning: Analyzing road connectivity and accessibility in cities.
- Transportation Studies: Optimizing routes based on travel time or distance.
- Geospatial Analysis: Studying spatial relationships in infrastructure networks.
- Emergency Response: Identifying the fastest routes for first responders.
Introduction
Network analysis allows us to study movement, connectivity, and accessibility across geographic space. Roads, sidewalks, rivers, power lines, and transit systems can be modeled as graphs, where intersections are nodes and paths are edges.
This lesson demonstrates how to:
- Download a road network using OSMnx
- Convert it into a graph using NetworkX
- Visualize the network
- Run shortest path routing between two locations
1. Install Required Libraries
2. Import Libraries
3. Download a Road Network from OpenStreetMap
Visualize network:
4. Convert the Graph to Nodes and Edges GeoDataFrames
Plot edges alone:
5. Find Shortest Route Between Two Points
Choose two coordinates manually or by clicking on a map.
PYTHON
orig = ox.distance.nearest_nodes(G, -86.9145, 40.4253) # lon, lat
dest = ox.distance.nearest_nodes(G, -86.9079, 40.4268)
Calculate shortest path:
Plot route:
Challenge
Challenge 1 — Try Your Own Route
- Pick any two points in a city of your choice.
- Compute and visualize the shortest path between them.
Math
A network is represented as a graph:
G = (V,E)
Where:
- V = set of nodes (intersections)
- E = edges (roads)
Shortest path = minimum weighted path across E.
OSMnx simplifies downloading and converting OSM road networks
Graphs model movement and connectivity in space
NetworkX allows shortest path and routing analysis
Visualization helps interpret accessibility patterns