Skip to main content

Python bindings for libosrm using pybind11

Project description

libosrmpy

Python bindings for OSRM (Open Source Routing Machine).

Installation

Using uv (Recommended)

uv is a fast Python package manager.

# Add to your project
uv add libosrmpy

# Or install directly
uv pip install libosrmpy

Using pip

pip install libosrmpy

Note: Pre-built wheels are available for:

  • Linux (x86_64, manylinux)
  • macOS (arm64, x86_64)

The package manager automatically downloads the correct wheel for your platform.

Using Pre-built Wheel Directly

If you have a wheel file, you can install it directly:

# Using uv
uv pip install libosrmpy-0.1.0-cp312-cp312-manylinux_2_17_x86_64.whl

# Using pip
pip install libosrmpy-0.1.0-cp312-cp312-manylinux_2_17_x86_64.whl

Build from Source

Building from source requires the OSRM C++ library and its dependencies.

Linux (Docker - Recommended)

The easiest way to build on Linux is using the provided Docker environment:

# Build the Docker image
docker build -t libosrmpy-builder .

# Start a container
docker run -d --name libosrmpy-dev -v $(pwd):/app libosrmpy-builder tail -f /dev/null

# Build the wheel inside the container
docker exec -it libosrmpy-dev bash -c "cd /app && uv sync && uv build && uvx auditwheel repair dist/*.whl -w wheelhouse/"

The wheel will be created in the wheelhouse/ directory.

macOS

Prerequisites (using Homebrew):

# Install dependencies
brew install cmake boost tbb

# Build and install OSRM
git clone --depth 1 --branch v6.0.0 https://github.com/Project-OSRM/osrm-backend.git
cd osrm-backend && mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release -DENABLE_NODE_BINDINGS=OFF
make -j$(sysctl -n hw.ncpu)
sudo make install
cd ../..

# Build the wheel
uv build

Usage

from libosrmpy import Engine, Algorithm
from libosrmpy.schema import Coordinate

# Initialize engine with map data
engine = Engine("/path/to/map.osrm", algorithm=Algorithm.MLD)

# Define coordinates (longitude, latitude)
coords = [
    Coordinate(longitude=126.9780, latitude=37.5665),
    Coordinate(longitude=126.9784, latitude=37.5660),
]

# Calculate distance/duration matrix
table_result = engine.table(coords)
print(table_result.durations)  # Duration matrix in seconds
print(table_result.distances)  # Distance matrix in meters

# Calculate route
route = engine.route(coords)
print(f"Distance: {route.distance}m, Duration: {route.duration}s")

# Find nearest point on road network
nearest = engine.nearest(coords[0])
print(nearest.waypoints[0].location)

# Match GPS trace to road network
match_result = engine.match(coords)
print(match_result.matchings[0].confidence)

# Calculate optimal trip (TSP solver)
trip = engine.trip(coords, roundtrip=True)
print(f"Optimized distance: {trip.distance}m")

Preparing Map Data

OSRM requires pre-processed map data. Here's how to prepare it using Docker:

1. Download OSM Data

# Example: Download South Korea OSM data from Geofabrik
curl -L -o korea.osm.pbf http://download.geofabrik.de/asia/south-korea-latest.osm.pbf

You can find regional OSM data at Geofabrik.

2. Process OSRM Data

# Extract
docker run -t --rm -v $(pwd):/data ghcr.io/project-osrm/osrm-backend:latest \
    osrm-extract -p /opt/car.lua /data/korea.osm.pbf

# Partition
docker run -t --rm -v $(pwd):/data ghcr.io/project-osrm/osrm-backend:latest \
    osrm-partition /data/korea.osrm

# Customize
docker run -t --rm -v $(pwd):/data ghcr.io/project-osrm/osrm-backend:latest \
    osrm-customize /data/korea.osrm

Routing Profiles

  • /opt/car.lua - Car routing
  • /opt/bicycle.lua - Bicycle routing
  • /opt/foot.lua - Pedestrian routing

API Reference

Engine Methods

Method Description
table(coordinates, sources?, destinations?) Calculate duration/distance matrix
route(coordinates) Calculate route between coordinates
nearest(coordinate, number_of_results=1) Find nearest road network point(s)
match(coordinates, timestamps?, radiuses?) Match GPS trace to road network
trip(coordinates, roundtrip=True, source?, destination?) Calculate optimal trip (TSP)

Schema Classes

  • Coordinate - Input coordinate with longitude and latitude
  • TableResult - Matrix result with durations, distances, sources, destinations
  • RouteResult - Route with distance, duration, waypoints
  • NearestResult - Nearest points with waypoints
  • MatchResult - Matched route with matchings, tracepoints
  • TripResult - Optimized trip with distance, duration, waypoints

Development

Running Tests

uv sync
uv run pytest

Running Examples

uv run python examples/hello_libosrm.py

License

MIT License

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

libosrmpy-0.1.1-cp312-cp312-manylinux_2_39_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.39+ x86-64

libosrmpy-0.1.1-cp312-cp312-manylinux_2_28_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.28+ x86-64

libosrmpy-0.1.1-cp312-cp312-macosx_26_0_arm64.whl (1.3 MB view details)

Uploaded CPython 3.12macOS 26.0+ ARM64

File details

Details for the file libosrmpy-0.1.1-cp312-cp312-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for libosrmpy-0.1.1-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 22842c186dd68fdcc761ed02b4ea77f59c9bbc0324f41fec36759b32d3a22491
MD5 409344be0032ce8fd5792b6445c78119
BLAKE2b-256 c5f0712a5c06e1211cdcf59573890a579415a58a5b2e7a4ec24b554304453cfd

See more details on using hashes here.

File details

Details for the file libosrmpy-0.1.1-cp312-cp312-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for libosrmpy-0.1.1-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 38d0f50116ab331a6906756c02c1373a5c7185237e6cce37b35d2e2892d068a9
MD5 89a28df26678b9ae83a4f221aa011d63
BLAKE2b-256 08ee1235772f657d637c19e433adbe3761540722cd068460a7d2385b543b0e60

See more details on using hashes here.

File details

Details for the file libosrmpy-0.1.1-cp312-cp312-macosx_26_0_arm64.whl.

File metadata

File hashes

Hashes for libosrmpy-0.1.1-cp312-cp312-macosx_26_0_arm64.whl
Algorithm Hash digest
SHA256 b32fbf25aff1b920d53d758286a0371d2072f314a9075c05e87a9bad9b514810
MD5 4649f690e2d2d71a064619ca1bd1e4d2
BLAKE2b-256 8605d40e860ae1d862e1433be1679f8e3ddbf8c523ea3295de169bfabbd905a3

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