-
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
I think this page is obsolete. I add few commet since I tried to compile OSMR recently using Codeblocks 20.03 on Windows 10. What I found was that
Several extra libraries ave to be installed like BZip2, lua. I was using MSYS2 so I did, pacman -S mingw-w64-x86_64-bzip2 pacman -S mingw-w64-x86_64-lua pacman -S mingw-w64-x86_64-zlib
instal Intell tbb https://github.com/oneapi-src/oneTBB/releases Extract it to a folder (e.g., C:\tbb).
Ensure to add TBB Path to CMake Command and that the bin folder of MinGW or MSYS2 (e.g., C:\msys64\mingw64\bin) is added to your system PATH environment variable.
Then in osrm-backend\build run
cmake -G "CodeBlocks - MinGW Makefiles" .. -DCMAKE_BUILD_TYPE=Release -DTBB_INCLUDE_DIR="C:/tbb/include" -DTBB_LIBRARY="C:/tbb/lib/intel64/gcc4.8/libtbb.so"
cmake -G "CodeBlocks - MinGW Makefiles" .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS="-fno-lto -mconsole" -DCMAKE_EXE_LINKER_FLAGS="-Wl,-e,mainCRTStartup"
It may be enough to compile.
However for some compilation problems I add to do
- remove "-Werror # Treat all warnings like error" in a CMakeLists.txt file
- add in shared_memory.hcp in line 208: (void)lock_file; // This explicitly marks lock_file as used to avoid warning of unused variable
- to avoid an Link Time Optimization (LTO) error run cmake -G "CodeBlocks - MinGW Makefiles" .. -DCMAKE_BUILD_TYPE=Release -DIPO=OFF
- put OFF in option(ENABLE_LTO "Use Link Time Optimisation" OFF) and I add set(CMAKE_INTERPROCEDURAL_OPTIMIZATION OFF) in Cmake
I finally gave up because of Windows console incompatibility (Winmain not found) without knowing the reason even after having, In codebleocks Project properties, Built target, type put Console application