furkot-directions
Advanced tools
Comparing version 0.0.7 to 0.0.8
0.0.8 / 2017-01-08 | ||
================== | ||
* refactor simplify route path | ||
* simplify route path after processing response | ||
* refactor OSRM service | ||
0.0.7 / 2017-01-04 | ||
@@ -3,0 +10,0 @@ ================== |
@@ -18,2 +18,3 @@ var _defaults = require('lodash.defaults'); | ||
var services = [{ | ||
name: 'osrm', | ||
service: require('./service/osrm'), | ||
@@ -20,0 +21,0 @@ skip: function (options, query, result) { |
var _defaults = require('lodash.defaults'); | ||
var partition = require('../partition'); | ||
var pathType = require("../../model").pathType; | ||
var simplify = require('../simplify'); | ||
var status = require('../status'); | ||
@@ -151,7 +150,5 @@ var util = require('../util'); | ||
if (route.overview_path && query.path === pathType.coarse) { | ||
directions.pathReady = true; | ||
partition(route.overview_path.map(fromLatLng), directions.routes, directions.segments); | ||
} | ||
else { | ||
simplify(query.path, query.span, directions.routes, directions.segments); | ||
} | ||
} | ||
@@ -174,3 +171,3 @@ } | ||
}); | ||
return require('../index')(options); | ||
return require('..')(options); | ||
} |
var _defaults = require('lodash.defaults'); | ||
var partition = require('../partition'); | ||
var pathType = require("../../model").pathType; | ||
var simplify = require('../simplify'); | ||
var status = require('../status'); | ||
@@ -123,7 +122,5 @@ var util = require('../util'); | ||
if (route.overview_polyline && route.overview_polyline.points && query.path === pathType.coarse) { | ||
directions.pathReady = true; | ||
partition(util.decode(route.overview_polyline.points), directions.routes, directions.segments); | ||
} | ||
else { | ||
simplify(query.path, query.span, directions.routes, directions.segments); | ||
} | ||
} | ||
@@ -145,3 +142,3 @@ } | ||
}); | ||
return require('../index')(options); | ||
return require('..')(options); | ||
} |
var _defaults = require('lodash.defaults'); | ||
var fetchagent = require('fetchagent'); | ||
var pathType = require("../model").pathType; | ||
var series = require('run-series'); | ||
var fetchagent = require('fetchagent'); | ||
var simplify = require('./simplify')(); | ||
var status = require('./status'); | ||
@@ -76,3 +77,3 @@ var util = require('./util'); | ||
reqInProgress = options.request(options.url(query), req, function (err, response) { | ||
var st; | ||
var st, res; | ||
reqInProgress = undefined; | ||
@@ -109,3 +110,10 @@ if (!timeoutId) { | ||
legs.result[idx] = options.processResponse(response, query); | ||
res = options.processResponse(response, query); | ||
if (!res.pathReady && res.routes && res.segments) { | ||
simplify(query.path, query.span, res.routes, res.segments); | ||
} | ||
if (!query.turnbyturn) { | ||
delete res.segments; | ||
} | ||
legs.result[idx] = res; | ||
callback(); | ||
@@ -112,0 +120,0 @@ }); |
var _defaults = require('lodash.defaults'); | ||
var pathType = require("../../model").pathType; | ||
var simplify = require('../simplify'); | ||
var status = require('../status'); | ||
@@ -43,3 +42,3 @@ var util = require('../util'); | ||
function extractSegment(result, maneuver) { | ||
var segments = result.segments, indexes = result.maneuverIndexes, path = result.path; | ||
var segments = result.directions.segments, indexes = result.maneuverIndexes, path = result.path; | ||
segments.push({ | ||
@@ -60,3 +59,3 @@ duration: maneuver.time, | ||
}; | ||
if (indexes && path && result.wantPath) { | ||
if (indexes && path && result.fullPath) { | ||
route.path = path.slice(indexes[leg.index], indexes[leg.index + 1]); | ||
@@ -67,7 +66,7 @@ } | ||
} | ||
if (result.segments) { | ||
route.segmentIndex = result.segments.length; | ||
if (directions.segments) { | ||
route.segmentIndex = directions.segments.length; | ||
} | ||
directions.routes.push(route); | ||
if (result.segments && leg.maneuvers) { | ||
if (directions.segments && leg.maneuvers) { | ||
leg.maneuvers.reduce(extractSegment, result); | ||
@@ -151,3 +150,3 @@ } | ||
function processResponse(response, query) { | ||
var directions, route, segments; | ||
var directions, route, fullPath; | ||
@@ -164,6 +163,7 @@ directions = { | ||
if (route.legs) { | ||
directions.routes = directions.routes || []; | ||
directions.routes = []; | ||
if (query.turnbyturn || query.path === pathType.smooth || query.path === pathType.coarse) { | ||
segments = segments || []; | ||
directions.segments = []; | ||
} | ||
fullPath = query.path === pathType.full; | ||
route.legs.reduce(extractDirections, { | ||
@@ -175,11 +175,8 @@ directions: directions, | ||
path: route.shape && route.shape.shapePoints.reduce(addPoint, []), | ||
wantPath: query.path === pathType.full, | ||
segments: segments | ||
fullPath: fullPath | ||
}); | ||
if (query.path !== pathType.full) { | ||
simplify(query.path, query.span, directions.routes, segments); | ||
if (fullPath) { | ||
// path is already prepared - no need to do it again from segments | ||
directions.pathReady = true; | ||
} | ||
if (query.turnbyturn) { | ||
directions.segments = segments; | ||
} | ||
} | ||
@@ -198,3 +195,3 @@ } | ||
}); | ||
return require('../index')(options); | ||
return require('..')(options); | ||
} |
var _defaults = require('lodash.defaults'); | ||
var pathType = require("../../model").pathType; | ||
var status = require('../status'); | ||
@@ -42,5 +43,7 @@ var util = require('../util'); | ||
duration: (leg.summary && leg.summary.time) || 0, | ||
distance: Math.round(((leg.summary && leg.summary.length) || 0) * result.unitMultiplier), | ||
path: result.path | ||
distance: Math.round(((leg.summary && leg.summary.length) || 0) * result.unitMultiplier) | ||
}; | ||
if (result.fullPath) { | ||
route.path = result.path; | ||
} | ||
if (directions.segments) { | ||
@@ -50,3 +53,3 @@ route.segmentIndex = directions.segments.length; | ||
directions.routes.push(route); | ||
if (directions.query.turnbyturn && leg.maneuvers) { | ||
if (directions.segments && leg.maneuvers) { | ||
leg.maneuvers.reduce(extractSegment, result); | ||
@@ -107,3 +110,3 @@ } | ||
function processResponse(response, query) { | ||
var directions, trip; | ||
var directions, trip, fullPath; | ||
@@ -116,10 +119,16 @@ directions = { | ||
if (trip && trip.legs) { | ||
directions.routes = directions.routes || []; | ||
if (query.turnbyturn) { | ||
directions.segments = directions.segments || []; | ||
directions.routes = []; | ||
if (query.turnbyturn || query.path === pathType.smooth || query.path === pathType.coarse) { | ||
directions.segments = []; | ||
} | ||
fullPath = query.path === pathType.full; | ||
trip.legs.reduce(extractDirections, { | ||
directions: directions, | ||
unitMultiplier: query.units === 'km' ? util.metersInKm : util.metersInMile | ||
unitMultiplier: query.units === 'km' ? util.metersInKm : util.metersInMile, | ||
fullPath: fullPath | ||
}); | ||
if (fullPath) { | ||
// path is already prepared - no need to do it again from segments | ||
directions.pathReady = true; | ||
} | ||
} | ||
@@ -139,3 +148,3 @@ return directions; | ||
}); | ||
return require('../index')(options); | ||
return require('..')(options); | ||
} |
var _defaults = require('lodash.defaults'); | ||
var decode = require('code42day-google-polyline').decode; | ||
var pathType = require("../../model").pathType; | ||
var status = require('../status'); | ||
var code2status = { | ||
Ok: status.success, | ||
NoRoute: status.empty, | ||
NoSegment: status.empty | ||
}; | ||
var RADIUS = 1000; // search radius for nearby roads | ||
@@ -22,68 +28,39 @@ module.exports = init; | ||
function convertRoute(route) { | ||
return route.legs.map(function(leg) { | ||
var r = { | ||
duration: leg.duration, | ||
distance: leg.distance, | ||
segments: leg.steps.map(convertStep) | ||
}; | ||
r.path = r.segments | ||
.map(function(s) { return s.path; }) | ||
.reduce(function(p1, p2) { | ||
return p1.concat(p2.slice(1)); | ||
}); | ||
return r; | ||
}); | ||
function convertPath(path, seg) { | ||
return path.concat(seg.path.slice(1)); | ||
} | ||
function processResponse(response, query) { | ||
var directions, | ||
body = response.body; | ||
directions = { | ||
query: query, | ||
provider: 'osrm', | ||
places: body.waypoints.map(convertPlace), | ||
routes: convertRoute(body.routes[0]) | ||
function convertLeg(leg) { | ||
var r = { | ||
duration: leg.duration, | ||
distance: leg.distance, | ||
segments: leg.steps.map(convertStep) | ||
}; | ||
if (query.turnbyturn) { | ||
directions.segments = []; | ||
// copy segments from route to its own table | ||
directions.routes.forEach(function(route) { | ||
route.segmentIndex = directions.segments.length; | ||
directions.segments = directions.segments.concat(route.segments); | ||
delete route.segments; | ||
}); | ||
} else { | ||
// delete segments | ||
directions.routes.forEach(function(route) { | ||
delete route.segments; | ||
}); | ||
} | ||
r.path = r.segments.reduce(convertPath, []); | ||
return directions; | ||
return r; | ||
} | ||
function convertRoute(route) { | ||
return route.legs.map(convertLeg); | ||
} | ||
// reponse codes: http://project-osrm.org/docs/v5.5.2/api/#responses | ||
function getStatus(response) { | ||
var | ||
body = response && response.body, | ||
code = body && body.code; | ||
var code = response && response.body && response.body.code; | ||
switch(code) { | ||
case 'Ok': return status.success; | ||
case 'NoRoute': return status.empty; | ||
default: return status.error; | ||
} | ||
return code2status[code] || status.error; | ||
} | ||
function point2radius() { | ||
return RADIUS; | ||
} | ||
function prepareRequest(query) { | ||
return { | ||
alternatives: query.alternate, | ||
steps: true, // allwasy asj for steps since legs do not have overview | ||
overview: false // we'll get this from steps | ||
steps: true, // always ask for steps since legs do not have overview | ||
overview: false, // we'll get this from steps | ||
radiuses: query.points.map(point2radius).join(';') | ||
}; | ||
@@ -119,2 +96,44 @@ } | ||
function init(options) { | ||
function processResponse(response, query) { | ||
var directions, | ||
body = response.body; | ||
directions = { | ||
query: query, | ||
provider: options.name | ||
}; | ||
if (body.waypoints) { | ||
directions.places = body.waypoints.map(convertPlace); | ||
} | ||
if (body.routes) { | ||
directions.routes = convertRoute(body.routes[0]); | ||
if (query.turnbyturn || query.path === pathType.smooth || query.path === pathType.coarse) { | ||
directions.segments = []; | ||
// copy segments from route to its own table | ||
directions.routes.forEach(function(route) { | ||
route.segmentIndex = directions.segments.length; | ||
directions.segments = directions.segments.concat(route.segments); | ||
delete route.segments; | ||
}); | ||
} else { | ||
// delete segments | ||
directions.routes.forEach(function(route) { | ||
delete route.segments; | ||
}); | ||
} | ||
if (query.path === pathType.full) { | ||
// path is already prepared - no need to do it again from segments | ||
directions.pathReady = true; | ||
} | ||
} | ||
return directions; | ||
} | ||
options = _defaults(options || {}, { | ||
@@ -121,0 +140,0 @@ maxPoints: 20, // max 20 points for automobile and 50 for bicycle and pedestrian |
@@ -1,39 +0,52 @@ | ||
var algorithm = require("code42day-vis-why"); | ||
var pathType = require("../model").pathType; | ||
var util = require('./util'); | ||
module.exports = simplify; | ||
module.exports = init; | ||
var endPoints = 25; // how many points keep at ends | ||
var maxPoints = 100; // maximum number of points kept per path | ||
var ed = maxPoints / endPoints; | ||
function simplify(type, endDistance, routes, segments) { | ||
if (type === pathType.none) { | ||
return; | ||
} | ||
endDistance = endDistance || 2000; | ||
routes.reduceRight(function (result, route) { | ||
var i, first = [], last = [], path = [], type = result.type, segments = result.segments, seg; | ||
function init(algorithm) { | ||
algorithm = algorithm || require("code42day-vis-why"); | ||
function simplifyRoute(result, route) { | ||
var i, seg, | ||
first = [], | ||
last = [], | ||
path = [], | ||
type = result.type, | ||
segments = result.segments, | ||
endDistance = result.endDistance; | ||
if (endDistance > route.distance / ed) { | ||
endDistance = 0; | ||
} | ||
last.distance = 0; | ||
for (i = result.segmentIndex - 1; i >= route.segmentIndex; i -= 1) { | ||
seg = segments[i]; | ||
last.distance += seg.distance; | ||
result.segmentIndex -= 1; | ||
if (last.distance >= endDistance) { | ||
last.split = util.indexAt(seg.path, last.distance - endDistance); | ||
Array.prototype.unshift.apply(last, seg.path.slice(last.split)); | ||
break; | ||
if (endDistance) { | ||
for (i = result.segmentIndex - 1; i >= route.segmentIndex; i -= 1) { | ||
seg = segments[i]; | ||
last.distance += seg.distance; | ||
if (last.distance >= endDistance) { | ||
last.split = util.indexAt(seg.path, last.distance - endDistance); | ||
Array.prototype.unshift.apply(last, seg.path.slice(last.split)); | ||
break; | ||
} | ||
result.segmentIndex -= 1; | ||
Array.prototype.unshift.apply(last, seg.path); | ||
} | ||
Array.prototype.unshift.apply(last, seg.path); | ||
} | ||
first.distance = 0; | ||
for (i = route.segmentIndex; i < result.segmentIndex; i += 1) { | ||
seg = segments[i]; | ||
first.distance += seg.distance; | ||
if (first.distance >= endDistance) { | ||
first.split = util.indexAt(seg.path, endDistance - first.distance + seg.distance); | ||
util.concat(first, seg.path.slice(0, first.split)); | ||
break; | ||
i = route.segmentIndex; | ||
if (endDistance) { | ||
for (; i < result.segmentIndex; i += 1) { | ||
seg = segments[i]; | ||
first.distance += seg.distance; | ||
if (first.distance >= endDistance) { | ||
first.split = util.indexAt(seg.path, endDistance - first.distance + seg.distance); | ||
util.concat(first, seg.path.slice(0, first.split)); | ||
break; | ||
} | ||
util.concat(first, seg.path); | ||
} | ||
util.concat(first, seg.path); | ||
} | ||
@@ -45,13 +58,18 @@ if (first.split) { | ||
} | ||
for(; i < result.segmentIndex; i += 1) { | ||
for(; i < (last.split ? result.segmentIndex - 1 : result.segmentIndex); i += 1) { | ||
util.concat(path, segments[i].path); | ||
} | ||
if (last.split) { | ||
util.concat(path, segments[i].path.slice(0, last.split)); | ||
if (last.split && i < result.segmentIndex) { | ||
util.concat(path, segments[i].path.slice(0, last.split)); | ||
} | ||
result.segmentIndex = route.segmentIndex; | ||
if (type === pathType.full || (first.length + path.length + last.length) <= maxPoints) { | ||
route.path = util.concat(util.concat(first, path), last); | ||
if (endDistance) { | ||
route.path = util.concat(util.concat(first, path), last); | ||
} | ||
else { | ||
route.path = path; | ||
} | ||
} | ||
else { | ||
else if (endDistance) { | ||
first = algorithm(first, endPoints); | ||
@@ -61,8 +79,21 @@ last = algorithm(last, endPoints); | ||
} | ||
else { | ||
route.path = algorithm(path, maxPoints); | ||
} | ||
return result; | ||
}, { | ||
type: type, | ||
segments: segments, | ||
segmentIndex: segments.length | ||
}); | ||
} | ||
function simplify(type, endDistance, routes, segments) { | ||
if (type === pathType.none) { | ||
return; | ||
} | ||
routes.reduceRight(simplifyRoute, { | ||
type: type, | ||
segments: segments, | ||
segmentIndex: segments.length, | ||
endDistance: endDistance || 2000 | ||
}); | ||
} | ||
return simplify; | ||
} |
{ | ||
"name": "furkot-directions", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"description": "Directions service for Furkot", | ||
@@ -5,0 +5,0 @@ "author": { |
82991
1308