Conversation
There was a problem hiding this comment.
Pull request overview
Reintroduces and documents OSRM “library usage” examples by adding a C++ libosrm sample, a CMake find-module for libosrm, and refreshing the Node.js example and documentation.
Changes:
- Modernize
example/example.jsto ESM imports and update its Monaco query example. - Add a C++
libosrmrouting example (example/example.cpp) plus a CMakeFindLibOSRM.cmake. - Add new documentation (
docs/libosrm.md) and anexample/README.mddescribing how to build/run the examples.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 10 comments.
Show a summary per file
| File | Description |
|---|---|
| example/example.js | Converts the Node.js example to ESM, updates threadpool sizing and example query URL. |
| example/example.cpp | Adds a standalone C++ libosrm “Route” example using EngineConfig + OSRM::Route. |
| example/cmake/FindLibOSRM.cmake | Introduces a pkg-config-based CMake find-module for locating libosrm. |
| example/README.md | Adds instructions for preparing test data and building/running the examples. |
| docs/libosrm.md | Adds introductory documentation for using OSRM as a C++ library. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| set(LibOSRM_CXXFLAGS ${output}) | ||
| set(LibOSRM_LIBRARY_DIRS ${PC_LibOSRM_LIBRARY_DIRS}) | ||
|
|
||
| find_path(LibOSRM_INCLUDE_DIR osrm/osrm.hpp | ||
| PATH_SUFFIXES osrm include/osrm include | ||
| HINTS ${PC_LibOSRM_INCLUDEDIR} ${PC_LibOSRM_INCLUDE_DIRS} | ||
| ~/Library/Frameworks |
There was a problem hiding this comment.
FindLibOSRM.cmake sets LibOSRM_INCLUDE_DIR but does not define LibOSRM_INCLUDE_DIRS. The example CMake build expects an *_INCLUDE_DIRS variable (plural), so includes will be empty and compilation will fail unless the caller guesses the singular variable name. Export LibOSRM_INCLUDE_DIRS (and ideally keep LibOSRM_INCLUDE_DIR as an alias) to make the find-module usable.
| find_library(TEST_LibOSRM_STATIC_LIBRARY Names osrm.lib libosrm.a | ||
| PATH_SUFFIXES osrm lib/osrm lib | ||
| HINTS ${PC_LibOSRM_LIBDIR} ${PC_LibOSRM_LIBRARY_DIRS} | ||
| ~/Library/Frameworks | ||
| /Library/Frameworks | ||
| /usr/local | ||
| /usr | ||
| /opt/local | ||
| /opt) | ||
| find_library(TEST_LibOSRM_DYNAMIC_LIBRARY Names libosrm.dylib libosrm.so | ||
| PATH_SUFFIXES osrm lib/osrm lib | ||
| HINTS ${PC_LibOSRM_LIBDIR} ${PC_LibOSRM_LIBRARY_DIRS} | ||
| ~/Library/Frameworks | ||
| /Library/Frameworks | ||
| /usr/local | ||
| /usr | ||
| /opt/local | ||
| /opt) |
There was a problem hiding this comment.
TEST_LibOSRM_STATIC_LIBRARY and TEST_LibOSRM_DYNAMIC_LIBRARY are discovered but never used to influence LibOSRM_LIBRARIES / LibOSRM_DEPENDENT_LIBRARIES or the package-found check. This is confusing and can hide misconfigurations (e.g., pkg-config missing but libraries present). Either remove these lookups or use them as a fallback / validation in find_package_handle_standard_args.
| ```bash | ||
| cd example/ | ||
| cmake .. | ||
| cmake --build . | ||
| ``` |
There was a problem hiding this comment.
The build instructions run cmake .. from inside example/, which points CMake at the repository root, not the example/ project (there is a dedicated example/CMakeLists.txt). This will configure/build the main OSRM project again instead of the example. Update the instructions to configure the example source directory explicitly (e.g., cmake -S . -B build from example/).
| - [`TableParameters`](https://github.com/Project-OSRM/osrm-backend/blob/master/include/engine/api/table_parameters.hpp) - this is an example of parameter types the Routing Machine functions expect. In this case `Table` expects its own parameters as `TableParameters`. 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 see `TableParameters` inheriting from `BaseParameters`. | ||
|
|
||
| - [`BaseParameter`](https://github.com/Project-OSRM/osrm-backend/blob/master/include/engine/api/base_parameters.hpp) - this most importantly holds coordinates (and a few other optional properties that you don't need for basic usage); the specific parameter types inherit from `BaseParameters` to get these member attributes. That means your `TableParameters` type has `coordinates`, `sources` and `destination` member attributes (and a few other that we ignore for now). | ||
|
|
There was a problem hiding this comment.
The BaseParameters type name and member names are incorrect here: the class is BaseParameters (plural), and TableParameters has destinations (plural), not destination. This can mislead users copying the doc into real code.
| - [Parameters for other services](https://github.com/Project-OSRM/osrm-backend/tree/master/include/engine/api) - here are all other `*Parameters` you need for other Routing Machine services. | ||
|
|
||
| - [JSON](https://github.com/Project-OSRM/osrm-backend/blob/master/include/util/json_container.hpp) - 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](https://github.com/mapbox/variant) which is similar to [Boost.Variant](http://www.boost.org/doc/libs/1_55_0/doc/html/variant.html). 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 [HTTP API](#http-api). | ||
|
|
There was a problem hiding this comment.
The JSON section is out of date: util::json::Value is implemented with std::variant (and visited with std::visit), not mapbox/variant / Boost.Variant. Updating this avoids sending readers to the wrong APIs and libraries.
| std::cout << "Code: " << code << "\n"; | ||
| std::cout << "Message: " << message << "\n"; | ||
| return EXIT_FAILURE; | ||
| } |
There was a problem hiding this comment.
If status ever ends up neither Status::Ok nor Status::Error (or if additional enum values are added later), main falls off the end and returns 0, which would incorrectly signal success. Add a final else/default return path that reports an unexpected status and returns EXIT_FAILURE.
| } | |
| } | |
| else | |
| { | |
| std::cerr << "Unexpected status value returned from OSRM Route\n"; | |
| return EXIT_FAILURE; | |
| } |
| import OSRM from '../lib/index.js'; | ||
|
|
||
| process.env.UV_THREADPOOL_SIZE = Math.ceil(availableParallelism()); | ||
|
|
There was a problem hiding this comment.
UV_THREADPOOL_SIZE is assigned after ESM static imports. Since static imports are evaluated before this assignment, the libuv threadpool size may already be initialized by the time the OSRM binding loads, making this setting ineffective. Set the env var before loading the binding (e.g., move it above and load OSRM via dynamic import), or document that it must be set externally before starting Node.
| // Accepts a query like: | ||
| // http://localhost:8888?start=13.414307,52.521835&end=13.402290,52.523728 | ||
| // http://localhost:8888?start=7.419758,43.731142&end=7.419505,43.736825 | ||
| app.get('/', function(req, res) { |
There was a problem hiding this comment.
Later in this example the route options object uses alternateRoute, but the Node.js binding expects the alternatives option (see test/nodejs/route.js). As written, the ?alternatives= query parameter won’t actually control alternative routing.
| @@ -0,0 +1,33 @@ | |||
| ## Introduction | |||
|
|
|||
| 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. | |||
There was a problem hiding this comment.
This introduction says “upcoming v5 release”, but the repository is already past v5 (the JS package version is 6.x). This makes the new doc immediately read as outdated; please rephrase to be version-agnostic or refer to the current major version.
| 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. | |
| 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 to how to use `libosrm`. |
| 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](https://github.com/Project-OSRM/osrm-backend/tree/master/example). 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: | ||
|
|
There was a problem hiding this comment.
This new page isn’t linked from the VitePress nav/sidebar (see docs/.vitepress/config.js), and there don’t appear to be other docs linking to /libosrm yet. Add it to the docs navigation (or at least link it from docs/index.md) so users can discover it in the rendered documentation site.
| For a general overview of OSRM and its HTTP API, see the [main documentation](./index.md). |
Issue
Adresses #7266. No tests or changelog because it doesn't change the library itself, but I think the wiki may be improved.
Tasklist
Requirements / Relations
N/A