-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
55 lines (45 loc) · 1.65 KB
/
index.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
const csv = require('csvtojson');
const fs = require('fs');
const { basename } = require('path');
const { reverseGeocode, toGeoJson } = require('./util.js');
async function main(exportFolderPath = './export', targetYear = 2019) {
const activitiesMetadata = await csv().fromFile(
`${exportFolderPath}/activities.csv`,
);
const paths = [];
let totalDistance = 0;
for (let i = 0; i < activitiesMetadata.length; ++i) {
const activityMetadata = activitiesMetadata[i];
const { Distance, Filename } = activityMetadata;
const activityType = activityMetadata['Activity Type'];
const activityFileName = basename(Filename, '.gz');
// Parse activity file into GeoJSON.
const path = await toGeoJson(
`${exportFolderPath}/activities/${activityFileName}`,
);
const [features] = path.features;
const { geometry, properties } = features;
const { coordinates } = geometry;
const { time } = properties;
// Determine date and year of activity.
const date = new Date(time);
const year = date.getFullYear();
// Only include activities with path data during the target year.
if (coordinates.length > 0 && year === targetYear) {
const distance = parseFloat(Distance);
const [[long, lat]] = coordinates;
// Figure out what city the activity was in and increment total distance.
const place = await reverseGeocode(long, lat);
totalDistance += distance;
paths.push({
...path,
activityType,
date: date.toLocaleDateString(),
distance,
place,
});
}
}
fs.writeFileSync(`./paths.json`, JSON.stringify({ paths, totalDistance }));
}
main();