-
Notifications
You must be signed in to change notification settings - Fork 0
/
runmeter2geojson.js
70 lines (58 loc) · 1.94 KB
/
runmeter2geojson.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
67
68
69
70
var sqlite3 = require('sqlite3').verbose(),
fs = require('fs'),
path = require('path'),
input = process.argv[2],
output = path.resolve(__dirname, process.argv[3]),
db = new sqlite3.Database(input);
var geojson = { type: "FeatureCollection", features: [] };
function getRunCoordinates(run, callback) {
var coordinates = [],
times = [],
startTime = new Date(run.startTime);
db.each('select latitude, longitude, timeOffset from coordinate where runID = ?', { 1: run.runID }, function(err, row) {
coordinates.push([row.longitude, row.latitude]);
var coordTime = new Date(startTime.getTime() + row.timeOffset*1000);
times.push(coordTime);
}, function() {
callback(run, coordinates, times);
});
}
function parseRuns(rows, doneParsingCallback) {
var i = 0;
function createRun(run, coordinates, times) {
var feature = {
type: 'Feature',
properties: {
id: run.runID,
start_time: run.startTime,
run_time: Math.floor(run.runTime/60) + ":" + Math.floor(run.runTime % 60),
distance: Math.floor(run.distance)/1000,
ascent: Math.floor(run.ascent),
descent: Math.floor(run.descent),
calories: Math.floor(run.calories),
locality: run.locality,
times: times
}
}
if (!coordinates || coordinates.length < 2) {
feature.geometry = null;
} else {
feature.geometry = { type: 'LineString', coordinates: coordinates };
}
geojson.features.push(feature);
i++;
if (i < rows.length) {
getRunCoordinates(rows[i], createRun);
} else {
console.log("Exported", rows.length, "runs");
doneParsingCallback();
}
}
getRunCoordinates(rows[i], createRun);
}
var sql = "select runID, startTime, runTime, distance, ascent, descent, calories, locality from run";
db.all(sql, [], function(err, rows) {
parseRuns(rows, function() {
fs.writeFileSync(output, JSON.stringify(geojson));
});
});