πŸ—ΊοΈ Open Source Routing

py-osrm-backend

A Python implementation of OSRM backend core functionality. Build routing applications with OpenStreetMap data.

v0.1.1 Latest Release
Capabilities

Features

Everything you need to build routing applications in Python

πŸ—ΊοΈ

OSM Parsing

Parse OpenStreetMap data in both XML (.osm) and PBF formats to extract nodes and ways for routing.

πŸ“Š

Graph Building

Build a routable graph with haversine distance weights automatically calculated from coordinates.

🧭

Dijkstra Routing

Find shortest paths using Dijkstra's algorithm with efficient priority queue implementation.

⚑

Contraction Hierarchies

Speed up queries with Contraction Hierarchies preprocessing for faster route computation.

🌐

REST API

Flask-based REST API compatible with OSRM-like endpoints for easy integration.

🐍

Pure Python

No complex dependencies, easy to install and extend. Works with Python 3.8+.

Setup

Installation

Get started in seconds with pip

Install from PyPI:

pip install py-osrm-backend

πŸ“¦ Dependencies

py-osrm-backend requires only two dependencies:

  • flask - For the REST API server
  • osmium - For parsing PBF files

πŸ”§ Development

For development, install with extras:

pip install py-osrm-backend[dev]
Tutorial

Quick Start

Build your first routing application in minutes

example.py
# 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}")

1️⃣ Load OSM Data

Use GraphBuilder to parse OSM files and build a routable graph. Supports both .osm (XML) and .pbf formats.

2️⃣ Create Engine

Initialize DijkstraEngine with your graph. For faster queries on large graphs, use CHEngine instead.

3️⃣ Compute Routes

Call shortest_path() with start and end node IDs. Returns the total distance in kilometers and the path as a list of node IDs.

Overview

Architecture

Modular design for flexibility and extensibility

πŸ“
OSM Data
.osm / .pbf files
OpenStreetMap
β†’
πŸ”§
Extractor
Parse & Build
Graph Structure
β†’
πŸ“Š
Graph
Nodes & Edges
Weighted Network
β†’
🧭
Engine
Dijkstra / CH
Route Computation
β†’
🌐
API Server
Flask REST
HTTP Endpoints

πŸ“¦ structures

Core data structures including Graph, Node, and Edge classes.

πŸ”§ extractor

OSM parsing and graph building utilities. Handles XML and PBF formats.

🧭 engine

Routing algorithms: Dijkstra and Contraction Hierarchies implementations.

REST API

HTTP Endpoints

OSRM-compatible REST API for web applications

run_server.py
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)

πŸ“ Route Endpoint

Get a route between coordinates:

GET /route/v1/driving/{lon1},{lat1};{lon2},{lat2}

Returns distance, duration, and geometry in GeoJSON format.