-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 34e3506
Showing
3 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.DS_STORE | ||
node_modules | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
}); |