Skip to main content

FastAPI gateway for OSRM routing engine

Project description

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.

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

# Build and start services
docker compose up -d --build

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

osrm_api_gateway-0.1.0.tar.gz (1.7 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

osrm_api_gateway-0.1.0-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

File details

Details for the file osrm_api_gateway-0.1.0.tar.gz.

File metadata

  • Download URL: osrm_api_gateway-0.1.0.tar.gz
  • Upload date:
  • Size: 1.7 MB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for osrm_api_gateway-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3f37e0eb10a17a571bb669ccc2411b48055cd19475e595b909a24413562e9349
MD5 a9323da47f18e3a5db5aab1551dc9aa4
BLAKE2b-256 deee0e826da135e2c061b24dc16385c8a2b350156c959112b0c93129e736a8c8

See more details on using hashes here.

File details

Details for the file osrm_api_gateway-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: osrm_api_gateway-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.5 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for osrm_api_gateway-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f10b25787aae82b19d2f5f4094a7f7b0ddccf884ed4b0cddaa695e617c3e45ce
MD5 9c2d9eaae4c792a62458196c70248083
BLAKE2b-256 19468f5bfecee7ec18d3e7bb6d59e43a745361c61c5f8eb39f69ce24dcb580f6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page