Skip to content

Commit

Permalink
Add support for a default_bearing_radius flag (#6575)
Browse files Browse the repository at this point in the history
  • Loading branch information
whytro authored Mar 24, 2023
1 parent d6afe91 commit d516314
Show file tree
Hide file tree
Showing 19 changed files with 95 additions and 34 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
1 change: 1 addition & 0 deletions docs/nodejs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions features/options/routed/help.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
16 changes: 10 additions & 6 deletions include/engine/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,16 @@ template <typename Algorithm> 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)
Expand Down
1 change: 1 addition & 0 deletions include/engine/engine_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double> 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;
Expand Down
6 changes: 4 additions & 2 deletions include/engine/plugins/match.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double> default_radius)
: BasePlugin(default_radius), max_locations_map_matching(max_locations_map_matching),
max_radius_map_matching(max_radius_map_matching)
{
}
Expand Down
2 changes: 1 addition & 1 deletion include/engine/plugins/nearest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double> default_radius);

Status HandleRequest(const RoutingAlgorithmsInterface &algorithms,
const api::NearestParameters &params,
Expand Down
10 changes: 8 additions & 2 deletions include/engine/plugins/plugin_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ namespace osrm::engine::plugins
class BasePlugin
{
protected:
BasePlugin() = default;

BasePlugin(const boost::optional<double> default_radius_) : default_radius(default_radius_) {}

bool CheckAllCoordinates(const std::vector<util::Coordinate> &coordinates) const
{
return !std::any_of(
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<double> default_radius;
};
} // namespace osrm::engine::plugins

Expand Down
3 changes: 2 additions & 1 deletion include/engine/plugins/table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double> default_radius);

Status HandleRequest(const RoutingAlgorithmsInterface &algorithms,
const api::TableParameters &params,
Expand Down
5 changes: 4 additions & 1 deletion include/engine/plugins/trip.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double> default_radius)
: BasePlugin(default_radius), max_locations_trip(max_locations_trip_)
{
}

Status HandleRequest(const RoutingAlgorithmsInterface &algorithms,
const api::TripParameters &parameters,
Expand Down
4 changes: 3 additions & 1 deletion include/engine/plugins/viaroute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double> default_radius);

Status HandleRequest(const RoutingAlgorithmsInterface &algorithms,
const api::RouteParameters &route_parameters,
Expand Down
8 changes: 8 additions & 0 deletions include/nodejs/node_osrm_support.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand Down Expand Up @@ -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();
Expand All @@ -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;
}
Expand Down
33 changes: 19 additions & 14 deletions src/engine/plugins/match.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double> &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<double> &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 =
Expand Down
5 changes: 4 additions & 1 deletion src/engine/plugins/nearest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double> default_radius_)
: BasePlugin(default_radius_), max_results{max_results_}
{
}

Status NearestPlugin::HandleRequest(const RoutingAlgorithmsInterface &algorithms,
const api::NearestParameters &params,
Expand Down
5 changes: 3 additions & 2 deletions src/engine/plugins/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double> default_radius)
: BasePlugin(default_radius), max_locations_distance_table(max_locations_distance_table)
{
}

Expand Down
7 changes: 5 additions & 2 deletions src/engine/plugins/viaroute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double> default_radius)
: BasePlugin(default_radius), max_locations_viaroute(max_locations_viaroute),
max_alternatives(max_alternatives)
{
}

Expand Down
1 change: 1 addition & 0 deletions src/nodejs/node_osrm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
5 changes: 4 additions & 1 deletion src/tools/routed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ inline unsigned generateServerProgramOptions(const int argc,
"Max. number of alternatives supported in the MLD route query") //
("max-matching-radius",
value<double>(&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<boost::optional<double>>(&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");
Expand Down
12 changes: 12 additions & 0 deletions test/nodejs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit d516314

Please sign in to comment.