-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtripper.js
66 lines (56 loc) · 2.18 KB
/
tripper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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)));
});