Python API
The Python bindings provide access to OSRM's routing services through the osrm package. Install with pip install osrm-bindings.
OSRM
The OSRM class is the main entry point. It requires a .osrm.* dataset prepared by the OSRM toolchain.
import osrm
# From file
engine = osrm.OSRM("path/to/data.osrm")
# With keyword arguments
engine = osrm.OSRM(
storage_config="path/to/data.osrm",
algorithm="CH", # or "MLD"
use_shared_memory=False,
max_locations_trip=3,
max_locations_viaroute=3,
max_locations_distance_table=3,
max_locations_map_matching=3,
max_results_nearest=1,
max_alternatives=1,
default_radius="unlimited",
)
# Using shared memory (requires osrm-datastore)
engine = osrm.OSRM(use_shared_memory=True)Parameters
storage_configstr- Path to the.osrmdataset.algorithmstr- Routing algorithm:"CH"or"MLD". Default:"CH".use_shared_memorybool- Connect to shared memory datastore. Default:True.dataset_namestr- Named shared memory dataset (requiresosrm-datastore --dataset_name).memory_filestr- Deprecated. Equivalent touse_mmap=True.use_mmapbool- Memory-map files instead of loading into RAM.max_locations_tripint- Max locations in trip queries.max_locations_viarouteint- Max locations in route queries.max_locations_distance_tableint- Max locations in table queries.max_locations_map_matchingint- Max locations in match queries.max_results_nearestint- Max results in nearest queries.max_alternativesint- Max alternative routes.default_radiusfloat | "unlimited"- Default search radius in meters.
Services
All service methods take a parameters object and return a dict-like Object:
result = engine.Route(route_params)
print(result["routes"])
print(result["waypoints"])Route
Finds the fastest route between two or more coordinates.
params = osrm.RouteParameters(
coordinates=[(7.41337, 43.72956), (7.41546, 43.73077)],
steps=True,
alternatives=2,
annotations=["speed", "duration"],
geometries="geojson",
overview="full",
)
result = engine.Route(params)RouteParameters
Inherits all BaseParameters.
stepsbool- Return route steps for each leg. Default:False.alternativesint- Number of alternative routes to search for. Default:0.annotationslist[str]- Additional metadata:"none","duration","nodes","distance","weight","datasources","speed","all". Default:[].geometriesstr- Geometry format:"polyline","polyline6","geojson". Default:"polyline".overviewstr- Overview geometry:"simplified","full","false". Default:"simplified".continue_straightbool | None- Force route to continue straight at waypoints.waypointslist[int]- Indices of coordinates to treat as waypoints. Must include first and last.
Table
Computes duration/distance matrices between coordinates.
params = osrm.TableParameters(
coordinates=[(7.41337, 43.72956), (7.41546, 43.73077), (7.41862, 43.73216)],
sources=[0],
destinations=[1, 2],
annotations=["duration", "distance"],
)
result = engine.Table(params)TableParameters
Inherits all BaseParameters.
sourceslist[int]- Indices of source coordinates. Default: all.destinationslist[int]- Indices of destination coordinates. Default: all.annotationslist[str]-"duration","distance","all". Default:["duration"].fallback_speedfloat- Speed for crow-flies fallback when no route found.fallback_coordinate_typestr-"input"or"snapped".scale_factorfloat- Scales duration values. Default:1.0.
Nearest
Finds the nearest street segment for a coordinate.
params = osrm.NearestParameters(
coordinates=[(7.41337, 43.72956)],
number_of_results=3,
)
result = engine.Nearest(params)NearestParameters
Inherits all BaseParameters.
number_of_resultsint- Number of nearest segments to return. Default:1.
Match
Snaps noisy GPS traces to the road network.
params = osrm.MatchParameters(
coordinates=[(7.41337, 43.72956), (7.41546, 43.73077), (7.41862, 43.73216)],
timestamps=[1424684612, 1424684616, 1424684620],
radiuses=[5.0, 5.0, 5.0],
annotations=["speed"],
geometries="geojson",
)
result = engine.Match(params)MatchParameters
Inherits all RouteParameters and BaseParameters.
timestampslist[int]- UNIX timestamps for each coordinate.gapsstr- Gap handling:"split"or"ignore". Default:"split".tidybool- Remove duplicates. Default:False.waypointslist[int]- Indices of coordinates to treat as waypoints.
Trip
Solves the Traveling Salesman Problem for the given coordinates.
params = osrm.TripParameters(
coordinates=[(7.41337, 43.72956), (7.41546, 43.73077), (7.41862, 43.73216)],
source="first",
destination="last",
roundtrip=True,
annotations=["duration"],
geometries="geojson",
)
result = engine.Trip(params)TripParameters
Inherits all RouteParameters and BaseParameters.
sourcestr-"any"or"first". Default:"any".destinationstr-"any"or"last". Default:"any".roundtripbool- Return to first location. Default:True.
Tile
Generates vector tiles with internal routing graph data.
params = osrm.TileParameters(x=17059, y=11948, z=15)
result = engine.Tile(params) # returns bytesTileParameters
xint- Tile x coordinate.yint- Tile y coordinate.zint- Tile zoom level.
BaseParameters
Shared parameters inherited by Nearest, Table, Route, Match, and Trip.
coordinateslist[tuple[float, float]]- List of(longitude, latitude)pairs.hintslist[str | None]- Base64-encoded hints from previous requests.radiuseslist[float | None]- Search radius per coordinate in meters.Nonefor unlimited.bearingslist[tuple[int, int] | None]-(bearing, range)pairs in degrees.Nonefor unrestricted.approacheslist[str | None]-"curb","unrestricted", orNone.generate_hintsbool- Include hints in response. Default:True.excludelist[str]- Road classes to avoid (e.g.["motorway"]).snappingstr-"default"or"any". Default:"default".
Types
Coordinate
coord = osrm.Coordinate((7.41337, 43.72956))
print(coord.lon, coord.lat)Bearing
bearing = osrm.Bearing((200, 180))
print(bearing.bearing, bearing.range)Object / Array
Service results are returned as Object (dict-like) and Array (list-like) wrappers around OSRM's internal JSON types. They support [], len(), in, and iteration.
result = engine.Route(params)
for route in result["routes"]:
print(route["distance"], route["duration"])CLI
The package also installs OSRM command-line tools, accessible via python -m osrm:
python -m osrm extract data.osm.pbf -p profiles/car.lua
python -m osrm contract data.osrm
python -m osrm partition data.osrm
python -m osrm customize data.osrm
python -m osrm datastore data.osrm
python -m osrm routed data.osrm