OSMnx Street Networks — Core Concepts

OSMnx is a Python library that downloads geospatial data from OpenStreetMap and models street networks as NetworkX graphs. It handles the entire pipeline — query, download, clean, project, analyze, and visualize — in a few lines of code.

Downloading a network

import osmnx as ox

# By place name
G = ox.graph_from_place("Manhattan, New York, USA", network_type="drive")

# By point and radius (meters)
G = ox.graph_from_point((40.748, -73.985), dist=1000, network_type="walk")

# By bounding box
G = ox.graph_from_bbox(40.80, 40.70, -73.90, -74.00, network_type="bike")

The network_type parameter filters edges: "drive" excludes footpaths, "walk" includes pedestrian paths, "bike" includes cycleways, and "all" keeps everything.

Graph structure

The result is a MultiDiGraph — a directed graph that allows multiple edges between nodes (for one-way streets, parallel roads):

print(G.number_of_nodes())  # intersections + dead-ends
print(G.number_of_edges())  # road segments

# Node attributes
node_data = G.nodes[42431337]
# {'y': 40.748, 'x': -73.985, 'street_count': 4}

# Edge attributes
edge_data = G.edges[(u, v, 0)]
# {'length': 120.5, 'name': 'Broadway', 'highway': 'primary', 'maxspeed': '25 mph'}

Shortest-path routing

orig = ox.nearest_nodes(G, -73.990, 40.750)
dest = ox.nearest_nodes(G, -73.970, 40.730)

route = ox.shortest_path(G, orig, dest, weight="length")
# Returns a list of node IDs

# Visualize
ox.plot_graph_route(G, route, route_linewidth=3, node_size=0)

You can weight routes by "length" (shortest distance), "travel_time" (fastest with speed limits), or any custom attribute.

Adding travel time

G = ox.add_edge_speeds(G)        # infer from maxspeed tags
G = ox.add_edge_travel_times(G)  # length / speed

fast_route = ox.shortest_path(G, orig, dest, weight="travel_time")

Network statistics

OSMnx computes standard graph metrics for urban analysis:

stats = ox.basic_stats(G)
print(stats["street_density_km"])     # km of road per sq km
print(stats["intersection_density_km"])  # intersections per sq km
print(stats["circuity_avg"])          # avg route circuity (1.0 = straight line)

Higher intersection density and lower circuity indicate more walkable, grid-like neighborhoods.

Visualization

# Basic plot
fig, ax = ox.plot_graph(G, node_size=5, edge_linewidth=0.5)

# Color edges by type
ec = ox.plot.get_edge_colors_by_attr(G, "length", cmap="plasma")
fig, ax = ox.plot_graph(G, edge_color=ec, node_size=0)

OSMnx also exports to GeoDataFrames for integration with GeoPandas plotting or web maps.

Beyond streets: buildings and amenities

OSMnx downloads more than roads:

# Building footprints
buildings = ox.features_from_place("Montmartre, Paris", tags={"building": True})

# Parks
parks = ox.features_from_place("Brooklyn, NY", tags={"leisure": "park"})

# Cafés
cafes = ox.features_from_point((48.886, 2.341), tags={"amenity": "cafe"}, dist=500)

All results are GeoPandas GeoDataFrames.

Common misconception

Many people think OSMnx only works for car routing. In practice, pedestrian and bicycle networks are equally well supported, and the library’s real strength is network analysis — measuring connectivity, centrality, and structure — not just point-to-point navigation.

The one thing to remember: OSMnx converts the free OpenStreetMap database into a routable, analyzable Python graph, making street-network science accessible with a single function call.

pythonosmnxstreet-networksgeospatial

See Also

  • Python Adaptive Learning Systems How Python builds learning apps that adjust to each student like a personal tutor who knows exactly what you need next.
  • Python Airflow Learn Airflow as a timetable manager that makes sure data tasks run in the right order every day.
  • Python Altair Learn Altair through the idea of drawing charts by describing rules, not by hand-placing every visual element.
  • Python Automated Grading How Python grades homework and exams automatically, from simple answer keys to understanding written essays.
  • Python Batch Vs Stream Processing Batch processing is like doing laundry once a week; stream processing is like a self-cleaning shirt that cleans itself constantly.