Skip to content

Your first network

This builds the road network of Monaco from scratch, then queries it, routes on it and draws it. It takes a few minutes. You need duckOSM installed: pip install duckosm.

1. Choose your area

Search for Monaco on openstreetmap.org and click a result whose border is drawn on the map. The page address ends in relation/<id>: that number is the area's OSM id. Monaco has two: the country, relation/1124039, whose border reaches far out to sea (its territorial waters), and its land area, relation/2220322. For roads we want the land: 2220322.

2. Download the map data

OpenStreetMap data comes as .osm.pbf files. Geofabrik offers free extracts of every continent, country and many regions, updated daily. Monaco has its own:

curl -LO https://download.geofabrik.de/europe/monaco-latest.osm.pbf       # 0.7 MB

For your own area, download the smallest region that contains it. duckosm get "Monaco" does steps 1 to 3 in one command (Prepare an area).

3. Make the boundary file

duckosm boundary --osm-id 2220322 --pbf monaco-latest.osm.pbf
wrote monaco.geojson: Monaco (admin level 8, OSM relation 2220322, 2.39 km²), from PBF monaco-latest.osm.pbf

4. Build the network

duckosm build --pbf monaco-latest.osm.pbf -b monaco.geojson \
    -m driving -m walking -m cycling

A few seconds later you have monaco.duckdb: one file holding a driving, a walking and a cycling network, each in its own schema, plus the base-map layers (water, buildings, POIs…). Roads that cross the border are kept whole.

5. Query it

The database is a normal DuckDB file. Which kinds of road make up Monaco's driving network?

import duckdb

con = duckdb.connect("monaco.duckdb", read_only=True)
con.sql("""SELECT highway, count(*) AS edges, round(sum(length_m) / 1000, 1) AS km
           FROM driving.edges GROUP BY highway ORDER BY km DESC LIMIT 5""").show()
highway edges km
residential 919 31.7
service 855 23.7
secondary 429 14.9
tertiary 387 12.5
primary 197 11.2

Your numbers can be a little different: OSM changes every day.

6. Find a route

Routes go from one edge (a directed piece of road) to another, and only take legal turns:

from duckosm import route

q = "SELECT min(edge_id) FROM driving.edges WHERE name = ?"
a = con.execute(q, ["Boulevard du Larvotto"]).fetchone()[0]
b = con.execute(q, ["Avenue Princesse Grace"]).fetchone()[0]

r = route(con, a, b)
r["time_s"], r["length_m"]          # (114.1, 1513.0): about 2 minutes, 1.5 km
r["edges"]                          # the 29 edge_ids along the way

Every edge_id stays the same when you rebuild, cut out a smaller area, or export: see Stable edge ids.

7. Draw it

duckosm viz monaco.duckdb -m driving       # -> reports/monaco_driving_network.html

Open the file in a browser. This is that file, live: zoom, hover a road, click one to see its edge_id, toggle road classes in the legend:

In Python or Jupyter, duckosm.build gives a duckosm.Network. Its methods give the tables and the map:

import duckosm

net = duckosm.build(pbf="monaco.osm.pbf", boundary="monaco.geojson")   # or duckosm.open("monaco.duckdb")
edges = net.edges()            # the driving edges as a GeoDataFrame (needs pip install "duckosm[gis]")
net.map()                      # the live map in Jupyter (needs pip install "duckosm[viz]")

The notebooks show more.

Next

To Read
build your own area Prepare an area, Build a network
fix the OSM data Edit OSM data
route and query Route, Query the database
export to other tools Export