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 withlongitudeandlatitudeTableResult- Matrix result withdurations,distances,sources,destinationsRouteResult- Route withdistance,duration,waypointsNearestResult- Nearest points withwaypointsMatchResult- Matched route withmatchings,tracepointsTripResult- Optimized trip withdistance,duration,waypoints
Development
Running Tests
uv sync
uv run pytest
Running Examples
uv run python examples/hello_libosrm.py
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file libosrmpy-0.1.1-cp312-cp312-manylinux_2_39_x86_64.whl.
File metadata
- Download URL: libosrmpy-0.1.1-cp312-cp312-manylinux_2_39_x86_64.whl
- Upload date:
- Size: 1.3 MB
- Tags: CPython 3.12, manylinux: glibc 2.39+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
22842c186dd68fdcc761ed02b4ea77f59c9bbc0324f41fec36759b32d3a22491
|
|
| MD5 |
409344be0032ce8fd5792b6445c78119
|
|
| BLAKE2b-256 |
c5f0712a5c06e1211cdcf59573890a579415a58a5b2e7a4ec24b554304453cfd
|
File details
Details for the file libosrmpy-0.1.1-cp312-cp312-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: libosrmpy-0.1.1-cp312-cp312-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 1.3 MB
- Tags: CPython 3.12, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
38d0f50116ab331a6906756c02c1373a5c7185237e6cce37b35d2e2892d068a9
|
|
| MD5 |
89a28df26678b9ae83a4f221aa011d63
|
|
| BLAKE2b-256 |
08ee1235772f657d637c19e433adbe3761540722cd068460a7d2385b543b0e60
|
File details
Details for the file libosrmpy-0.1.1-cp312-cp312-macosx_26_0_arm64.whl.
File metadata
- Download URL: libosrmpy-0.1.1-cp312-cp312-macosx_26_0_arm64.whl
- Upload date:
- Size: 1.3 MB
- Tags: CPython 3.12, macOS 26.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b32fbf25aff1b920d53d758286a0371d2072f314a9075c05e87a9bad9b514810
|
|
| MD5 |
4649f690e2d2d71a064619ca1bd1e4d2
|
|
| BLAKE2b-256 |
8605d40e860ae1d862e1433be1679f8e3ddbf8c523ea3295de169bfabbd905a3
|