Merges node-osrm into repository

Build with

    cmake .. -DCMAKE_BUILD_TYPE=Release -DENABLE_NODE_BINDINGS=On -DENABLE_MASON=On
This commit is contained in:
Daniel J. Hofmann
2017-03-01 18:27:57 +01:00
committed by Patrick Niklaus
parent ff238c2724
commit 2351b5a084
53 changed files with 4871 additions and 244 deletions
+61 -108
View File
@@ -36,7 +36,7 @@ int main(int argc, const char *argv[]) try
// Routing machine with several services (such as Route, Table, Nearest, Trip, Match)
OSRM osrm{config};
// Route in monaco
// Match traces to the road network in our Berlin test dataset
MatchParameters params;
params.overview = RouteParameters::OverviewType::False;
params.steps = false;
@@ -45,170 +45,123 @@ int main(int argc, const char *argv[]) try
using osrm::util::FloatLatitude;
using osrm::util::FloatLongitude;
// Grab trace, or: go to geojson.io, create linestring.
// Extract coordinates: jq '.features[].geometry.coordinates[]' coordinates.json
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.422176599502563}, FloatLatitude{43.73754595167546}});
FloatCoordinate{FloatLongitude{13.410401344299316}, FloatLatitude{52.522749270442254}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.421715259552002}, FloatLatitude{43.73744517900973}});
FloatCoordinate{FloatLongitude{13.410615921020508}, FloatLatitude{52.52284066124772}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.421489953994752}, FloatLatitude{43.73738316497729}});
FloatCoordinate{FloatLongitude{13.410787582397461}, FloatLatitude{52.522932051863044}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.421286106109619}, FloatLatitude{43.737274640266}});
FloatCoordinate{FloatLongitude{13.411259651184082}, FloatLatitude{52.52333677944541}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.420910596847533}, FloatLatitude{43.73714285999499}});
FloatCoordinate{FloatLongitude{13.411538600921629}, FloatLatitude{52.52341511338546}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.420696020126342}, FloatLatitude{43.73699557581948}});
FloatCoordinate{FloatLongitude{13.411903381347656}, FloatLatitude{52.52374150329884}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.42049217224121}, FloatLatitude{43.73690255404829}});
FloatCoordinate{FloatLongitude{13.412246704101562}, FloatLatitude{52.523950391570665}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.420309782028198}, FloatLatitude{43.73672426191624}});
FloatCoordinate{FloatLongitude{13.410637378692625}, FloatLatitude{52.52398955801103}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.420159578323363}, FloatLatitude{43.7366622471372}});
FloatCoordinate{FloatLongitude{13.409242630004881}, FloatLatitude{52.52413316799366}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.420148849487305}, FloatLatitude{43.736623487867654}});
FloatCoordinate{FloatLongitude{13.407998085021973}, FloatLatitude{52.52448566323317}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.419934272766113}, FloatLatitude{43.73647620241466}});
FloatCoordinate{FloatLongitude{13.40705394744873}, FloatLatitude{52.52474676899426}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.419805526733398}, FloatLatitude{43.736228141885455}});
FloatCoordinate{FloatLongitude{13.406410217285156}, FloatLatitude{52.5249948180297}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.419601678848267}, FloatLatitude{43.736142870841206}});
FloatCoordinate{FloatLongitude{13.406989574432373}, FloatLatitude{52.525686736883024}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.419376373291015}, FloatLatitude{43.735956824504974}});
FloatCoordinate{FloatLongitude{13.407375812530518}, FloatLatitude{52.52628726139225}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.419247627258301}, FloatLatitude{43.73574752168583}});
FloatCoordinate{FloatLongitude{13.406217098236084}, FloatLatitude{52.52663973934549}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.419043779373169}, FloatLatitude{43.73566224995717}});
FloatCoordinate{FloatLongitude{13.405036926269531}, FloatLatitude{52.52696610529863}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.418732643127442}, FloatLatitude{43.735406434042645}});
FloatCoordinate{FloatLongitude{13.404350280761717}, FloatLatitude{52.52717497823596}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.418657541275024}, FloatLatitude{43.735321161828274}});
FloatCoordinate{FloatLongitude{13.404221534729004}, FloatLatitude{52.5265222470087}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.418593168258667}, FloatLatitude{43.73521263337983}});
FloatCoordinate{FloatLongitude{13.40383529663086}, FloatLatitude{52.526039219655445}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.418367862701416}, FloatLatitude{43.73508084857086}});
FloatCoordinate{FloatLongitude{13.402740955352783}, FloatLatitude{52.526300316181675}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.418346405029297}, FloatLatitude{43.73484828643578}});
FloatCoordinate{FloatLongitude{13.401474952697754}, FloatLatitude{52.52666584871098}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.4180567264556885}, FloatLatitude{43.734437424456566}});
FloatCoordinate{FloatLongitude{13.400874137878418}, FloatLatitude{52.527370795712564}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.417809963226318}, FloatLatitude{43.73414284243448}});
FloatCoordinate{FloatLongitude{13.400616645812988}, FloatLatitude{52.52780159108807}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.417863607406615}, FloatLatitude{43.73375523230292}});
FloatCoordinate{FloatLongitude{13.399865627288817}, FloatLatitude{52.52756661231615}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.417809963226318}, FloatLatitude{43.73386376339265}});
FloatCoordinate{FloatLongitude{13.399114608764648}, FloatLatitude{52.52744912245876}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.417895793914795}, FloatLatitude{43.73365445325776}});
FloatCoordinate{FloatLongitude{13.39802026748657}, FloatLatitude{52.527266359833675}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.418067455291747}, FloatLatitude{43.73343739012297}});
FloatCoordinate{FloatLongitude{13.398470878601072}, FloatLatitude{52.52648308282661}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.41803526878357}, FloatLatitude{43.73319706930599}});
FloatCoordinate{FloatLongitude{13.398964405059813}, FloatLatitude{52.52538647154948}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.418024539947509}, FloatLatitude{43.73295674752463}});
FloatCoordinate{FloatLongitude{13.398363590240479}, FloatLatitude{52.52542563670941}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.417906522750854}, FloatLatitude{43.73284821479115}});
FloatCoordinate{FloatLongitude{13.39780569076538}, FloatLatitude{52.525347306354654}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.417917251586914}, FloatLatitude{43.7327551865773}});
FloatCoordinate{FloatLongitude{13.397247791290283}, FloatLatitude{52.525190645226104}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.417434453964233}, FloatLatitude{43.73281720540258}});
FloatCoordinate{FloatLongitude{13.396217823028564}, FloatLatitude{52.52494259729653}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.4173808097839355}, FloatLatitude{43.73307303237796}});
FloatCoordinate{FloatLongitude{13.395531177520752}, FloatLatitude{52.52452482919627}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.41750955581665}, FloatLatitude{43.73328234454499}});
FloatCoordinate{FloatLongitude{13.39482307434082}, FloatLatitude{52.524472607904364}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.417563199996948}, FloatLatitude{43.73352266501975}});
FloatCoordinate{FloatLongitude{13.39359998703003}, FloatLatitude{52.5246814926995}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.41750955581665}, FloatLatitude{43.733770736756355}});
FloatCoordinate{FloatLongitude{13.392891883850098}, FloatLatitude{52.52490343170594}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.417466640472412}, FloatLatitude{43.73409632935116}});
FloatCoordinate{FloatLongitude{13.392398357391357}, FloatLatitude{52.5239765025348}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.417230606079102}, FloatLatitude{43.73428238146768}});
FloatCoordinate{FloatLongitude{13.391926288604736}, FloatLatitude{52.52310177678706}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.41724133491516}, FloatLatitude{43.73405756842078}});
FloatCoordinate{FloatLongitude{13.39184045791626}, FloatLatitude{52.52222703362077}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.4169838428497314}, FloatLatitude{43.73449168940785}});
FloatCoordinate{FloatLongitude{13.39184045791626}, FloatLatitude{52.521169485041774}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.41701602935791}, FloatLatitude{43.734615723397525}});
FloatCoordinate{FloatLongitude{13.39184045791626}, FloatLatitude{52.52039915585348}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.41704821586609}, FloatLatitude{43.73487929477265}});
FloatCoordinate{FloatLongitude{13.39205503463745}, FloatLatitude{52.519681040207885}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.41725206375122}, FloatLatitude{43.734949063471895}});
FloatCoordinate{FloatLongitude{13.392269611358643}, FloatLatitude{52.51900208371135}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.4173808097839355}, FloatLatitude{43.73533666587628}});
FloatCoordinate{FloatLongitude{13.392527103424072}, FloatLatitude{52.51812725890996}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.41750955581665}, FloatLatitude{43.735623490040375}});
FloatCoordinate{FloatLongitude{13.392677307128904}, FloatLatitude{52.51750050804369}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.417799234390259}, FloatLatitude{43.73577852955704}});
FloatCoordinate{FloatLongitude{13.393385410308838}, FloatLatitude{52.51735687637764}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.4180781841278085}, FloatLatitude{43.735972328388435}});
FloatCoordinate{FloatLongitude{13.394951820373535}, FloatLatitude{52.517474393230245}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.41850733757019}, FloatLatitude{43.73608860738618}});
FloatCoordinate{FloatLongitude{13.396711349487305}, FloatLatitude{52.51735687637764}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.418850660324096}, FloatLatitude{43.736228141885455}});
FloatCoordinate{FloatLongitude{13.398127555847168}, FloatLatitude{52.517696368649815}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.419086694717407}, FloatLatitude{43.73636767605958}});
FloatCoordinate{FloatLongitude{13.399629592895508}, FloatLatitude{52.51773554066627}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.419333457946777}, FloatLatitude{43.73664674343239}});
FloatCoordinate{FloatLongitude{13.400981426239014}, FloatLatitude{52.51829700239765}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.419633865356444}, FloatLatitude{43.73676302112054}});
FloatCoordinate{FloatLongitude{13.403105735778809}, FloatLatitude{52.51887151395141}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.419784069061279}, FloatLatitude{43.737096349241845}});
FloatCoordinate{FloatLongitude{13.40355634689331}, FloatLatitude{52.51966798345114}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.420030832290649}, FloatLatitude{43.73720487427631}});
FloatCoordinate{FloatLongitude{13.404908180236816}, FloatLatitude{52.52007274110608}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.419601678848267}, FloatLatitude{43.73708084564945}});
FloatCoordinate{FloatLongitude{13.40555191040039}, FloatLatitude{52.520529721073366}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.419333457946777}, FloatLatitude{43.73708084564945}});
FloatCoordinate{FloatLongitude{13.407869338989258}, FloatLatitude{52.52144366674759}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.419043779373169}, FloatLatitude{43.737158363571325}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.418915033340454}, FloatLatitude{43.737305647346446}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.41848587989807}, FloatLatitude{43.7374916894919}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.418271303176879}, FloatLatitude{43.73746843425534}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.417960166931152}, FloatLatitude{43.73744517900973}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.417885065078735}, FloatLatitude{43.737212626056944}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.417563199996948}, FloatLatitude{43.73703433484817}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.4173057079315186}, FloatLatitude{43.73692580950463}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.417144775390625}, FloatLatitude{43.7367707729584}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.416973114013672}, FloatLatitude{43.73653821738638}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.416855096817017}, FloatLatitude{43.73639868360965}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.4167799949646}, FloatLatitude{43.736142870841206}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.41675853729248}, FloatLatitude{43.735848297208605}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.416619062423706}, FloatLatitude{43.73567000193752}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.416543960571288}, FloatLatitude{43.735406434042645}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.416479587554932}, FloatLatitude{43.73529790574875}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.416415214538574}, FloatLatitude{43.73515061703527}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.416350841522218}, FloatLatitude{43.73490255101476}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.416340112686156}, FloatLatitude{43.73475526132885}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.416222095489501}, FloatLatitude{43.73446068087028}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.416243553161621}, FloatLatitude{43.73430563794159}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.416050434112548}, FloatLatitude{43.73403431185051}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.415814399719239}, FloatLatitude{43.73382500231174}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.415750026702881}, FloatLatitude{43.73354592178871}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.415513992309569}, FloatLatitude{43.73347615145474}});
params.coordinates.push_back(
FloatCoordinate{FloatLongitude{7.415342330932617}, FloatLatitude{43.733251335381205}});
FloatCoordinate{FloatLongitude{13.408942222595215}, FloatLatitude{52.52203119321206}});
TIMER_START(routes);
auto NUM = 100;
+54
View File
@@ -0,0 +1,54 @@
# node-cmake requires CMake 3.1 features; for the osrm project we only
# require CMake 2.8.11 so that we can build e.g. on Trusty by default.
cmake_minimum_required(VERSION 3.1)
message(STATUS "Building node-osrm")
set(BINDING_DIR "${PROJECT_SOURCE_DIR}/lib/binding")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/nodejs")
include(FindNodeJS)
set(NodeJS_CXX_STANDARD 14 CACHE INTERNAL "Use C++14" FORCE)
set(NodeJS_DOWNLOAD ON CACHE INTERNAL "Download node.js sources" FORCE)
set(NodeJS_USE_CLANG_STDLIB OFF CACHE BOOL "Don't use libc++ by default" FORCE)
find_package(NodeJS REQUIRED)
add_nodejs_module(node-osrm node_osrm.cpp)
target_link_libraries(node-osrm osrm)
# node-osrm artifacts in ${BINDING_DIR} to depend targets on
set(ARTIFACTS "")
set(OSRM_BINARIES osrm-extract osrm-contract osrm-routed osrm-datastore osrm-components)
foreach(binary ${OSRM_BINARIES})
add_custom_command(OUTPUT ${BINDING_DIR}/${binary}
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${binary}> ${BINDING_DIR}
DEPENDS ${binary} ${BINDING_DIR})
list(APPEND ARTIFACTS "${BINDING_DIR}/${binary}")
endforeach(binary)
# For mason-enabled builds we copy over tbb's shared objects for packaging.
# TODO: consider using statically linked tbb library (for node-osrm only!)
if (ENABLE_MASON)
foreach(libpath ${MASON_PACKAGE_tbb_LIBRARY_DIRS})
file(GLOB TBBGlob ${libpath}/*.*)
foreach(filepath ${TBBGlob})
get_filename_component(filename ${filepath} NAME)
add_custom_command(OUTPUT "${BINDING_DIR}/${filename}"
COMMAND ${CMAKE_COMMAND} -E copy ${filepath} ${BINDING_DIR}
DEPENDS ${filepath} ${BINDING_DIR})
list(APPEND ARTIFACTS "${BINDING_DIR}/${filename}")
endforeach()
endforeach()
endif()
add_custom_command(OUTPUT ${BINDING_DIR}/node-osrm.node
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:node-osrm> ${BINDING_DIR}
DEPENDS node-osrm ${BINDING_DIR})
list(APPEND ARTIFACTS "${BINDING_DIR}/node-osrm.node")
message(STATUS "node-osrm artifacts will be copied to: ${BINDING_DIR}")
add_custom_target(copy_artifacts ALL DEPENDS ${ARTIFACTS})
+507
View File
@@ -0,0 +1,507 @@
#include "osrm/engine_config.hpp"
#include "osrm/osrm.hpp"
#include "osrm/match_parameters.hpp"
#include "osrm/nearest_parameters.hpp"
#include "osrm/route_parameters.hpp"
#include "osrm/table_parameters.hpp"
#include "osrm/tile_parameters.hpp"
#include "osrm/trip_parameters.hpp"
#include <exception>
#include <type_traits>
#include <utility>
#include "nodejs/node_osrm.hpp"
#include "nodejs/node_osrm_support.hpp"
namespace node_osrm
{
Engine::Engine(osrm::EngineConfig &config) : Base(), this_(std::make_shared<osrm::OSRM>(config)) {}
Nan::Persistent<v8::Function> &Engine::constructor()
{
static Nan::Persistent<v8::Function> init;
return init;
}
NAN_MODULE_INIT(Engine::Init)
{
const auto whoami = Nan::New("OSRM").ToLocalChecked();
auto fnTp = Nan::New<v8::FunctionTemplate>(New);
fnTp->InstanceTemplate()->SetInternalFieldCount(1);
fnTp->SetClassName(whoami);
SetPrototypeMethod(fnTp, "route", route);
SetPrototypeMethod(fnTp, "nearest", nearest);
SetPrototypeMethod(fnTp, "table", table);
SetPrototypeMethod(fnTp, "tile", tile);
SetPrototypeMethod(fnTp, "match", match);
SetPrototypeMethod(fnTp, "trip", trip);
const auto fn = Nan::GetFunction(fnTp).ToLocalChecked();
constructor().Reset(fn);
Nan::Set(target, whoami, fn);
}
/**
*
* The `OSRM` method is the main constructor for creating an OSRM instance. An OSRM instance
* requires a `.osrm` network,
* which is prepared by the OSRM Backend C++ library. Once you have a complete `network.osrm` file,
* you can calculate
* networks in javascript with this library using the methods below. To create an OSRM instance with
* your network
* you need to construct an instance like this:
*
* ```javascript
* var osrm = new OSRM('network.osrm');
* ```
*
* #### Methods
*
* | Service | Description |
* |-----------------------------|-----------------------------------------------------------|
* | [`osrm.route`](#route) | shortest path between given coordinates |
* | [`osrm.nearest`](#nearest) | returns the nearest street segment for a given coordinate |
* | [`osrm.table`](#table) | computes distance tables for given coordinates |
* | [`osrm.match`](#match) | matches given coordinates to the road network |
* | [`osrm.trip`](#trip) | computes the shortest trip between given coordinates |
* | [`osrm.tile`](#tile) | Return vector tiles containing debugging info |
*
* #### General Options
*
* 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`
* |
*
* @class OSRM
*
*/
NAN_METHOD(Engine::New)
{
if (info.IsConstructCall())
{
try
{
auto config = argumentsToEngineConfig(info);
if (!config)
return;
auto *const self = new Engine(*config);
self->Wrap(info.This());
}
catch (const std::exception &ex)
{
return Nan::ThrowTypeError(ex.what());
}
info.GetReturnValue().Set(info.This());
}
else
{
return Nan::ThrowTypeError(
"Cannot call constructor as function, you need to use 'new' keyword");
}
}
template <typename ParameterParser, typename ServiceMemFn>
inline void async(const Nan::FunctionCallbackInfo<v8::Value> &info,
ParameterParser argsToParams,
ServiceMemFn service,
bool requires_multiple_coordinates)
{
auto params = argsToParams(info, requires_multiple_coordinates);
if (!params)
return;
BOOST_ASSERT(params->IsValid());
if (!info[info.Length() - 1]->IsFunction())
return Nan::ThrowTypeError("last argument must be a callback function");
auto *const self = Nan::ObjectWrap::Unwrap<Engine>(info.Holder());
using ParamPtr = decltype(params);
struct Worker final : Nan::AsyncWorker
{
using Base = Nan::AsyncWorker;
Worker(std::shared_ptr<osrm::OSRM> osrm_,
ParamPtr params_,
ServiceMemFn service,
Nan::Callback *callback)
: Base(callback), osrm{std::move(osrm_)}, service{std::move(service)},
params{std::move(params_)}
{
}
void Execute() override try
{
const auto status = ((*osrm).*(service))(*params, result);
ParseResult(status, result);
}
catch (const std::exception &e)
{
SetErrorMessage(e.what());
}
void HandleOKCallback() override
{
Nan::HandleScope scope;
const constexpr auto argc = 2u;
v8::Local<v8::Value> argv[argc] = {Nan::Null(), render(result)};
callback->Call(argc, argv);
}
// Keeps the OSRM object alive even after shutdown until we're done with callback
std::shared_ptr<osrm::OSRM> osrm;
ServiceMemFn service;
const ParamPtr params;
// All services return json::Object .. except for Tile!
using ObjectOrString =
typename std::conditional<std::is_same<ParamPtr, tile_parameters_ptr>::value,
std::string,
osrm::json::Object>::type;
ObjectOrString result;
};
auto *callback = new Nan::Callback{info[info.Length() - 1].As<v8::Function>()};
Nan::AsyncQueueWorker(new Worker{self->this_, std::move(params), service, callback});
}
/**
* Returns the fastest route between two or more coordinates while visiting the waypoints in order.
*
* @name route
* @memberof OSRM
* @param {Object} options Object literal containing parameters for the route query.
* @param {Boolean} [options.alternatives=false] Search for alternative routes and return as well.
* *Please note that even if an alternative route is requested, a result cannot be guaranteed.*
* @param {Boolean} [options.steps=false] Return route steps for each route leg.
* @param {Boolean} or {Array} [options.annotations=false] Return annotations for each route leg.
* Can be `false`, `true` or an array with strings of `duration`, `nodes`, `distance`, `weight`,
* `datasources`, `speed`.
* @param {String} [options.geometries=polyline] Returned route geometry format (influences overview
* and per step). Can also be `geojson`.
* @param {String} [options.overview=simplified] Add overview geometry either `full`, `simplified`
* according to highest zoom level it could be display on, or not at all (`false`).
* @param {Boolean} [options.continue_straight] Forces the route to keep going straight at waypoints
* and don't do a uturn even if it would be faster. Default value depends on the profile.
* `null`/`true`/`false`
* @param {Function} callback
*
* @returns {Object} An array of [Waypoint](#waypoint) objects representing all waypoints in order
* AND an array of [`Route`](#route) objects ordered by descending recommendation rank.
*
* @example
* var osrm = new OSRM("berlin-latest.osrm");
* osrm.route({coordinates: [[52.519930,13.438640], [52.513191,13.415852]]}, function(err, result) {
* if(err) throw err;
* console.log(result.waypoints); // array of Waypoint objects representing all waypoints in order
* console.log(result.routes); // array of Route objects ordered by descending recommendation rank
* });
*/
NAN_METHOD(Engine::route) //
{
async(info, &argumentsToRouteParameter, &osrm::OSRM::Route, true);
}
/**
* Snaps a coordinate to the street network and returns the nearest n matches.
*
* Note: `coordinates` in the general options only supports a single `{longitude},{latitude}` entry.
*
* @name nearest
* @memberof OSRM
* @param {Object} options - Object literal containing parameters for the nearest query.
* @param {Number} [options.number=1] Number of nearest segments that should be returned.
* Must be an integer greater than or equal to `1`.
* @param {Function} callback
*
* @returns {Object} containing `waypoints`.
* **`waypoints`**: array of [`Ẁaypoint`](#waypoint) objects sorted by distance to the input
* coordinate.
* Each object has an additional `distance` property, which is the distance in meters to the
* supplied
* input coordinate.
*
* @example
* var osrm = new OSRM('network.osrm');
* var options = {
* coordinates: [[13.388860,52.517037]],
* number: 3,
* bearings: [[0,20]]
* };
* osrm.nearest(options, function(err, response) {
* console.log(response.waypoints); // array of Waypoint objects
* });
*/
NAN_METHOD(Engine::nearest) //
{
async(info, &argumentsToNearestParameter, &osrm::OSRM::Nearest, false);
}
/**
* Computes duration tables for the given locations. Allows for both symmetric and asymmetric
* tables.
*
* @name table
* @memberof OSRM
* @param {Object} options - Object literal containing parameters for the table query.
* @param {Array} [options.sources] An array of `index` elements (`0 <= integer < #coordinates`) to
* use
* location with given index as source. Default is to use all.
* @param {Array} [options.destinations] An array of `index` elements (`0 <= integer <
* #coordinates`) to use location with given index as destination. Default is to use all.
* @param {Function} callback
*
* @returns {Object} containing `durations`, `sources`, and `destinations`.
* **`durations`**: array of arrays that stores the matrix in row-major order. `durations[i][j]`
* gives the travel time from the i-th waypoint to the j-th waypoint. Values are given in seconds.
* **`sources`**: array of [`Ẁaypoint`](#waypoint) objects describing all sources in order.
* **`destinations`**: array of [`Ẁaypoint`](#waypoint) objects describing all destinations in
* order.
*
* @example
* var osrm = new OSRM('network.osrm');
* var options = {
* coordinates: [
* [13.388860,52.517037],
* [13.397634,52.529407],
* [13.428555,52.523219]
* ]
* };
* osrm.table(options, function(err, response) {
* console.log(response.durations); // array of arrays, matrix in row-major order
* console.log(response.sources); // array of Waypoint objects
* console.log(response.destinations); // array of Waypoint objects
* });
*/
NAN_METHOD(Engine::table) //
{
async(info, &argumentsToTableParameter, &osrm::OSRM::Table, true);
}
/**
* This generates [Mapbox Vector Tiles](https://mapbox.com/vector-tiles) that can be viewed with a
* vector-tile capable slippy-map viewer. The tiles contain road geometries and metadata that can
* be used to examine the routing graph. The tiles are generated directly from the data in-memory,
* so are in sync with actual routing results, and let you examine which roads are actually
* routable,
* and what weights they have applied.
*
* @name tile
* @memberof OSRM
* @param {Array} ZXY - an array consisting of `x`, `y`, and `z` values representing tile
* coordinates like
* [wiki.openstreetmap.org/wiki/Slippy_map_tilenames](https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames)
* and are supported by vector tile viewers like [Mapbox GL
* JS](https://www.mapbox.com/mapbox-gl-js/api/.
* @param {Function} callback
*
* @returns {Buffer} contains a Protocol Buffer encoded vector tile.
*
* @example
* var osrm = new OSRM('network.osrm');
* osrm.tile([0, 0, 0], function(err, response) {
* if (err) throw err;
* fs.writeFileSync('./tile.vector.pbf', response); // write the buffer to a file
* });
*/
NAN_METHOD(Engine::tile)
{
async(info, &argumentsToTileParameters, &osrm::OSRM::Tile, {/*unused*/});
}
/**
* Map matching matches given GPS points to the road network in the most plausible way.
* Please note the request might result multiple sub-traces. Large jumps in the timestamps
* (>60s) or improbable transitions lead to trace splits if a complete matching could
* not be found. The algorithm might not be able to match all points. Outliers are removed
* if they can not be matched successfully.
*
* @name match
* @memberof OSRM
* @param {Object} options - Object literal containing parameters for the match query.
* @param {Boolean} [options.steps=false] Return route steps for each route.
* @param {Boolean} or {Array} [options.annotations=false] Return annotations for each route leg.
* Can be `false`, `true` or an array with strings of `duration`, `nodes`, `distance`, `weight`,
* `datasources`, `speed`.
* @param {String} [options.geometries=polyline] Returned route geometry format (influences overview
* and per step). Can also be `geojson`.
* @param {String} [options.overview=simplified] Add overview geometry either `full`, `simplified`
* according to highest zoom level it could be display on, or not at all (`false`).
* @param {Array<Number>} [options.timestamps] Timestamp of the input location (integers, UNIX-like
* timestamp).
* @param {Array} [options.radiuses] Standard deviation of GPS precision used for map matching.
* If applicable use GPS accuracy (`double >= 0`, default `5m`).
* @param {Function} callback
*
* @returns {Object} containing `tracepoints` and `matchings`.
* **`tracepoints`** Array of [`Ẁaypoint`](#waypoint) objects representing all points of the trace
* in order.
* If the trace point was ommited by map matching because it is an outlier, the entry will be null.
* Each
* `Waypoint` object includes two additional properties, 1) `matchings_index`: Index to the
* [`Route`](#route) object in matchings the sub-trace was matched to, 2) `waypoint_index`: Index of
* the waypoint inside the matched route.
* **`matchings`** is an array of [`Route`](#route) objects that
* assemble the trace. Each `Route` object has an additional `confidence` property, which is the
* confidence of
* the matching. float value between `0` and `1`. `1` is very confident that the matching is
* correct.
*
* @example
* var osrm = new OSRM('network.osrm');
* var options = {
* coordinates: [[13.393252,52.542648],[13.39478,52.543079],[13.397389,52.542107]],
* timestamps: [1424684612, 1424684616, 1424684620]
* };
* osrm.match(options, function(err, response) {
* if (err) throw err;
* console.log(response.tracepoints); // array of Waypoint objects
* console.log(response.matchings); // array of Route objects
* });
*
*/
NAN_METHOD(Engine::match) //
{
async(info, &argumentsToMatchParameter, &osrm::OSRM::Match, true);
}
/**
* The trip plugin solves the Traveling Salesman Problem using a greedy heuristic
* (farthest-insertion algorithm) for 10 or * more waypoints and uses brute force for less than 10
* waypoints. The returned path does not have to be the shortest path, * as TSP is NP-hard it is
* only an approximation.
* Note that all input coordinates have to be connected for the trip service to work.
*
* @name trip
* @memberof OSRM
* @param {Object} options - Object literal containing parameters for the trip query.
* @param {Boolean} [options.steps=false] Return route steps for each route.
* @param {Boolean} or {Array} [options.annotations=false] Return annotations for each route leg.
* Can be `false`, `true` or an array with strings of `duration`, `nodes`, `distance`, `weight`,
* `datasources`, `speed`.
* @param {String} [options.geometries=polyline] Returned route geometry format (influences overview
* and per step). Can also be `geojson`.
* @param {String} [options.overview=simplified] Add overview geometry either `full`, `simplified`
* @param {Function} callback
* @param {Boolean} [options.roundtrip=true] Return route is a roundtrip.
* @param {String} [options.source=any] Return route starts at `any` or `first` coordinate.
* @param {String} [options.destination=any] Return route ends at `any` or `last` coordinate.
*
* @returns {Object} containing `waypoints` and `trips`.
* **`waypoints`**: an array of [`Waypoint`](#waypoint) objects representing all waypoints in input
* order.
* Each Waypoint object has the following additional properties, 1) `trips_index`: index to trips of
* the
* sub-trip the point was matched to, and 2) `waypoint_index`: index of the point in the trip.
* **`trips`**: an array of [`Route`](#route) objects that assemble the trace.
*
* @example
* var osrm = new OSRM('network.osrm');
* var options = {
* coordinates: [
* [13.36761474609375, 52.51663871100423],
* [13.374481201171875, 52.506191342034576]
* ]
* }
* osrm.trip(options, function(err, response) {
* if (err) throw err;
* console.log(response.waypoints); // array of Waypoint objects
* console.log(response.trips); // array of Route objects
* });
*/
NAN_METHOD(Engine::trip) //
{
async(info, &argumentsToTripParameter, &osrm::OSRM::Trip, true);
}
/**
* Responses
* @class Responses
*/
/**
* Represents a route through (potentially multiple) waypoints.
*
* @name Route
* @memberof Responses
*
* @param {documentation} exteral in
* [`osrm-backend`](https://github.com/Project-OSRM/osrm-backend/blob/master/docs/http.md#route)
*
*/
/**
* Represents a route between two waypoints.
*
* @name RouteLeg
* @memberof Responses
*
* @param {documentation} exteral in
* [`osrm-backend`](https://github.com/Project-OSRM/osrm-backend/blob/master/docs/http.md#routeleg)
*
*/
/**
* A step consists of a maneuver such as a turn or merge, followed by a distance of travel along a
* single way to the subsequent step.
*
* @name RouteStep
* @memberof Responses
*
* @param {documentation} exteral in
* [`osrm-backend`](https://github.com/Project-OSRM/osrm-backend/blob/master/docs/http.md#routestep)
*
*/
/**
*
* @name StepManeuver
* @memberof Responses
*
* @param {documentation} exteral in
* [`osrm-backend`](https://github.com/Project-OSRM/osrm-backend/blob/master/docs/http.md#stepmaneuver)
*
*/
/**
* Object used to describe waypoint on a route.
*
* @name Waypoint
* @memberof Responses
*
* @param {documentation} exteral in
* [`osrm-backend`](https://github.com/Project-OSRM/osrm-backend/blob/master/docs/http.md#waypoint)
*
*/
} // namespace node_osrm