diff --git a/include/nodejs/node_osrm_support.hpp b/include/nodejs/node_osrm_support.hpp index e6c616239..1b6c20ce7 100644 --- a/include/nodejs/node_osrm_support.hpp +++ b/include/nodejs/node_osrm_support.hpp @@ -4,6 +4,7 @@ #include "nodejs/json_v8_renderer.hpp" #include "osrm/bearing.hpp" +#include "osrm/approach.hpp" #include "osrm/coordinate.hpp" #include "osrm/engine_config.hpp" #include "osrm/json_container.hpp" @@ -297,6 +298,63 @@ inline bool argumentsToParameter(const Nan::FunctionCallbackInfo &arg return false; } + if (obj->Has(Nan::New("approaches").ToLocalChecked())) + { + v8::Local approaches = obj->Get(Nan::New("approaches").ToLocalChecked()); + if (approaches.IsEmpty()) + return false; + + if (!approaches->IsArray()) + { + Nan::ThrowError("Approaches must be an array of arrays of numbers"); + return false; + } + + auto approaches_array = v8::Local::Cast(approaches); + + if (approaches_array->Length() != params->coordinates.size()) + { + Nan::ThrowError("Approaches array must have the same length as coordinates array"); + return false; + } + + for (uint32_t i = 0; i < approaches_array->Length(); ++i) + { + v8::Local approach_raw = approaches_array->Get(i); + if (approach_raw.IsEmpty()) + return false; + + if (approach_raw->IsNull()) + { + params->bearings.emplace_back(); + } + else if (approach_raw->IsString()) + { + const Nan::Utf8String approach_utf8str(approach_raw); + std::string approach_str{*approach_utf8str, + *approach_utf8str + approach_utf8str.length()}; + if (approach_str == "curb") + { + params->approaches.push_back(osrm::Approach::CURB); + } + else if (approach_str == "unrestricted") + { + params->approaches.push_back(osrm::Approach::UNRESTRICTED); + } + else + { + Nan::ThrowError("'approach' param must be one of [curb, unrestricted]"); + return false; + } + } + else + { + Nan::ThrowError("Approach must be a string: [curb, unrestricted] or null"); + return false; + } + } + } + if (obj->Has(Nan::New("bearings").ToLocalChecked())) { v8::Local bearings = obj->Get(Nan::New("bearings").ToLocalChecked()); diff --git a/include/osrm/approach.hpp b/include/osrm/approach.hpp new file mode 100644 index 000000000..842dc8c32 --- /dev/null +++ b/include/osrm/approach.hpp @@ -0,0 +1,38 @@ +/* + +Copyright (c) 2016, Project OSRM contributors +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +Redistributions of source code must retain the above copyright notice, this list +of conditions and the following disclaimer. +Redistributions in binary form must reproduce the above copyright notice, this +list of conditions and the following disclaimer in the documentation and/or +other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +*/ + +#ifndef OSRM_APPROACH_HPP +#define OSRM_APPROACH_HPP + +#include "engine/approach.hpp" + +namespace osrm +{ +using engine::Approach; +} + +#endif