-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Library API
OSRM can be used as a library (libosrm) via C++ instead of using it through the HTTP interface and osrm-routed
. This allows for fine-tuning OSRM and has much less overhead. Here is a quick introduction into how to use libosrm
in the upcoming v5 release.
Take a look at the example code that lives in the example directory. Here is all you ever wanted to know about libosrm
, that is a short description of what the types do and where to find documentation on it:
-
EngineConfig
- for initializing an OSRM instance we can configure certain properties and constraints. E.g. the storage config is the base path such asfrance.osm.osrm
from which we derive and loadfrance.osm.osrm.*
auxiliary files. This also lets you set constraints such as the maximum number of locations allowed for specific services. -
OSRM
- this is the main Routing Machine type with functions such asRoute
andTable
. You initialize it with aEngineConfig
. It does all the heavy lifting for you. Each function takes its own parameters, e.g. theRoute
function takesRouteParameters
, and a out-reference to a JSON result that gets filled. The return value is aStatus
, indicating error or success. -
Status
- this is a type wrappingError
orOk
for indicating error or success, respectively. -
TableParameters
- this is an example of parameter types the Routing Machine functions expect. In this caseTable
expects its own parameters asTableParameters
. You can see it wrapping two vectors, sources and destinations --- these are indices into your coordinates for the table service to construct a matrix from (empty sources or destinations means: use all of them). If you ask yourself where coordinates come from, you can seeTableParameters
inheriting fromBaseParameters
. -
BaseParameter
- this most importantly holds coordinates (and a few other optional properties that you don't need for basic usage); the specific parameter types inherit fromBaseParameters
to get these member attributes. That means yourTableParameters
type hascoordinates
,sources
anddestination
member attributes (and a few other that we ignore for now). -
Coordinate
- this is a wrapper around a (longitude, latitude) pair. We really don't care about (lon,lat) vs (lat, lon) but we don't want you to accidentally mix them up, so both latitude and longitude are strictly typed wrappers around integers (fixed notation such as13423240
) and floating points (floating notation such as13.42324
). -
Parameters for other services - here are all other
*Parameters
you need for other Routing Machine services. -
JSON - this is a sum type resembling JSON. The Routing Machine service functions take a out-ref to a JSON result and fill it accordingly. It is currently implemented using mapbox/variant which is similar to Boost.Variant (Boost documentation is great). There are two ways to work with this sum type: either provide a visitor that acts on each type on visitation or use the
get
function in case you're sure about the structure. The JSON structure is written down in the v5 server API.
To summarize:
- create an
OSRM
instance initialized with aEngineConfig
- call the service function on the
OSRM
object providing service specific*Parameters
- check the return code and use the JSON result
Building and Running The C++ API example application:
Once libosrm is installed on your system, move to the example folder containing the example.cpp file, then run:
mkdir build
cd build
cmake ..
cmake --build .
Then run with the pre-processed osrm data as an argument e.g:
./osrm-backend monaco.osrm