furkot-directions
Advanced tools
Comparing version 1.1.2 to 1.2.0
1.2.0 / 2019-04-14 | ||
================== | ||
* default truck/RV routing | ||
* refactor service connectors to ES6 | ||
1.1.2 / 2018-12-15 | ||
@@ -3,0 +9,0 @@ ================== |
@@ -1,12 +0,13 @@ | ||
var pathType = require("../../model").pathType; | ||
var status = require('../status'); | ||
var util = require('../util'); | ||
const { pathType } = require("../../model"); | ||
const status = require('../status'); | ||
const util = require('../util'); | ||
module.exports = init; | ||
var vehicle = { | ||
const vehicle = { | ||
'-1': 'car', | ||
0: 'car', | ||
1: 'bike', | ||
2: 'foot' | ||
2: 'foot', | ||
5: 'truck' | ||
}; | ||
@@ -20,9 +21,9 @@ | ||
function extractSegment(result, instruction) { | ||
var segments = result.directions.segments, path = result.path; | ||
function extractSegment(result, { distance, interval, text, time }) { | ||
const { directions: { segments }, path } = result; | ||
segments.push({ | ||
duration: Math.round((instruction.time || 0) / 1000), | ||
distance: Math.round(instruction.distance || 0), | ||
path: path && path.slice(instruction.interval[0], instruction.interval[1]), | ||
instructions: instruction.text | ||
duration: Math.round((time || 0) / 1000), | ||
distance: Math.round(distance || 0), | ||
path: path && path.slice(interval[0], interval[1]), | ||
instructions: text | ||
}); | ||
@@ -32,20 +33,20 @@ return result; | ||
function extractDirections(result, path) { | ||
var directions = result.directions, route; | ||
result.path = util.decode(path.points); | ||
route = { | ||
duration: Math.round((path.time || 0) / 1000), | ||
distance: Math.round(path.distance || 0) | ||
function extractDirections(result, { distance, instructions, points, time }) { | ||
const { directions: { routes, segments }, fullPath } = result; | ||
result.path = util.decode(points); | ||
const route = { | ||
duration: Math.round((time || 0) / 1000), | ||
distance: Math.round(distance || 0) | ||
}; | ||
if (result.fullPath) { | ||
if (fullPath) { | ||
route.path = result.path; | ||
} | ||
if (directions.segments) { | ||
route.segmentIndex = directions.segments.length; | ||
if (segments) { | ||
route.segmentIndex = segments.length; | ||
} | ||
directions.routes.push(route); | ||
if (directions.segments && path.instructions) { | ||
path.instructions.reduce(extractSegment, result); | ||
if (directions.segments.length) { | ||
util.last(directions.segments).path.push(util.last(result.path)); | ||
routes.push(route); | ||
if (segments && instructions) { | ||
instructions.reduce(extractSegment, result); | ||
if (segments.length) { | ||
util.last(segments).path.push(util.last(result.path)); | ||
} | ||
@@ -78,16 +79,16 @@ } | ||
function prepareUrl(url, query) { | ||
var req = { | ||
vehicle: vehicle[query.mode] || vehicle[0], | ||
function prepareUrl(url, { avoidHighways, avoidTolls, mode, path, points, turnbyturn }) { | ||
let req = { | ||
vehicle: vehicle[mode] || vehicle[0], | ||
key: options.graphhopper_key | ||
}; | ||
if (!query.turnbyturn && query.path !== pathType.smooth && query.path !== pathType.coarse) { | ||
if (!turnbyturn && path !== pathType.smooth && path !== pathType.coarse) { | ||
req.instructions = false; | ||
} | ||
if (options.parameters.flexible) { | ||
if (query.avoidTolls) { | ||
if (avoidTolls) { | ||
req['ch.disable'] = true; | ||
req.avoid = ['toll']; | ||
} | ||
if (query.avoidHighways) { | ||
if (avoidHighways) { | ||
req['ch.disable'] = true; | ||
@@ -99,3 +100,3 @@ req.avoid = req.avoid || []; | ||
req = query.points.reduce(prepareWaypoint, Object.keys(req).map(function (name) { | ||
req = points.reduce(prepareWaypoint, Object.keys(req).map(function (name) { | ||
return name + '=' + encodeURIComponent(req[name]); | ||
@@ -112,3 +113,2 @@ })); | ||
function processResponse(response, query) { | ||
var directions, paths, fullPath; | ||
@@ -120,7 +120,7 @@ if (response && response.status >= 400) { | ||
directions = { | ||
query: query, | ||
const directions = { | ||
query, | ||
provider: options.name | ||
}; | ||
paths = response && response.paths; | ||
const paths = response && response.paths; | ||
if (paths) { | ||
@@ -131,3 +131,3 @@ directions.routes = []; | ||
} | ||
fullPath = query.path === pathType.full; | ||
const fullPath = query.path === pathType.full; | ||
paths.reduce(extractDirections, { | ||
@@ -149,4 +149,4 @@ directions: directions, | ||
status: getStatus, | ||
prepareRequest: prepareRequest, | ||
processResponse: processResponse | ||
prepareRequest, | ||
processResponse | ||
}); | ||
@@ -153,0 +153,0 @@ options.parameters = options.graphhopper_parameters || {}; |
@@ -1,18 +0,19 @@ | ||
var pathType = require("../../model").pathType; | ||
var status = require('../status'); | ||
var util = require('../util'); | ||
const { pathType } = require("../../model"); | ||
const status = require('../status'); | ||
const util = require('../util'); | ||
module.exports = init; | ||
var units = { | ||
const units = { | ||
km: 'k', | ||
m: 'm' | ||
}; | ||
var routeType = { | ||
const routeType = { | ||
'-1': 'shortest', | ||
0: 'shortest', | ||
1: 'bicycle', | ||
2: 'pedestrian' | ||
2: 'pedestrian', | ||
5: 'shortest' | ||
}; | ||
var tolerance = { | ||
const tolerance = { | ||
'-1': 20, | ||
@@ -40,9 +41,9 @@ 0: 20, | ||
function extractSegment(result, maneuver) { | ||
var segments = result.directions.segments, indexes = result.maneuverIndexes, path = result.path; | ||
function extractSegment(result, { distance, index, narrative, time }) { | ||
const { directions: { segments }, maneuverIndexes: indexes, path, unitMultiplier } = result; | ||
segments.push({ | ||
duration: maneuver.time, | ||
distance: Math.round((maneuver.distance || 0) * result.unitMultiplier), | ||
path: indexes && path && path.slice(indexes[maneuver.index], indexes[maneuver.index + 1]), | ||
instructions: maneuver.narrative | ||
duration: time, | ||
distance: Math.round((distance || 0) * unitMultiplier), | ||
path: indexes && path && path.slice(indexes[index], indexes[index + 1]), | ||
instructions: narrative | ||
}); | ||
@@ -53,4 +54,4 @@ return result; | ||
function extractDirections(result, leg) { | ||
var directions = result.directions, indexes = result.legIndexes, path = result.path, route; | ||
route = { | ||
const { directions: { routes, segments }, legIndexes: indexes, path } = result; | ||
const route = { | ||
duration: leg.time || 0, | ||
@@ -65,7 +66,7 @@ distance: Math.round((leg.distance || 0) * result.unitMultiplier) | ||
} | ||
if (directions.segments) { | ||
route.segmentIndex = directions.segments.length; | ||
if (segments) { | ||
route.segmentIndex = segments.length; | ||
} | ||
directions.routes.push(route); | ||
if (directions.segments && leg.maneuvers) { | ||
routes.push(route); | ||
if (segments && leg.maneuvers) { | ||
leg.maneuvers.reduce(extractSegment, result); | ||
@@ -77,7 +78,7 @@ } | ||
function getStatus(err, response) { | ||
var st; | ||
if (!response) { | ||
return; | ||
} | ||
st = response.info && response.info.statuscode; | ||
const { info, route } = response; | ||
const st = info && info.statuscode; | ||
if (st === 403 || st === 500) { | ||
@@ -92,3 +93,3 @@ // assume its because we exceeded the limit | ||
if (st === 0) { | ||
if (response.route) { | ||
if (route) { | ||
return status.success; | ||
@@ -115,3 +116,3 @@ } | ||
function prepareRequest(query) { | ||
var req = { | ||
const req = { | ||
unit: units[query.units] || units.m, | ||
@@ -155,6 +156,5 @@ manmaps: false, | ||
function processResponse(response, query) { | ||
var directions, route, fullPath; | ||
if (response && response.info) { | ||
var st = response.info.statuscode; | ||
const { statuscode: st } = response.info; | ||
if (st === 402 || st > 600) { | ||
@@ -166,3 +166,3 @@ // let it cascade to the next service | ||
route = response && response.route; | ||
const route = response && response.route; | ||
if (!(route && route.legs && route.legs.length)) { | ||
@@ -172,4 +172,4 @@ // shouldn't happen | ||
} | ||
directions = { | ||
query: query, | ||
const directions = { | ||
query, | ||
provider: getProvider(query) | ||
@@ -184,3 +184,3 @@ }; | ||
} | ||
fullPath = query.path === pathType.full; | ||
const fullPath = query.path === pathType.full; | ||
route.legs.reduce(extractDirections, { | ||
@@ -206,6 +206,6 @@ directions: directions, | ||
status: getStatus, | ||
prepareRequest: prepareRequest, | ||
processResponse: processResponse | ||
prepareRequest, | ||
processResponse | ||
}); | ||
return require('..')(options); | ||
} |
@@ -1,13 +0,19 @@ | ||
var pathType = require("../../model").pathType; | ||
var status = require('../status'); | ||
var util = require('../util'); | ||
const { pathType } = require("../../model"); | ||
const status = require('../status'); | ||
const util = require('../util'); | ||
module.exports = init; | ||
var profile = { | ||
const profile = { | ||
'-1': 'driving-car', | ||
0: 'driving-car', | ||
1: 'cycling-regular', | ||
2: 'foot-hiking' | ||
2: 'foot-hiking', | ||
5: 'driving-hgv' | ||
}; | ||
const profileRestrictions = { | ||
5: { | ||
hazmat: true | ||
} | ||
}; | ||
@@ -19,9 +25,9 @@ function prepareWaypoint(p) { | ||
function extractStep(result, step) { | ||
var segments = result.directions.segments, path = result.path; | ||
function extractStep(result, { distance, duration, instruction, way_points }) { | ||
const { directions: { segments }, path } = result; | ||
segments.push({ | ||
duration: Math.round(step.duration || 0), | ||
distance: Math.round(step.distance || 0), | ||
path: path && path.slice(step.way_points[0], step.way_points[1]), | ||
instructions: step.instruction | ||
duration: Math.round(duration || 0), | ||
distance: Math.round(distance || 0), | ||
path: path && path.slice(way_points[0], way_points[1]), | ||
instructions: instruction | ||
}); | ||
@@ -31,17 +37,17 @@ return result; | ||
function extractDirections(result, path, i) { | ||
var directions = result.directions, route; | ||
route = { | ||
duration: Math.round(path.duration || 0), | ||
distance: Math.round(path.distance || 0), | ||
path: result.path && result.path.slice(result.waypoints[i], result.waypoints[i + 1]) | ||
function extractDirections(result, { distance, duration, steps }, i) { | ||
const { directions: { routes, segments }, path, waypoints } = result; | ||
const route = { | ||
duration: Math.round(duration || 0), | ||
distance: Math.round(distance || 0), | ||
path: path && path.slice(waypoints[i], waypoints[i + 1]) | ||
}; | ||
if (directions.segments) { | ||
route.segmentIndex = directions.segments.length; | ||
if (segments) { | ||
route.segmentIndex = segments.length; | ||
} | ||
directions.routes.push(route); | ||
if (directions.segments && path.steps) { | ||
path.steps.reduce(extractStep, result); | ||
if (directions.segments.length) { | ||
util.last(directions.segments).path.push(util.last(route.path)); | ||
routes.push(route); | ||
if (segments && steps) { | ||
steps.reduce(extractStep, result); | ||
if (segments.length) { | ||
util.last(segments).path.push(util.last(route.path)); | ||
} | ||
@@ -62,3 +68,3 @@ } | ||
function prepareRequest(query) { | ||
var req = { | ||
const req = { | ||
api_key: options.openroute_key, | ||
@@ -71,5 +77,13 @@ profile: profile[query.mode] || profile[0], | ||
} | ||
const restrictions = profileRestrictions[query.mode]; | ||
if (restrictions) { | ||
req.options = { | ||
profile_params: { | ||
restrictions | ||
} | ||
}; | ||
} | ||
if (req.profile === 'driving-car') { | ||
if (query.avoidHighways) { | ||
req.options = { | ||
req.options = req.options || { | ||
avoid_features: ['highways'] | ||
@@ -79,3 +93,3 @@ }; | ||
if (query.avoidTolls) { | ||
req.options = req.optrions || {}; | ||
req.options = req.options || {}; | ||
req.options.avoid_features = req.options.avoid_features || []; | ||
@@ -96,3 +110,2 @@ req.options.avoid_features.push('tollways'); | ||
function processResponse(response, query) { | ||
var directions, paths, geometry, fullPath; | ||
@@ -104,8 +117,8 @@ if (!(response && response.routes && response.routes.length)) { | ||
directions = { | ||
const directions = { | ||
query: query, | ||
provider: options.name | ||
}; | ||
paths = response.routes[0].segments; | ||
geometry = response.routes[0].geometry; | ||
const paths = response.routes[0].segments; | ||
const geometry = response.routes[0].geometry; | ||
if (paths) { | ||
@@ -116,3 +129,3 @@ directions.routes = []; | ||
} | ||
fullPath = query.path === pathType.full; | ||
const fullPath = query.path === pathType.full; | ||
paths.reduce(extractDirections, { | ||
@@ -135,6 +148,6 @@ directions: directions, | ||
status: getStatus, | ||
prepareRequest: prepareRequest, | ||
processResponse: processResponse | ||
prepareRequest, | ||
processResponse | ||
}); | ||
return require('..')(options); | ||
} |
@@ -1,6 +0,6 @@ | ||
var pathType = require("../../model").pathType; | ||
var status = require('../status'); | ||
var util = require('../util'); | ||
const { pathType } = require("../../model"); | ||
const status = require('../status'); | ||
const util = require('../util'); | ||
var code2status = { | ||
const code2status = { | ||
Ok: status.success, | ||
@@ -10,3 +10,3 @@ NoRoute: status.empty, | ||
}; | ||
var RADIUS = 1000; // search radius for nearby roads | ||
const RADIUS = 1000; // search radius for nearby roads | ||
@@ -32,7 +32,7 @@ module.exports = init; | ||
function convertLeg(leg) { | ||
var r = { | ||
duration: leg.duration, | ||
distance: leg.distance, | ||
segments: leg.steps.map(convertStep) | ||
function convertLeg({ distance, duration, steps }) { | ||
const r = { | ||
duration: duration, | ||
distance: distance, | ||
segments: steps.map(convertStep) | ||
}; | ||
@@ -45,4 +45,4 @@ | ||
function convertRoute(route) { | ||
return route.legs.map(convertLeg); | ||
function convertRoute({ legs }) { | ||
return legs.map(convertLeg); | ||
} | ||
@@ -52,3 +52,3 @@ | ||
function getStatus(err, response) { | ||
var code = response && response.code; | ||
const code = response && response.code; | ||
if (!response) { | ||
@@ -73,7 +73,8 @@ return; | ||
var profile = { | ||
const profile = { | ||
'-1': 'driving', // same as car | ||
0: 'car', | ||
1: 'bicycle', | ||
2: 'foot' | ||
2: 'foot', | ||
5: 'driving' | ||
}; | ||
@@ -88,3 +89,3 @@ | ||
var path = query.points | ||
const path = query.points | ||
.map(coords2string) | ||
@@ -104,5 +105,3 @@ .join(';'); | ||
function processResponse(response, query) { | ||
var directions; | ||
directions = { | ||
const directions = { | ||
query: query, | ||
@@ -148,6 +147,6 @@ provider: options.name | ||
status: getStatus, | ||
prepareRequest: prepareRequest, | ||
processResponse: processResponse | ||
prepareRequest, | ||
processResponse | ||
}); | ||
return require('..')(options); | ||
} |
// https://github.com/valhalla/valhalla-docs | ||
var pathType = require("../../model").pathType; | ||
var status = require('../status'); | ||
var util = require('../util'); | ||
const { pathType } = require("../../model"); | ||
const status = require('../status'); | ||
const util = require('../util'); | ||
module.exports = init; | ||
var units = { | ||
const units = { | ||
km: 'kilometers', | ||
m: 'miles' | ||
}; | ||
var costing = { | ||
'-1': 'auto', | ||
0: 'auto', | ||
const costing = { | ||
1: 'bicycle', | ||
2: 'pedestrian' | ||
2: 'pedestrian', | ||
5: 'truck' | ||
}; | ||
const defaultCosting = 'auto'; | ||
const costingOptions = { | ||
5: { | ||
// default restriction for trucks/RVs | ||
hazmat: true | ||
} | ||
}; | ||
@@ -29,9 +35,9 @@ function prepareWaypoint(p) { | ||
function extractSegment(result, maneuver) { | ||
var segments = result.directions.segments, path = result.path; | ||
function extractSegment(result, { begin_shape_index, end_shape_index, instruction, length, time }) { | ||
const { directions: { segments }, unitMultiplier, path } = result; | ||
segments.push({ | ||
duration: maneuver.time, | ||
distance: Math.round((maneuver.length || 0) * result.unitMultiplier), | ||
path: path && path.slice(maneuver.begin_shape_index, maneuver.end_shape_index), | ||
instructions: maneuver.instruction | ||
duration: time, | ||
distance: Math.round((length || 0) * unitMultiplier), | ||
path: path && path.slice(begin_shape_index, end_shape_index), | ||
instructions: instruction | ||
}); | ||
@@ -42,5 +48,5 @@ return result; | ||
function extractDirections(result, leg) { | ||
var directions = result.directions, route; | ||
const { directions } = result; | ||
result.path = util.decode(leg.shape, { factor: 1e6 }); | ||
route = { | ||
const route = { | ||
duration: (leg.summary && leg.summary.time) || 0, | ||
@@ -66,3 +72,3 @@ distance: Math.round(((leg.summary && leg.summary.length) || 0) * result.unitMultiplier) | ||
function getStatus(err, response) { | ||
var st = response && response.status_code; | ||
let st = response && response.status_code; | ||
if (!response) { | ||
@@ -92,5 +98,6 @@ return; | ||
function prepareRequest(query) { | ||
var req = { | ||
let req = { | ||
locations: query.points.map(prepareWaypoint), | ||
costing: costing[query.mode] || costing[0], | ||
costing: costing[query.mode] || defaultCosting, | ||
costing_options: Object.assign({}, costingOptions[query.mode]), | ||
directions_options: { | ||
@@ -100,3 +107,2 @@ units: units[query.units] || units.m | ||
}; | ||
req.costing_options = req.costing_options || {}; | ||
req.costing_options[req.costing] = {}; | ||
@@ -133,8 +139,6 @@ if (query.avoidTolls) { | ||
function processResponse(response, query) { | ||
var directions, trip, fullPath; | ||
trip = response && response.trip; | ||
const trip = response && response.trip; | ||
if (trip && trip.legs && trip.legs.length) { | ||
directions = { | ||
query: query, | ||
const directions = { | ||
query, | ||
provider: options.name, | ||
@@ -146,7 +150,7 @@ routes: [] | ||
} | ||
fullPath = query.path === pathType.full; | ||
const fullPath = query.path === pathType.full; | ||
trip.legs.reduce(extractDirections, { | ||
directions: directions, | ||
directions, | ||
unitMultiplier: query.units !== 'km' ? util.metersInMile : util.metersInKm, | ||
fullPath: fullPath | ||
fullPath | ||
}); | ||
@@ -170,6 +174,6 @@ if (fullPath) { | ||
status: getStatus, | ||
prepareRequest: prepareRequest, | ||
processResponse: processResponse | ||
prepareRequest, | ||
processResponse | ||
}); | ||
return require('..')(options); | ||
} |
{ | ||
"name": "furkot-directions", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"description": "Directions service for Furkot", | ||
@@ -5,0 +5,0 @@ "author": { |
85740
24
1340