From 7d2a0c4133880071e60c86c2b3dfeac9e85d1197 Mon Sep 17 00:00:00 2001 From: "Daniel J. Hofmann" Date: Tue, 28 Feb 2017 12:19:21 +0100 Subject: [PATCH] Exposes `generate_hints` parameter, resolves #300 --- docs/api.md | 13 ++++++------ src/node_osrm_support.hpp | 37 +++++++++++++++++++++++++------- test/table.js | 44 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 79 insertions(+), 15 deletions(-) diff --git a/docs/api.md b/docs/api.md index e0f261b..bbe8fcb 100644 --- a/docs/api.md +++ b/docs/api.md @@ -33,12 +33,13 @@ var osrm = new OSRM('network.osrm'); Each OSRM method (except for `OSRM.tile()`) has set of general options as well as unique options, outlined below. -| Option | Values | Description | Format | -| ----------- | ------------------------------------------------------- | ------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ | -| coordinates | `array` of `coordinate` elements: `[{coordinate}, ...]` | The coordinates this request will use. | `array` with `[{lon},{lat}]` values, in decimal degrees | -| bearings | `array` of `bearing` elements: `[{bearing}, ...]` | Limits the search to segments with given bearing in degrees towards true north in clockwise direction. | `null` or `array` with `[{value},{range}]` `integer 0 .. 360,integer 0 .. 180` | -| radiuses | `array` of `radius` elements: `[{radius}, ...]` | Limits the search to given radius in meters. | `null` or `double >= 0` or `unlimited` (default) | -| hints | `array` of `hint` elements: `[{hint}, ...]` | Hint to derive position in street network. | Base64 `string` | +| Option | Values | Description | Format | +| --------------- | ------------------------------------------------------- | ------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ | +| coordinates | `array` of `coordinate` elements: `[{coordinate}, ...]` | The coordinates this request will use. | `array` with `[{lon},{lat}]` values, in decimal degrees | +| bearings | `array` of `bearing` elements: `[{bearing}, ...]` | Limits the search to segments with given bearing in degrees towards true north in clockwise direction. | `null` or `array` with `[{value},{range}]` `integer 0 .. 360,integer 0 .. 180` | +| radiuses | `array` of `radius` elements: `[{radius}, ...]` | Limits the search to given radius in meters. | `null` or `double >= 0` or `unlimited` (default) | +| hints | `array` of `hint` elements: `[{hint}, ...]` | Hint to derive position in street network. | Base64 `string` | +| generate\_hints | `true` (default) or `false` | Adds a Hint to the response which can be used in subsequent requests, see `hints` parameter. | `Boolean` | ## route diff --git a/src/node_osrm_support.hpp b/src/node_osrm_support.hpp index 5ea61d7..a2c1946 100644 --- a/src/node_osrm_support.hpp +++ b/src/node_osrm_support.hpp @@ -397,6 +397,19 @@ inline bool argumentsToParameter(const Nan::FunctionCallbackInfo &arg } } + if (obj->Has(Nan::New("generate_hints").ToLocalChecked())) + { + v8::Local generate_hints = obj->Get(Nan::New("generate_hints").ToLocalChecked()); + + if (!generate_hints->IsBoolean()) + { + Nan::ThrowError("generate_hints must be of type Boolean"); + return false; + } + + params->generate_hints = generate_hints->BooleanValue(); + } + return true; } @@ -430,31 +443,38 @@ inline bool parseCommonParameters(const v8::Local &obj, ParamType &p for (std::size_t i = 0; i < annotations_array->Length(); i++) { const Nan::Utf8String annotations_utf8str(annotations_array->Get(i)); - std::string annotations_str{*annotations_utf8str, *annotations_utf8str + annotations_utf8str.length()}; + std::string annotations_str{*annotations_utf8str, + *annotations_utf8str + annotations_utf8str.length()}; if (annotations_str == "duration") { - params->annotations_type = params->annotations_type | osrm::RouteParameters::AnnotationsType::Duration; + params->annotations_type = + params->annotations_type | osrm::RouteParameters::AnnotationsType::Duration; } else if (annotations_str == "nodes") { - params->annotations_type = params->annotations_type | osrm::RouteParameters::AnnotationsType::Nodes; + params->annotations_type = + params->annotations_type | osrm::RouteParameters::AnnotationsType::Nodes; } else if (annotations_str == "distance") { - params->annotations_type = params->annotations_type | osrm::RouteParameters::AnnotationsType::Distance; + params->annotations_type = + params->annotations_type | osrm::RouteParameters::AnnotationsType::Distance; } else if (annotations_str == "weight") { - params->annotations_type = params->annotations_type | osrm::RouteParameters::AnnotationsType::Weight; + params->annotations_type = + params->annotations_type | osrm::RouteParameters::AnnotationsType::Weight; } else if (annotations_str == "datasources") { - params->annotations_type = params->annotations_type | osrm::RouteParameters::AnnotationsType::Datasources; + params->annotations_type = params->annotations_type | + osrm::RouteParameters::AnnotationsType::Datasources; } else if (annotations_str == "speed") { - params->annotations_type = params->annotations_type | osrm::RouteParameters::AnnotationsType::Speed; + params->annotations_type = + params->annotations_type | osrm::RouteParameters::AnnotationsType::Speed; } else { @@ -480,7 +500,8 @@ inline bool parseCommonParameters(const v8::Local &obj, ParamType &p return false; } const Nan::Utf8String geometries_utf8str(geometries); - std::string geometries_str{*geometries_utf8str, *geometries_utf8str + geometries_utf8str.length()}; + std::string geometries_str{*geometries_utf8str, + *geometries_utf8str + geometries_utf8str.length()}; if (geometries_str == "polyline") { diff --git a/test/table.js b/test/table.js index a92ca33..fcdb5e3 100644 --- a/test/table.js +++ b/test/table.js @@ -61,7 +61,7 @@ test('table: distance table in Berlin with sources/destinations', function(asser }); test('table: throws on invalid arguments', function(assert) { - assert.plan(13); + assert.plan(14); var osrm = new OSRM(berlin_path); var options = {}; assert.throws(function() { osrm.table(options); }, @@ -109,6 +109,9 @@ test('table: throws on invalid arguments', function(assert) { options.destinations = [0, 1]; assert.doesNotThrow(function() { osrm.table(options, function(err, response) {}) }, /You can either specify sources and destinations, or coordinates/); + + assert.throws(function() { osrm.route({coordinates: [[13.43864,52.51993],[13.415852,52.513191]], generate_hints: null}, function(err, route) {}) }, + /generate_hints must be of type Boolean/); }); test('table: throws on invalid arguments', function(assert) { @@ -117,3 +120,42 @@ test('table: throws on invalid arguments', function(assert) { assert.throws(function() { osrm.table(null, function() {}); }, /First arg must be an object/); }); + +test('table: distance table in Berlin with hints', function(assert) { + assert.plan(5); + var osrm = new OSRM(berlin_path); + var options = { + coordinates: [[13.43864,52.51993],[13.415852,52.513191]], + generate_hints: true // true is default but be explicit here + }; + osrm.table(options, function(err, table) { + console.log(table); + assert.ifError(err); + + function assertHasHints(waypoint) { + assert.notStrictEqual(waypoint.hint, undefined); + } + + table.sources.map(assertHasHints); + table.destinations.map(assertHasHints); + }); +}); + +test('table: distance table in Berlin without hints', function(assert) { + assert.plan(5); + var osrm = new OSRM(berlin_path); + var options = { + coordinates: [[13.43864,52.51993],[13.415852,52.513191]], + generate_hints: false // true is default + }; + osrm.table(options, function(err, table) { + assert.ifError(err); + + function assertHasNoHints(waypoint) { + assert.strictEqual(waypoint.hint, undefined); + } + + table.sources.map(assertHasNoHints); + table.destinations.map(assertHasNoHints); + }); +});