From 9a0bfc39a45fa69c01dfa0365e34c746c5e43309 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Tue, 15 May 2018 19:39:28 +0200 Subject: [PATCH] radar: polylines option, return polylines --- docs/radar.md | 3 +++ index.js | 9 +++++++-- parse/movement.js | 27 ++++++++++++++++++--------- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/docs/radar.md b/docs/radar.md index 281b2f77..8c4fa6a8 100644 --- a/docs/radar.md +++ b/docs/radar.md @@ -11,6 +11,7 @@ With `opt`, you can override the default options, which look like this: results: 256, // maximum number of vehicles duration: 30, // compute frames for the next n seconds frames: 3, // nr of frames to compute + polylines: false // return a track shape for each vehicle? } ``` @@ -156,3 +157,5 @@ The response may look like this: } ] }, /* … */ ] ``` + +If you pass `polylines: true`, each result will have a `polyline` field, containing an encoded shape. You can use e.g. [`@mapbox/polyline`](https://www.npmjs.com/package/@mapbox/polyline) to decode it. diff --git a/index.js b/index.js index ac8f5a8e..b08a408b 100644 --- a/index.js +++ b/index.js @@ -313,7 +313,8 @@ const createClient = (profile, request = _request) => { results: 256, // maximum number of vehicles duration: 30, // compute frames for the next n seconds frames: 3, // nr of frames to compute - products: null // optionally an object of booleans + products: null, // optionally an object of booleans + polylines: false // return a track shape for each vehicle? }, opt || {}) opt.when = opt.when || new Date() @@ -339,7 +340,11 @@ const createClient = (profile, request = _request) => { .then((d) => { if (!Array.isArray(d.jnyL)) return [] - const parse = profile.parseMovement(profile, d.locations, d.lines, d.remarks) + let polylines = [] + if (opt.polylines && Array.isArray(d.common.polyL)) { + polylines = d.common.polyL + } + const parse = profile.parseMovement(profile, d.locations, d.lines, d.remarks, polylines) return d.jnyL.map(parse) }) } diff --git a/parse/movement.js b/parse/movement.js index 4a006026..5de96cd9 100644 --- a/parse/movement.js +++ b/parse/movement.js @@ -1,13 +1,12 @@ 'use strict' -const createParseMovement = (profile, locations, lines, remarks) => { +const createParseMovement = (profile, locations, lines, remarks, polylines) => { // todo: what is m.dirGeo? maybe the speed? // todo: what is m.stopL? // todo: what is m.proc? wut? // todo: what is m.pos? // todo: what is m.ani.dirGeo[n]? maybe the speed? // todo: what is m.ani.proc[n]? wut? - // todo: how does m.ani.poly work? const parseMovement = (m) => { const pStopover = profile.parseStopover(profile, locations, lines, remarks, m.date) @@ -24,13 +23,23 @@ const createParseMovement = (profile, locations, lines, remarks) => { frames: [] } - if (m.ani && Array.isArray(m.ani.mSec)) { - for (let i = 0; i < m.ani.mSec.length; i++) { - res.frames.push({ - origin: locations[m.ani.fLocX[i]] || null, - destination: locations[m.ani.tLocX[i]] || null, - t: m.ani.mSec[i] - }) + if (m.ani) { + if (Array.isArray(m.ani.mSec)) { + for (let i = 0; i < m.ani.mSec.length; i++) { + res.frames.push({ + origin: locations[m.ani.fLocX[i]] || null, + destination: locations[m.ani.tLocX[i]] || null, + t: m.ani.mSec[i] + }) + } + } + + if (m.ani.poly && m.ani.poly.crdEncYX) { + res.polyline = m.ani.poly.crdEncYX + } else if (m.ani.polyG && Array.isArray(m.ani.polyG.polyXL)) { + let p = m.ani.polyG.polyXL[0] + // todo: there can be >1 polyline + res.polyline = polylines[p] && polylines[p].crdEncXY || null } }