From 3df3092cb4c9a17666998ce56d0613251d884c1c Mon Sep 17 00:00:00 2001 From: Whytro Date: Sat, 25 Mar 2023 00:49:33 +0900 Subject: [PATCH] Add support for a default_bearing_radius flag (#6575) --- CHANGELOG.md | 2 ++ docs/nodejs/api.md | 1 + features/options/routed/help.feature | 3 +++ include/engine/engine.hpp | 16 ++++++++----- include/engine/engine_config.hpp | 1 + include/engine/plugins/match.hpp | 6 +++-- include/engine/plugins/nearest.hpp | 2 +- include/engine/plugins/plugin_base.hpp | 10 ++++++-- include/engine/plugins/table.hpp | 3 ++- include/engine/plugins/trip.hpp | 5 +++- include/engine/plugins/viaroute.hpp | 4 +++- include/nodejs/node_osrm_support.hpp | 8 +++++++ src/engine/plugins/match.cpp | 33 +++++++++++++++----------- src/engine/plugins/nearest.cpp | 5 +++- src/engine/plugins/table.cpp | 5 ++-- src/engine/plugins/viaroute.cpp | 7 ++++-- src/nodejs/node_osrm.cpp | 1 + src/tools/routed.cpp | 5 +++- test/nodejs/index.js | 12 ++++++++++ 19 files changed, 95 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cfad168229a..cbff44b384f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Unreleased - Changes from 5.27.1 + - Features + - ADDED: Add support for a default_radius flag. [#6575](https://github.com/Project-OSRM/osrm-backend/pull/6575) - Build: - ADDED: Add CI job which builds OSRM with gcc 12. [#6455](https://github.com/Project-OSRM/osrm-backend/pull/6455) - CHANGED: Upgrade to clang-tidy 15. [#6439](https://github.com/Project-OSRM/osrm-backend/pull/6439) diff --git a/docs/nodejs/api.md b/docs/nodejs/api.md index c4d38902395..b248002104a 100644 --- a/docs/nodejs/api.md +++ b/docs/nodejs/api.md @@ -38,6 +38,7 @@ var osrm = new OSRM('network.osrm'); - `options.max_radius_map_matching` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** Max. radius size supported in map matching query (default: 5). - `options.max_results_nearest` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** Max. results supported in nearest query (default: unlimited). - `options.max_alternatives` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** Max. number of alternatives supported in alternative routes query (default: 3). + - `options.default_radius` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** Default radius for queries (default: unlimited). ### route diff --git a/features/options/routed/help.feature b/features/options/routed/help.feature index a581be3fed4..401a5ba4f8d 100644 --- a/features/options/routed/help.feature +++ b/features/options/routed/help.feature @@ -22,6 +22,7 @@ Feature: osrm-routed command line options: help And stdout should contain "--max-trip-size" And stdout should contain "--max-table-size" And stdout should contain "--max-matching-size" + And stdout should contain "--default-radius" And it should exit successfully Scenario: osrm-routed - Help, short @@ -42,6 +43,7 @@ Feature: osrm-routed command line options: help And stdout should contain "--max-trip-size" And stdout should contain "--max-table-size" And stdout should contain "--max-matching-size" + And stdout should contain "--default-radius" And it should exit successfully Scenario: osrm-routed - Help, long @@ -62,4 +64,5 @@ Feature: osrm-routed command line options: help And stdout should contain "--max-table-size" And stdout should contain "--max-table-size" And stdout should contain "--max-matching-size" + And stdout should contain "--default-radius" And it should exit successfully diff --git a/include/engine/engine.hpp b/include/engine/engine.hpp index fcb4f2dc534..3e479e5cd7d 100644 --- a/include/engine/engine.hpp +++ b/include/engine/engine.hpp @@ -43,12 +43,16 @@ template class Engine final : public EngineInterface { public: explicit Engine(const EngineConfig &config) - : route_plugin(config.max_locations_viaroute, config.max_alternatives), // - table_plugin(config.max_locations_distance_table), // - nearest_plugin(config.max_results_nearest), // - trip_plugin(config.max_locations_trip), // - match_plugin(config.max_locations_map_matching, config.max_radius_map_matching), // - tile_plugin() // + : route_plugin(config.max_locations_viaroute, + config.max_alternatives, + config.default_radius), // + table_plugin(config.max_locations_distance_table, config.default_radius), // + nearest_plugin(config.max_results_nearest, config.default_radius), // + trip_plugin(config.max_locations_trip, config.default_radius), // + match_plugin(config.max_locations_map_matching, + config.max_radius_map_matching, + config.default_radius), // + tile_plugin() // { if (config.use_shared_memory) diff --git a/include/engine/engine_config.hpp b/include/engine/engine_config.hpp index 6f4594b1535..5ca54ba1c31 100644 --- a/include/engine/engine_config.hpp +++ b/include/engine/engine_config.hpp @@ -83,6 +83,7 @@ struct EngineConfig final int max_locations_map_matching = -1; double max_radius_map_matching = -1.0; int max_results_nearest = -1; + boost::optional default_radius; int max_alternatives = 3; // set an arbitrary upper bound; can be adjusted by user bool use_shared_memory = true; boost::filesystem::path memory_file; diff --git a/include/engine/plugins/match.hpp b/include/engine/plugins/match.hpp index 0bb4682ba69..d56be60fe0f 100644 --- a/include/engine/plugins/match.hpp +++ b/include/engine/plugins/match.hpp @@ -20,8 +20,10 @@ class MatchPlugin : public BasePlugin using CandidateLists = routing_algorithms::CandidateLists; static const constexpr double RADIUS_MULTIPLIER = 3; - MatchPlugin(const int max_locations_map_matching, const double max_radius_map_matching) - : max_locations_map_matching(max_locations_map_matching), + MatchPlugin(const int max_locations_map_matching, + const double max_radius_map_matching, + const boost::optional default_radius) + : BasePlugin(default_radius), max_locations_map_matching(max_locations_map_matching), max_radius_map_matching(max_radius_map_matching) { } diff --git a/include/engine/plugins/nearest.hpp b/include/engine/plugins/nearest.hpp index e4c9212b2f2..415639b2cc5 100644 --- a/include/engine/plugins/nearest.hpp +++ b/include/engine/plugins/nearest.hpp @@ -13,7 +13,7 @@ namespace osrm::engine::plugins class NearestPlugin final : public BasePlugin { public: - explicit NearestPlugin(const int max_results); + explicit NearestPlugin(const int max_results, const boost::optional default_radius); Status HandleRequest(const RoutingAlgorithmsInterface &algorithms, const api::NearestParameters ¶ms, diff --git a/include/engine/plugins/plugin_base.hpp b/include/engine/plugins/plugin_base.hpp index d2084ae3636..37545689755 100644 --- a/include/engine/plugins/plugin_base.hpp +++ b/include/engine/plugins/plugin_base.hpp @@ -27,6 +27,10 @@ namespace osrm::engine::plugins class BasePlugin { protected: + BasePlugin() = default; + + BasePlugin(const boost::optional default_radius_) : default_radius(default_radius_) {} + bool CheckAllCoordinates(const std::vector &coordinates) const { return !std::any_of( @@ -237,7 +241,7 @@ class BasePlugin phantom_nodes[i] = facade.NearestPhantomNodes( parameters.coordinates[i], number_of_results, - use_radiuses ? parameters.radiuses[i] : boost::none, + use_radiuses ? parameters.radiuses[i] : default_radius, use_bearings ? parameters.bearings[i] : boost::none, use_approaches && parameters.approaches[i] ? parameters.approaches[i].get() : engine::Approach::UNRESTRICTED); @@ -279,7 +283,7 @@ class BasePlugin alternatives[i] = facade.NearestCandidatesWithAlternativeFromBigComponent( parameters.coordinates[i], - use_radiuses ? parameters.radiuses[i] : boost::none, + use_radiuses ? parameters.radiuses[i] : default_radius, use_bearings ? parameters.bearings[i] : boost::none, use_approaches && parameters.approaches[i] ? parameters.approaches[i].get() : engine::Approach::UNRESTRICTED, @@ -320,6 +324,8 @@ class BasePlugin return std::string("Could not find a matching segment for coordinate ") + std::to_string(missing_index); } + + const boost::optional default_radius; }; } // namespace osrm::engine::plugins diff --git a/include/engine/plugins/table.hpp b/include/engine/plugins/table.hpp index fc8fd18eb00..e4cf9a33ce2 100644 --- a/include/engine/plugins/table.hpp +++ b/include/engine/plugins/table.hpp @@ -14,7 +14,8 @@ namespace osrm::engine::plugins class TablePlugin final : public BasePlugin { public: - explicit TablePlugin(const int max_locations_distance_table); + explicit TablePlugin(const int max_locations_distance_table, + const boost::optional default_radius); Status HandleRequest(const RoutingAlgorithmsInterface &algorithms, const api::TableParameters ¶ms, diff --git a/include/engine/plugins/trip.hpp b/include/engine/plugins/trip.hpp index 01b72a38ccc..9cfdab09409 100644 --- a/include/engine/plugins/trip.hpp +++ b/include/engine/plugins/trip.hpp @@ -32,7 +32,10 @@ class TripPlugin final : public BasePlugin const bool roundtrip) const; public: - explicit TripPlugin(const int max_locations_trip_) : max_locations_trip(max_locations_trip_) {} + explicit TripPlugin(const int max_locations_trip_, boost::optional default_radius) + : BasePlugin(default_radius), max_locations_trip(max_locations_trip_) + { + } Status HandleRequest(const RoutingAlgorithmsInterface &algorithms, const api::TripParameters ¶meters, diff --git a/include/engine/plugins/viaroute.hpp b/include/engine/plugins/viaroute.hpp index d45dd44aa37..e036ac90ecd 100644 --- a/include/engine/plugins/viaroute.hpp +++ b/include/engine/plugins/viaroute.hpp @@ -25,7 +25,9 @@ class ViaRoutePlugin final : public BasePlugin const int max_alternatives; public: - explicit ViaRoutePlugin(int max_locations_viaroute, int max_alternatives); + explicit ViaRoutePlugin(int max_locations_viaroute, + int max_alternatives, + boost::optional default_radius); Status HandleRequest(const RoutingAlgorithmsInterface &algorithms, const api::RouteParameters &route_parameters, diff --git a/include/nodejs/node_osrm_support.hpp b/include/nodejs/node_osrm_support.hpp index a1d7b4473af..5296dc4ed3b 100644 --- a/include/nodejs/node_osrm_support.hpp +++ b/include/nodejs/node_osrm_support.hpp @@ -285,6 +285,7 @@ inline engine_config_ptr argumentsToEngineConfig(const Napi::CallbackInfo &args) auto max_results_nearest = params.Get("max_results_nearest"); auto max_alternatives = params.Get("max_alternatives"); auto max_radius_map_matching = params.Get("max_radius_map_matching"); + auto default_radius = params.Get("default_radius"); if (!max_locations_trip.IsUndefined() && !max_locations_trip.IsNumber()) { @@ -316,6 +317,11 @@ inline engine_config_ptr argumentsToEngineConfig(const Napi::CallbackInfo &args) ThrowError(args.Env(), "max_alternatives must be an integral number"); return engine_config_ptr(); } + if (!default_radius.IsUndefined() && !default_radius.IsNumber()) + { + ThrowError(args.Env(), "default_radius must be an integral number"); + return engine_config_ptr(); + } if (max_locations_trip.IsNumber()) engine_config->max_locations_trip = max_locations_trip.ToNumber().Int32Value(); @@ -333,6 +339,8 @@ inline engine_config_ptr argumentsToEngineConfig(const Napi::CallbackInfo &args) engine_config->max_alternatives = max_alternatives.ToNumber().Int32Value(); if (max_radius_map_matching.IsNumber()) engine_config->max_radius_map_matching = max_radius_map_matching.ToNumber().DoubleValue(); + if (default_radius.IsNumber()) + engine_config->default_radius = default_radius.ToNumber().DoubleValue(); return engine_config; } diff --git a/src/engine/plugins/match.cpp b/src/engine/plugins/match.cpp index 8fe0d78e023..90517df1849 100644 --- a/src/engine/plugins/match.cpp +++ b/src/engine/plugins/match.cpp @@ -181,24 +181,29 @@ Status MatchPlugin::HandleRequest(const RoutingAlgorithmsInterface &algorithms, if (tidied.parameters.radiuses.empty()) { search_radiuses.resize(tidied.parameters.coordinates.size(), - routing_algorithms::DEFAULT_GPS_PRECISION * RADIUS_MULTIPLIER); + default_radius.has_value() + ? *default_radius + : routing_algorithms::DEFAULT_GPS_PRECISION * RADIUS_MULTIPLIER); } else { search_radiuses.resize(tidied.parameters.coordinates.size()); - std::transform(tidied.parameters.radiuses.begin(), - tidied.parameters.radiuses.end(), - search_radiuses.begin(), - [](const boost::optional &maybe_radius) { - if (maybe_radius) - { - return *maybe_radius * RADIUS_MULTIPLIER; - } - else - { - return routing_algorithms::DEFAULT_GPS_PRECISION * RADIUS_MULTIPLIER; - } - }); + std::transform( + tidied.parameters.radiuses.begin(), + tidied.parameters.radiuses.end(), + search_radiuses.begin(), + [default_radius = this->default_radius](const boost::optional &maybe_radius) { + if (maybe_radius) + { + return *maybe_radius * RADIUS_MULTIPLIER; + } + else + { + return default_radius.has_value() + ? *default_radius + : routing_algorithms::DEFAULT_GPS_PRECISION * RADIUS_MULTIPLIER; + } + }); } auto candidates_lists = diff --git a/src/engine/plugins/nearest.cpp b/src/engine/plugins/nearest.cpp index bd6dd8e54e0..671dbe3f273 100644 --- a/src/engine/plugins/nearest.cpp +++ b/src/engine/plugins/nearest.cpp @@ -10,7 +10,10 @@ namespace osrm::engine::plugins { -NearestPlugin::NearestPlugin(const int max_results_) : max_results{max_results_} {} +NearestPlugin::NearestPlugin(const int max_results_, const boost::optional default_radius_) + : BasePlugin(default_radius_), max_results{max_results_} +{ +} Status NearestPlugin::HandleRequest(const RoutingAlgorithmsInterface &algorithms, const api::NearestParameters ¶ms, diff --git a/src/engine/plugins/table.cpp b/src/engine/plugins/table.cpp index 150cefb3424..4518113053b 100644 --- a/src/engine/plugins/table.cpp +++ b/src/engine/plugins/table.cpp @@ -14,8 +14,9 @@ namespace osrm::engine::plugins { -TablePlugin::TablePlugin(const int max_locations_distance_table) - : max_locations_distance_table(max_locations_distance_table) +TablePlugin::TablePlugin(const int max_locations_distance_table, + const boost::optional default_radius) + : BasePlugin(default_radius), max_locations_distance_table(max_locations_distance_table) { } diff --git a/src/engine/plugins/viaroute.cpp b/src/engine/plugins/viaroute.cpp index aa533d96ac8..fe072793664 100644 --- a/src/engine/plugins/viaroute.cpp +++ b/src/engine/plugins/viaroute.cpp @@ -15,8 +15,11 @@ namespace osrm::engine::plugins { -ViaRoutePlugin::ViaRoutePlugin(int max_locations_viaroute, int max_alternatives) - : max_locations_viaroute(max_locations_viaroute), max_alternatives(max_alternatives) +ViaRoutePlugin::ViaRoutePlugin(int max_locations_viaroute, + int max_alternatives, + boost::optional default_radius) + : BasePlugin(default_radius), max_locations_viaroute(max_locations_viaroute), + max_alternatives(max_alternatives) { } diff --git a/src/nodejs/node_osrm.cpp b/src/nodejs/node_osrm.cpp index 1537ab4d590..5d5bb916ff4 100644 --- a/src/nodejs/node_osrm.cpp +++ b/src/nodejs/node_osrm.cpp @@ -81,6 +81,7 @@ Napi::Object Engine::Init(Napi::Env env, Napi::Object exports) * @param {Number} [options.max_radius_map_matching] Max. radius size supported in map matching query (default: 5). * @param {Number} [options.max_results_nearest] Max. results supported in nearest query (default: unlimited). * @param {Number} [options.max_alternatives] Max. number of alternatives supported in alternative routes query (default: 3). + * @param {Number} [options.default_radius] Default radius for queries (default: unlimited). * * @class OSRM * diff --git a/src/tools/routed.cpp b/src/tools/routed.cpp index 94347505239..e8fb366951e 100644 --- a/src/tools/routed.cpp +++ b/src/tools/routed.cpp @@ -147,7 +147,10 @@ inline unsigned generateServerProgramOptions(const int argc, "Max. number of alternatives supported in the MLD route query") // ("max-matching-radius", value(&config.max_radius_map_matching)->default_value(-1.0), - "Max. radius size supported in map matching query. Default: unlimited."); + "Max. radius size supported in map matching query. Default: unlimited.") // + ("default-radius", + value>(&config.default_radius), + "Default radius size for queries. Default: unlimited."); // hidden options, will be allowed on command line, but will not be shown to the user boost::program_options::options_description hidden_options("Hidden options"); diff --git a/test/nodejs/index.js b/test/nodejs/index.js index f083567063a..dd6f44ea489 100644 --- a/test/nodejs/index.js +++ b/test/nodejs/index.js @@ -112,6 +112,18 @@ test('constructor: throws if dataset_name is not a string', function(assert) { assert.throws(function() { new OSRM({dataset_name: "unsued_name___", shared_memory: true}); }, /Could not find shared memory region/, 'Does not accept wrong name'); }); +test('constructor: takes a default_radius argument', function(assert) { + assert.plan(1); + var osrm = new OSRM({algorithm: 'MLD', path: monaco_mld_path, default_radius: 1}); + assert.ok(osrm); +}); + +test('constructor: throws if default_radius is not a number', function(assert) { + assert.plan(2); + assert.throws(function() { new OSRM({algorithm: 'MLD', path: monaco_mld_path, default_radius: 'abc'}); }, /default_radius must be an integral number/, 'Does not accept string'); + assert.ok(new OSRM({algorithm: 'MLD', path: monaco_mld_path, default_radius: 1}), 'Does accept number'); +}); + test('constructor: parses custom limits', function(assert) { assert.plan(1); var osrm = new OSRM({