From 7f5acf6a19ccb83ed7f7ae304d56df8a6e23778e Mon Sep 17 00:00:00 2001 From: Siarhei Fedartsou Date: Mon, 17 Oct 2022 21:36:19 +0200 Subject: [PATCH] wip --- features/step_definitions/distance_matrix.js | 1 + features/support/shared_steps.js | 2 +- include/nodejs/node_osrm_support.hpp | 1 + server/server.js | 97 +++++++++++--- server/server.ts | 130 +++++++++++++++---- 5 files changed, 184 insertions(+), 47 deletions(-) diff --git a/features/step_definitions/distance_matrix.js b/features/step_definitions/distance_matrix.js index 37de07a269d..8fff203972e 100644 --- a/features/step_definitions/distance_matrix.js +++ b/features/step_definitions/distance_matrix.js @@ -98,6 +98,7 @@ function tableParse(table, noRoute, annotation, format, callback) { } } else { //flatbuffers var body = response.body; + console.log(body) var bytes = new Uint8Array(body.length); for (var indx = 0; indx < body.length; ++indx) { bytes[indx] = body.charCodeAt(indx); diff --git a/features/support/shared_steps.js b/features/support/shared_steps.js index fbf679e8e32..3065b150e25 100644 --- a/features/support/shared_steps.js +++ b/features/support/shared_steps.js @@ -178,7 +178,7 @@ module.exports = function () { if (whitelist.indexOf(a_type) == -1) return cb(new Error('Unrecognized annotation field', a_type)); if (annotation && !annotation[a_type]) - return cb(new Error('Annotation not found in response', a_type)); + return cb(new Error('Annotation not found in response ' + a_type)); got[k] = annotation && annotation[a_type] || ''; } else if (k.match(/^am:/)) { let a_type = k.slice(3); diff --git a/include/nodejs/node_osrm_support.hpp b/include/nodejs/node_osrm_support.hpp index f1e288226e1..5940f949afd 100644 --- a/include/nodejs/node_osrm_support.hpp +++ b/include/nodejs/node_osrm_support.hpp @@ -799,6 +799,7 @@ inline bool parseCommonParameters(const v8::Local &obj, ParamType &p if (annotations->IsBoolean()) { params->annotations = Nan::To(annotations).FromJust(); + params->annotations_type = params->annotations ? osrm::RouteParameters::AnnotationsType::All : osrm::RouteParameters::AnnotationsType::None; } else if (annotations->IsArray()) { diff --git a/server/server.js b/server/server.js index 5d6962fb772..6567ec516c6 100755 --- a/server/server.js +++ b/server/server.js @@ -84,13 +84,26 @@ async function handleTable(osrm, coordinates, query) { const options = { coordinates: coordinates }; + handleCommonParams(query, options); + if (query.scale_factor) { + options.scale_factor = parseFloat(query.scale_factor); + } + if (query.fallback_coordinate) { + options.fallback_coordinate = query.fallback_coordinate; + } + if (query.fallback_speed) { + options.fallback_speed = parseFloat(query.fallback_speed); + } + if (query.sources) { + options.sources = query.sources.split(';').map((t) => parseInt(t)); + } + if (query.destinations) { + options.destinations = query.destinations.split(';').map((t) => parseInt(t)); + } const res = await table(osrm, options); return res; } -async function handleMatch(osrm, coordinates, query) { - const options = { - coordinates: coordinates, - }; +function handleCommonParams(query, options) { if (query.overview) { options.overview = query.overview; } @@ -103,18 +116,9 @@ async function handleMatch(osrm, coordinates, query) { if (query.waypoints) { options.waypoints = query.waypoints.split(';').map((t) => parseInt(t)); } - if (query.tidy) { - options.tidy = query.tidy == 'true' ? true : false; - } - if (query.gaps) { - options.gaps = query.gaps; - } if (query.steps) { options.steps = query.steps === 'true' ? true : false; } - if (query.generate_hints) { - options.generate_hints = query.generate_hints == 'true' ? true : false; - } if (query.annotations) { let annotations; if (query.annotations === 'true') { @@ -125,24 +129,81 @@ async function handleMatch(osrm, coordinates, query) { } options.annotations = annotations; } + if (query.exclude) { + options.exclude = query.exclude.split(','); + } + if (query.snapping) { + options.snapping = query.snapping; + } + if (query.radiuses) { + options.radiuses = query.radiuses.split(';').map((t) => { + if (t === 'unlimited') { + return null; + } + return parseFloat(t); + }); + } + if (query.bearings) { + options.bearings = query.bearings.split(';').map((bearingWithRange) => { + if (bearingWithRange === '') { + return null; + } + return bearingWithRange.split(',').map((t) => parseFloat(t)); + }); + } + if (query.hints) { + options.hints = query.hints.split(';'); + } + if (query.generate_hints) { + options.generate_hints = query.generate_hints == 'true' ? true : false; + } + if (query.skip_waypoints) { + options.skip_waypoints = query.skip_waypoints === 'true' ? true : false; + } +} +async function handleMatch(osrm, coordinates, query) { + const options = { + coordinates: coordinates, + }; + handleCommonParams(query, options); + if (query.gaps) { + options.gaps = query.gaps; + } + if (query.tidy) { + options.tidy = query.tidy === 'true' ? true : false; + } //throw new Error(`not implemented ${JSON.stringify(options)}`); const res = await match(osrm, options); return res; } async function handleTrip(osrm, coordinates, query) { const options = { - coordinates: coordinates, - steps: query.steps === 'true' ? true : false, + coordinates: coordinates }; + handleCommonParams(query, options); + if (query.roundtrip) { + options.roundtrip = query.roundtrip === 'true' ? true : false; + } + if (query.source) { + options.source = query.source; + } + if (query.destination) { + options.destination = query.destination; + } const res = await trip(osrm, options); return res; } async function handleRoute(osrm, coordinates, query) { const options = { - coordinates: coordinates, - steps: query.steps === 'true' ? true : false, - alternatives: query.alternatives === 'true' ? true : false, + coordinates: coordinates }; + handleCommonParams(query, options); + if (query.alternatives) { + options.alternatives = query.alternatives === 'true' ? true : false; + } + if (query.approaches) { + options.approaches = query.approaches.split(';'); + } const res = await route(osrm, options); return res; } diff --git a/server/server.ts b/server/server.ts index a1143a554f4..f68886e9ebd 100644 --- a/server/server.ts +++ b/server/server.ts @@ -1,5 +1,6 @@ #!/usr/bin/env node import Fastify from 'fastify' +import { option } from 'yargs'; import yargs from 'yargs/yargs'; const OSRM = require('../lib/index.js') @@ -80,21 +81,30 @@ async function handleNearest(osrm: any, coordinates: [number, number][], query: async function handleTable(osrm: any, coordinates: [number, number][], query: any): Promise { - const options = { + const options: any = { coordinates: coordinates }; + handleCommonParams(query, options); + if (query.scale_factor) { + options.scale_factor = parseFloat(query.scale_factor); + } + if (query.fallback_coordinate) { + options.fallback_coordinate = query.fallback_coordinate; + } + if (query.fallback_speed) { + options.fallback_speed = parseFloat(query.fallback_speed); + } + if (query.sources) { + options.sources = query.sources.split(';').map((t: string) => parseInt(t)); + } + if (query.destinations) { + options.destinations = query.destinations.split(';').map((t: string) => parseInt(t)); + } const res = await table(osrm, options); return res; } -async function handleMatch(osrm: any, coordinates: [number, number][], query: any): Promise { - - - const options: any = { - coordinates: coordinates, - - }; - +function handleCommonParams(query: any, options: any) { if (query.overview) { options.overview = query.overview; } @@ -111,22 +121,11 @@ async function handleMatch(osrm: any, coordinates: [number, number][], query: an options.waypoints = query.waypoints.split(';').map((t: string) => parseInt(t)); } - if (query.tidy) { - options.tidy = query.tidy == 'true' ? true : false; - } - - if (query.gaps) { - options.gaps = query.gaps; - } - if (query.steps) { options.steps = query.steps === 'true' ? true : false; } - if (query.generate_hints) { - options.generate_hints = query.generate_hints == 'true' ? true : false; - } - + if (query.annotations) { let annotations; if (query.annotations === 'true') { @@ -136,26 +135,100 @@ async function handleMatch(osrm: any, coordinates: [number, number][], query: an } options.annotations = annotations; } + + if (query.exclude) { + options.exclude = query.exclude.split(','); + } + + if (query.snapping) { + options.snapping = query.snapping; + } + + if (query.radiuses) { + options.radiuses = query.radiuses.split(';').map((t: string) => { + if (t === 'unlimited') { + return null; + } + return parseFloat(t); + }); + } + + if (query.bearings) { + options.bearings = query.bearings.split(';').map((bearingWithRange: string) => { + if (bearingWithRange === '') { + return null; + } + return bearingWithRange.split(',').map((t: string) => parseFloat(t)); + }); + } + + if (query.hints) { + options.hints = query.hints.split(';'); + } + + if (query.generate_hints) { + options.generate_hints = query.generate_hints == 'true' ? true : false; + } + + if (query.skip_waypoints) { + options.skip_waypoints = query.skip_waypoints === 'true' ? true : false; + } +} + +async function handleMatch(osrm: any, coordinates: [number, number][], query: any): Promise { + + + const options: any = { + coordinates: coordinates, + + }; + + handleCommonParams(query, options); + if (query.gaps) { + options.gaps = query.gaps; + } + + if (query.tidy) { + options.tidy = query.tidy === 'true' ? true : false; + } + + + + //throw new Error(`not implemented ${JSON.stringify(options)}`); const res = await match(osrm, options); return res; } async function handleTrip(osrm: any, coordinates: [number, number][], query: any): Promise { - const options = { - coordinates: coordinates, - steps: query.steps === 'true' ? true : false, + const options: any = { + coordinates: coordinates }; + handleCommonParams(query, options); + if (query.roundtrip) { + options.roundtrip = query.roundtrip === 'true' ? true : false; + } + if (query.source) { + options.source = query.source; + } + if (query.destination) { + options.destination = query.destination; + } const res = await trip(osrm, options); return res; } async function handleRoute(osrm: any, coordinates: [number, number][], query: any): Promise { - const options = { - coordinates: coordinates, - steps: query.steps === 'true' ? true : false, - alternatives: query.alternatives === 'true' ? true : false, + const options: any = { + coordinates: coordinates }; + handleCommonParams(query, options); + if (query.alternatives) { + options.alternatives = query.alternatives === 'true' ? true : false; + } + if (query.approaches) { + options.approaches = query.approaches.split(';'); + } const res = await route(osrm, options); return res; } @@ -196,6 +269,7 @@ async function main() { // TODO: validation fastify.get('/:service(route|nearest|table|match|trip|tile)/v1/:profile/:coordinates', async (request, reply) => { const { service, profile, coordinates } = request.params as any; + const query = request.query as any; const parsedCoordinates = coordinates.split(';').map((c: string) => c.split(',').map((n: string) => parseFloat(n))); const handlers: Map = new Map();