From 34e3506df1b9ed5438b901855c9e4a901444b8b3 Mon Sep 17 00:00:00 2001 From: Niklas Date: Wed, 15 Jul 2020 15:41:13 +0200 Subject: [PATCH] Initial commit --- .gitignore | 3 +++ package.json | 21 +++++++++++++++++ tripper.js | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 .gitignore create mode 100644 package.json create mode 100644 tripper.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4a2c0bf --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.DS_STORE +node_modules +package-lock.json \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..a3558ac --- /dev/null +++ b/package.json @@ -0,0 +1,21 @@ +{ + "name": "tripper", + "version": "1.0.0", + "description": "", + "main": "tripper.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "JSONStream": "^1.3.5", + "chalk": "^4.1.0", + "event-stream": "^4.0.1", + "figlet": "^1.5.0", + "fs": "0.0.1-security", + "minimist": "^1.2.5", + "path": "^0.12.7" + } +} diff --git a/tripper.js b/tripper.js new file mode 100644 index 0000000..9c877a6 --- /dev/null +++ b/tripper.js @@ -0,0 +1,66 @@ +const fs = require("fs"); +const JSONStream = require("JSONStream"); +const es = require("event-stream"); +const path = require("path"); +const byline = require("byline"); +const through2 = require("through2"); +const chalk = require("chalk"); +const figlet = require("figlet"); +const argv = require("minimist")(process.argv.slice(2)); + +console.log( + chalk.yellow( + figlet.textSync("tripper", { horizontalLayout: "full" }) + ) +); + + +if (!argv.input || !argv.output) { + console.log( "\n" + chalk.gray.bold.bgYellow("Error") + " Specify input and output file, e.g. " + chalk.cyan("node tripper --input=\"./trips.json\" --output=\"./parsed.json\"")); + process.exit(); +} + +const input = path.join(__dirname, argv.input); + +let trips = []; + +fs.createReadStream(input) + .pipe(byline.createStream()) + .pipe( + through2((chunk, enc, next) => { + try { + const data = JSON.parse(chunk.toString()); + + let coordinates = []; + + data.route.features.forEach( (feature) => { + coordinates.push([feature.geometry.coordinates[0], feature.geometry.coordinates[1], 0, 1563122921000 + feature.properties.timestamp]); + }); + + let trip = { + "vehicle_id": data.vehicle_id, + "trip_duration": data.trip_duration, + "trip_distance": data.trip_distance, + "start_time": data.start_time, + "end_time": data.end_time, + "geojson": { + "type": "Feature", + "geometry": { + "type": "LineString", + "coordinates": coordinates + } + } + }; + + trips.push(trip); + next(); + } catch (e) { + console.log(e); + next(); + } + }) + ) + .on("finish", () => { + fs.writeFileSync(path.join(__dirname, argv.output), JSON.stringify(trips)); + console.log("\n" + chalk.bold.bgGreen("Success") + " Generated file " + chalk.underline(path.join(__dirname, argv.output))); + }); \ No newline at end of file