Skip to content

Commit 72da455

Browse files
authored
Allow -1.0 as unlimited for default_radius value (#6599)
1 parent 0ca9131 commit 72da455

File tree

8 files changed

+74
-18
lines changed

8 files changed

+74
-18
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
- NodeJS:
1111
- CHANGED: Use node-api instead of NAN. [#6452](https://github.com/Project-OSRM/osrm-backend/pull/6452)
1212
- Misc:
13+
- ADDED: Add support for "unlimited" to be passed as a value for the default-radius and max-matching-radius flags. [#6599](https://github.com/Project-OSRM/osrm-backend/pull/6599)
14+
- CHANGED: Allow -1.0 as unlimited for default_radius value. [#6599](https://github.com/Project-OSRM/osrm-backend/pull/6599)
1315
- CHANGED: keep libosrm* in the docker image for downstream linking [#6602](https://github.com/Project-OSRM/osrm-backend/pull/6602)
1416
- CHANGED: Move vector in CSVFilesParser instead copying it. [#6470](https://github.com/Project-OSRM/osrm-backend/pull/6470)
1517
- REMOVED: Get rid of unused functions in util/json_util.hpp. [#6446](https://github.com/Project-OSRM/osrm-backend/pull/6446)

include/engine/engine_config.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ struct EngineConfig final
8383
int max_locations_map_matching = -1;
8484
double max_radius_map_matching = -1.0;
8585
int max_results_nearest = -1;
86-
boost::optional<double> default_radius;
86+
boost::optional<double> default_radius = -1.0;
8787
int max_alternatives = 3; // set an arbitrary upper bound; can be adjusted by user
8888
bool use_shared_memory = true;
8989
boost::filesystem::path memory_file;

include/engine/geospatial_query.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ template <typename RTreeT, typename DataFacadeT> class GeospatialQuery
7272
[this, &max_distance, &max_results, input_coordinate](const std::size_t num_results,
7373
const CandidateSegment &segment) {
7474
return (max_results && num_results >= *max_results) ||
75-
(max_distance &&
75+
(max_distance && max_distance != -1.0 &&
7676
CheckSegmentDistance(input_coordinate, segment, *max_distance));
7777
});
7878

@@ -163,7 +163,8 @@ template <typename RTreeT, typename DataFacadeT> class GeospatialQuery
163163
auto distance = GetSegmentDistance(input_coordinate, segment);
164164
auto further_than_big_component = distance > big_component_distance;
165165
auto no_more_candidates = has_big_component && further_than_big_component;
166-
auto too_far_away = max_distance && distance > *max_distance;
166+
auto too_far_away =
167+
max_distance && max_distance != -1.0 && distance > *max_distance;
167168

168169
// Time to terminate the search when:
169170
// 1. We've found a node from a big component and the next candidate is further away

include/nodejs/node_osrm_support.hpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,16 @@ inline engine_config_ptr argumentsToEngineConfig(const Napi::CallbackInfo &args)
317317
ThrowError(args.Env(), "max_alternatives must be an integral number");
318318
return engine_config_ptr();
319319
}
320-
if (!default_radius.IsUndefined() && !default_radius.IsNumber())
320+
if (!max_radius_map_matching.IsUndefined() && max_radius_map_matching.IsString() &&
321+
max_radius_map_matching.ToString().Utf8Value() != "unlimited")
321322
{
322-
ThrowError(args.Env(), "default_radius must be an integral number");
323+
ThrowError(args.Env(), "max_radius_map_matching must be unlimited or an integral number");
324+
return engine_config_ptr();
325+
}
326+
if (!default_radius.IsUndefined() && default_radius.IsString() &&
327+
default_radius.ToString().Utf8Value() != "unlimited")
328+
{
329+
ThrowError(args.Env(), "default_radius must be unlimited or an integral number");
323330
return engine_config_ptr();
324331
}
325332

@@ -337,10 +344,17 @@ inline engine_config_ptr argumentsToEngineConfig(const Napi::CallbackInfo &args)
337344
engine_config->max_results_nearest = max_results_nearest.ToNumber().Int32Value();
338345
if (max_alternatives.IsNumber())
339346
engine_config->max_alternatives = max_alternatives.ToNumber().Int32Value();
347+
340348
if (max_radius_map_matching.IsNumber())
341349
engine_config->max_radius_map_matching = max_radius_map_matching.ToNumber().DoubleValue();
350+
else if (max_radius_map_matching.IsString() &&
351+
max_radius_map_matching.ToString().Utf8Value() == "unlimited")
352+
engine_config->max_radius_map_matching = -1.0;
353+
342354
if (default_radius.IsNumber())
343355
engine_config->default_radius = default_radius.ToNumber().DoubleValue();
356+
else if (default_radius.IsString() && default_radius.ToString().Utf8Value() == "unlimited")
357+
engine_config->default_radius = -1.0;
344358

345359
return engine_config;
346360
}

src/engine/engine_config.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ bool EngineConfig::IsValid() const
1313
return v == -1 || v > limit;
1414
};
1515

16-
const bool limits_valid = unlimited_or_more_than(max_locations_distance_table, 2) &&
17-
unlimited_or_more_than(max_locations_map_matching, 2) &&
18-
unlimited_or_more_than(max_radius_map_matching, 0) &&
19-
unlimited_or_more_than(max_locations_trip, 2) &&
20-
unlimited_or_more_than(max_locations_viaroute, 2) &&
21-
unlimited_or_more_than(max_results_nearest, 0) &&
22-
max_alternatives >= 0;
16+
const bool limits_valid =
17+
unlimited_or_more_than(max_locations_distance_table, 2) &&
18+
unlimited_or_more_than(max_locations_map_matching, 2) &&
19+
unlimited_or_more_than(max_radius_map_matching, 0) &&
20+
unlimited_or_more_than(max_locations_trip, 2) &&
21+
unlimited_or_more_than(max_locations_viaroute, 2) &&
22+
unlimited_or_more_than(max_results_nearest, 0) &&
23+
(!default_radius.has_value() || unlimited_or_more_than(*default_radius, 0)) &&
24+
max_alternatives >= 0;
2325

2426
return ((use_shared_memory && all_path_are_empty) || (use_mmap && storage_config.IsValid()) ||
2527
storage_config.IsValid()) &&

src/engine/plugins/match.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ Status MatchPlugin::HandleRequest(const RoutingAlgorithmsInterface &algorithms,
181181
if (tidied.parameters.radiuses.empty())
182182
{
183183
search_radiuses.resize(tidied.parameters.coordinates.size(),
184-
default_radius.has_value()
184+
default_radius.has_value() && *default_radius != -1.0
185185
? *default_radius
186186
: routing_algorithms::DEFAULT_GPS_PRECISION * RADIUS_MULTIPLIER);
187187
}
@@ -199,7 +199,7 @@ Status MatchPlugin::HandleRequest(const RoutingAlgorithmsInterface &algorithms,
199199
}
200200
else
201201
{
202-
return default_radius.has_value()
202+
return default_radius.has_value() && *default_radius != -1.0
203203
? *default_radius
204204
: routing_algorithms::DEFAULT_GPS_PRECISION * RADIUS_MULTIPLIER;
205205
}

src/tools/routed.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <boost/algorithm/string/case_conv.hpp>
1313
#include <boost/any.hpp>
1414
#include <boost/filesystem.hpp>
15+
#include <boost/optional/optional_io.hpp>
1516
#include <boost/program_options.hpp>
1617

1718
#include <cstdlib>
@@ -70,6 +71,33 @@ std::istream &operator>>(std::istream &in, EngineConfig::Algorithm &algorithm)
7071
}
7172
} // namespace osrm::engine
7273

74+
// overload validate for the double type to allow "unlimited" as an input
75+
namespace boost
76+
{
77+
void validate(boost::any &v, const std::vector<std::string> &values, double *, double)
78+
{
79+
boost::program_options::validators::check_first_occurrence(v);
80+
const std::string &s = boost::program_options::validators::get_single_string(values);
81+
82+
if (s == "unlimited")
83+
{
84+
v = -1.0;
85+
}
86+
else
87+
{
88+
try
89+
{
90+
v = std::stod(s);
91+
}
92+
catch (const std::invalid_argument &)
93+
{
94+
throw boost::program_options::validation_error(
95+
boost::program_options::validation_error::invalid_option_value);
96+
}
97+
}
98+
}
99+
} // namespace boost
100+
73101
// generate boost::program_options object for the routing part
74102
inline unsigned generateServerProgramOptions(const int argc,
75103
const char *argv[],
@@ -149,7 +177,7 @@ inline unsigned generateServerProgramOptions(const int argc,
149177
value<double>(&config.max_radius_map_matching)->default_value(-1.0),
150178
"Max. radius size supported in map matching query. Default: unlimited.") //
151179
("default-radius",
152-
value<boost::optional<double>>(&config.default_radius),
180+
value<boost::optional<double>>(&config.default_radius)->default_value(-1.0),
153181
"Default radius size for queries. Default: unlimited.");
154182

155183
// hidden options, will be allowed on command line, but will not be shown to the user

test/nodejs/index.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,17 @@ test('constructor: takes a default_radius argument', function(assert) {
118118
assert.ok(osrm);
119119
});
120120

121+
test('constructor: takes a default_radius unlimited argument', function(assert) {
122+
assert.plan(1);
123+
var osrm = new OSRM({algorithm: 'MLD', path: monaco_mld_path, default_radius: 'unlimited'});
124+
assert.ok(osrm);
125+
});
126+
121127
test('constructor: throws if default_radius is not a number', function(assert) {
122-
assert.plan(2);
123-
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');
128+
assert.plan(3);
129+
assert.throws(function() { new OSRM({algorithm: 'MLD', path: monaco_mld_path, default_radius: 'abc'}); }, /default_radius must be unlimited or an integral number/, 'Does not accept invalid string');
124130
assert.ok(new OSRM({algorithm: 'MLD', path: monaco_mld_path, default_radius: 1}), 'Does accept number');
131+
assert.ok(new OSRM({algorithm: 'MLD', path: monaco_mld_path, default_radius: 'unlimited'}), 'Does accept unlimited');
125132
});
126133

127134
test('constructor: parses custom limits', function(assert) {
@@ -135,6 +142,7 @@ test('constructor: parses custom limits', function(assert) {
135142
max_locations_map_matching: 1,
136143
max_results_nearest: 1,
137144
max_alternatives: 1,
145+
default_radius: 1
138146
});
139147
assert.ok(osrm);
140148
});
@@ -150,7 +158,8 @@ test('constructor: throws on invalid custom limits', function(assert) {
150158
max_locations_distance_table: false,
151159
max_locations_map_matching: 'a lot',
152160
max_results_nearest: null,
153-
max_alternatives: '10'
161+
max_alternatives: '10',
162+
default_radius: '10'
154163
})
155164
});
156165
});

0 commit comments

Comments
 (0)