A Python implementation of OSRM backend core functionality. Build routing applications with OpenStreetMap data.
Everything you need to build routing applications in Python
Parse OpenStreetMap data in both XML (.osm) and PBF formats to extract nodes and ways for routing.
Build a routable graph with haversine distance weights automatically calculated from coordinates.
Find shortest paths using Dijkstra's algorithm with efficient priority queue implementation.
Speed up queries with Contraction Hierarchies preprocessing for faster route computation.
Flask-based REST API compatible with OSRM-like endpoints for easy integration.
No complex dependencies, easy to install and extend. Works with Python 3.8+.
Get started in seconds with pip
Install from PyPI:
py-osrm-backend requires only two dependencies:
flask - For the REST API server
osmium - For parsing PBF files
For development, install with extras:
pip install py-osrm-backend[dev]
Build your first routing application in minutes
# Import the library
from osrm.extractor.graph_builder import GraphBuilder
from osrm.engine.dijkstra import DijkstraEngine
# Build graph from OSM file
builder = GraphBuilder()
graph = builder.build_graph("your_map.osm")
# Create routing engine
engine = DijkstraEngine(graph)
# Find shortest path
distance, path = engine.shortest_path(start_id, end_id)
print(f"Distance: {distance} km")
print(f"Path: {path}")
Use GraphBuilder to parse OSM files and build
a routable graph. Supports both .osm (XML) and .pbf formats.
Initialize DijkstraEngine with your graph. For
faster queries on large graphs, use CHEngine instead.
Call shortest_path() with start and end node
IDs. Returns the total distance in kilometers and the path as a list of node IDs.
Modular design for flexibility and extensibility
Core data structures including Graph, Node,
and Edge classes.
OSM parsing and graph building utilities. Handles XML and PBF formats.
Routing algorithms: Dijkstra and Contraction Hierarchies implementations.
OSRM-compatible REST API for web applications
from osrm.server.app import create_app
from osrm.extractor.graph_builder import GraphBuilder
# Build graph
builder = GraphBuilder()
graph = builder.build_graph("map.osm")
# Create and run server
app = create_app(graph)
app.run(host="0.0.0.0", port=5000)
Get a route between coordinates:
GET /route/v1/driving/{lon1},{lat1};{lon2},{lat2}
Returns distance, duration, and geometry in GeoJSON format.