diff --git a/docs/changelog.md b/docs/changelog.md index 135c5d70..3bc5021a 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -1,5 +1,11 @@ # Changelog +## `2.7.0` + +- `journeys()`: `polylines` option +- `journeyLeg()`: `polyline` option +- `radar()`: `polylines` option + ## `2.6.0` - 5d10d76 journey legs: parse cycle diff --git a/docs/radar.md b/docs/radar.md index 0090b577..ab00b767 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? } ``` @@ -161,3 +162,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 d8cc7734..6d5c082e 100644 --- a/index.js +++ b/index.js @@ -321,7 +321,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() @@ -347,7 +348,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 && d.common && 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/package.json b/package.json index 9045e677..de06f070 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "hafas-client", "description": "JavaScript client for HAFAS public transport APIs.", - "version": "2.6.0", + "version": "2.7.2", "main": "index.js", "files": [ "index.js", diff --git a/parse/movement.js b/parse/movement.js index 4a006026..7999eb52 100644 --- a/parse/movement.js +++ b/parse/movement.js @@ -1,18 +1,18 @@ '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) const res = { direction: profile.parseStationName(m.dirTxt), + journeyId: m.jid || null, trip: m.jid && +m.jid.split('|')[1] || null, // todo: this seems brittle line: lines[m.prodX] || null, location: m.pos ? { @@ -24,13 +24,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].crdEncYX || null } } diff --git a/readme.md b/readme.md index d5b042ef..70e885b1 100644 --- a/readme.md +++ b/readme.md @@ -13,7 +13,7 @@ HAFAS endpoint | wrapper library | docs | example code | source code [![npm version](https://img.shields.io/npm/v/hafas-client.svg)](https://www.npmjs.com/package/hafas-client) [![build status](https://img.shields.io/travis/public-transport/hafas-client.svg)](https://travis-ci.org/public-transport/hafas-client) ![ISC-licensed](https://img.shields.io/github/license/public-transport/hafas-client.svg) -[![chat on gitter](https://badges.gitter.im/derhuerst.svg)](https://gitter.im/derhuerst) +[![chat on gitter](https://badges.gitter.im/public-transport/Lobby.svg)](https://gitter.im/public-transport/Lobby) [![support me on Patreon](https://img.shields.io/badge/support%20me-on%20patreon-fa7664.svg)](https://patreon.com/derhuerst)