UNPKG

standstill

Version:

Find locations where there has been no movement, a stop, within a GeoJSON track, typically recorded from a GPS

61 lines (55 loc) 2.78 kB
var findstops = require('./'), track = require('./test/sample-geojson3.json'), ts = function(d) { return new Date(d).getTime(); }; var d = findstops(track, {maxTimeGap: 24 * 60 * 60 * 1000, stopTolerance: 0.05}), timeDriving = d.routes.features.reduce(function(t, route) { var first = ts(route.properties.coordTimes[0]), last = ts(route.properties.coordTimes[route.properties.coordTimes.length - 1]); return t + last - first; }, 0), timeStill = d.stops.features.reduce(function(t, stop) { return t + ts(stop.properties.endTime) - ts(stop.properties.startTime); }, 0), /*distance = d.routes.features.reduce(function(d, route) { return d + linedistance(route); }, 0),*/ stops = d.stops.features.map(function(s, i) { return [s.properties.startTime, s.properties.endTime, 'Stop #' + i]; }), routes = d.routes.features.map(function(s, i) { return [s.properties.coordTimes[0], s.properties.coordTimes[s.properties.coordTimes.length - 1], 'Route #' + i]; }), events = [ [track.properties.coordTimes[0], track.properties.coordTimes[0], 'Recording starts'], [track.properties.coordTimes[track.properties.coordTimes.length - 1], track.properties.coordTimes[track.properties.coordTimes.length - 1], 'Recording ends'] ].concat(stops).concat(routes); indataTime = ts(track.properties.coordTimes[track.properties.coordTimes.length - 1]) - ts(track.properties.coordTimes[0]); events.sort(function(a, b) { return ts(a[0]) - ts(b[0]); }); events.forEach(function(e, i) { e[3] = i > 0 ? ts(e[0]) - ts(events[i - 1][1]) : undefined; }); console.log(JSON.stringify({ stats: { timeDriving: timeDriving, timeStill: timeStill, indataTime: indataTime, aggregatedTime: timeDriving + timeStill, timeDiffMinutes: (indataTime - (timeDriving + timeStill)) / (60 * 1000) }, events: events }, null, ' ')); console.log('<html><head><style>.bar { position:relative; width: 100%; height: 40px; } .bar div { position: absolute; top:0; height: 100%; overflow: hidden; }</style></head><body><div class="bar">' + events.map(function(e, i, es) { var color = 'white', start = (ts(e[0]) - ts(es[0][0])) / (timeDriving + timeStill) * 100, percentage = (ts(e[1]) - ts(e[0])) / (timeDriving + timeStill) * 100; if (e[2].substring(0, 4) === 'Stop') { color = 'red'; } else if (e[2].substring(0, 5) === 'Route') { color = 'green'; } return `<div style="background:${color};left:${start}%;width:${percentage}%;">${e[0].substring(11,16)}</div>`; }).join('') + '</div></body></html>' );