Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
superextinct committed Jul 15, 2020
0 parents commit 34e3506
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_STORE
node_modules
package-lock.json
21 changes: 21 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
66 changes: 66 additions & 0 deletions tripper.js
Original file line number Diff line number Diff line change
@@ -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)));
});

0 comments on commit 34e3506

Please sign in to comment.