Skip to main content

OSRM Backend Microservice

English | Español | Français

High-performance routing and map-matching microservice for Costa Rica.

Setup Instructions

This project uses a Local Build & Bundled Transfer workflow to support deployment to remote Docker hosts while processing data locally on macOS.

1. Prerequisites

  • Docker Desktop (macOS)
  • Remote Docker Host (e.g., Linux VM at 10.211.55.28)
  • make

2. Data Acquisition & Local Processing

Extract and process the Costa Rica OSM data locally. This process bundles the data into your local ./data folder using a Docker-based "No-Mount" builder.

# Download the latest Costa Rica map data
make download-data

# Process the data locally for a specific profile (car, bicycle, foot)
# Defaults to car if PROFILE is omitted
make process-osrm PROFILE=car

3. Remote Deployment

Deploy the API and the OSRM engine to the remote host. The processed data is bundled directly from the builder image into the OSRM runtime image via a multi-stage Dockerfile.osrm.

ghcr.io/project-osrm/osrm-backend is currently amd64 only. Confirm your active Docker daemon architecture before starting services.

# Target the remote host
export DOCKER_HOST=tcp://10.211.55.28:2375

# Check Docker target and architecture
make compose-doctor

# Build and start services with safe sequencing + health checks
make compose-up

# Tail service logs
make compose-logs

# Stop services
make compose-down

Avoid running docker compose down & docker compose up --build; & backgrounds the first command and can trigger race conditions.

Core Services

The application encapsulates complex routing logic into several key services located in app/services/:

1. OSRM Client (osrm_client.py)

An asynchronous HTTP client that interacts directly with the C++ OSRM backend. It formats queries and standardizes responses. Example Use Case: Fetching the exact geometry and driving instructions for a trip between a warehouse and multiple delivery points.

2. Graph Builder (graph_builder.py)

Transforms raw OSRM distance and duration matrices into directed NetworkX graphs. Example Use Case: Generating a mathematical representation of the road network to feed into advanced optimization algorithms (like custom TSP solvers) or to identify isolated nodes in the delivery network.

3. VRP Service (vrp_service.py)

A comprehensive Vehicle Routing Problem (VRP) solver. It implements a Location-Allocation strategy, assigning delivery stops to the nearest available warehouse (depot) and generating optimized delivery sequences. Example Use Case: A logistics company wants to distribute 500 daily packages across 5 drivers starting from 2 different warehouses, ensuring each driver takes the most optimal cluster of stops.

Client Application Usage Examples

Here are some examples of how a client application can interact with the FastAPI microservice using Python's requests library:

import requests

BASE_URL = "http://localhost:8000"

# 1. Route Plotting
route_payload = {
    "origin": {"lat": 9.9281, "lon": -84.0907},
    "destination": {"lat": 9.9333, "lon": -84.0833},
    "alternatives": True
}
route_res = requests.post(f"{BASE_URL}/route", json=route_payload)

# 2. Traveling Salesperson Problem (TSP)
tsp_payload = {
    "locations": [
        {"lat": 9.9281, "lon": -84.0907},
        {"lat": 9.9333, "lon": -84.0833},
        {"lat": 9.9981, "lon": -84.1107}
    ]
}
tsp_res = requests.post(f"{BASE_URL}/trip", json=tsp_payload)

# 3. Clustering (Location Allocation)
cluster_payload = {
    "depots": [{"id": "D1", "lat": 9.9281, "lon": -84.0907}],
    "locations": [
        {"id": "L1", "lat": 9.9333, "lon": -84.0833},
        {"id": "L2", "lat": 9.9981, "lon": -84.1107}
    ],
    "num_vehicles": 2
}
cluster_res = requests.post(f"{BASE_URL}/vrp/allocate", json=cluster_payload)

# 4. Vehicle Routing Problem (VRP)
vrp_payload = {
    "depots": [{"id": "D1", "lat": 9.9281, "lon": -84.0907}],
    "locations": [
        {"id": "L1", "lat": 9.9333, "lon": -84.0833},
        {"id": "L2", "lat": 9.9981, "lon": -84.1107}
    ],
    "num_vehicles": 2
}
vrp_res = requests.post(f"{BASE_URL}/vrp", json=vrp_payload)

Visualization Tools

The project includes Python tools to visualize and compare routes:

  • visualize_routes.py: Fetches and plots primary and alternate routes for a trip.
  • compare_tsp.py: Compares a provided sequence of stops (Actual) against a TSP-optimized round-trip (Optimized).
  • matrix_example.py: Demonstrates how to generate a distance and duration table (matrix) between multiple origins and destinations.
  • simple_id_example.py: A comprehensive VRP demonstrator that simulates 10 vehicles across multiple depots and generates an interactive Folium map.

Usage:

# Run the distance/duration matrix example
uv run examples/routing/matrix_example.py

# Run the 10-vehicle VRP simulation
uv run examples/clustering/simple_id_example.py

# Compare actual vs optimized sequences
uv run compare_tsp.py

Maps are saved as interactive HTML files (map.html, comparison_map.html).

API Documentation

Interactive documentation is available once the service is running:

  • Swagger UI: http://localhost:8000/docs
  • Redoc: http://localhost:8000/redoc

For a detailed developer guide, see:

Components

  • OSRM Engine: C++ routing powerhouse running the MLD algorithm.
  • FastAPI Gateway: Asynchronous Python API providing specialized endpoints for map matching, graph generation, and Vehicle Routing Problems (VRP).
  • VRP Solver: Location-Allocation engine for multi-vehicle clustering with support for custom IDs and capacity-based route splitting.
  • NetworkX Integration: Transparently converts matrix outputs into serializable graphs.

Metadata

Release files for osrm-api-gateway 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for osrm-api-gateway 0.2.1
File Size Uploaded Provenance Actions
osrm_api_gateway-0.2.1.tar.gz 17.5 kB Without provenance Details for osrm_api_gateway-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for osrm-api-gateway 0.2.1
File Interpreter ABI Platform Actions
osrm_api_gateway-0.2.1-py3-none-any.whl Python 3 none any Details for osrm_api_gateway-0.2.1-py3-none-any.whl

Total release size: 32.2 kB

Release files / osrm_api_gateway-0.2.1.tar.gz

Download URL osrm_api_gateway-0.2.1.tar.gz
Size 17.5 kB
Tags Source
SHA-256 checksum
How to use checksums
4efc976c6def7347de0ed51dcf3c6b06c9f70c259a69f1df2e822411e4665be6
BLAKE2b-256 checksum
How to use checksums
2257225415fc1edcdcfb9da94e490a5603f589bc165d259f9ccf8be16bf65615
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / osrm_api_gateway-0.2.1-py3-none-any.whl

Download URL osrm_api_gateway-0.2.1-py3-none-any.whl
Size 14.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8c1d22fd48492bc01707bd34cf3f746848f6a4c13c80f4227d6d64b7c667cca2
BLAKE2b-256 checksum
How to use checksums
e9b537c1a646adaca4dea63d27a9398a22032be9ab9fc3cf3019baa2618c9543
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for osrm-api-gateway 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page