Skip to content

Instantly share code, notes, and snippets.

@fabulator
Created May 10, 2018 14:04
Show Gist options
  • Save fabulator/e74fba0efcd0a0f9dda133b18145050c to your computer and use it in GitHub Desktop.
Save fabulator/e74fba0efcd0a0f9dda133b18145050c to your computer and use it in GitHub Desktop.
Google export json to gpx
const fs = require('fs');
const createGpx = require('gps-to-gpx').default;
const program = require('commander');
program
.version('0.1.0')
.command('parse <inputFile>')
.option('--from <from>', 'From which date parse data YYYY-MM-DDTHH:ii.')
.option('--to <to>', 'To which date parse data YYYY-MM-DDTHH:ii.')
.option('--accuracy <accuracy>', 'Minimal accuracy.')
.action((inputFile, options) => {
const from = options.from ? new Date(options.from) : null;
const to = options.to ? new Date(options.to) : null;
const googleLocation = JSON.parse(fs.readFileSync(inputFile, 'utf8'));
let waypoints = googleLocation.locations;
if (from || to || options.accuracy) {
waypoints = waypoints.filter((item) => {
return (!from || (item.timestampMs >= from.getTime())) &&
(!to || (item.timestampMs <= to.getTime())) &&
(!options.accuracy || item.accuracy <= options.accuracy)
});
}
waypoints = waypoints.map((item) => {
return {
time: new Date(Number(item.timestampMs)),
latitude: item.latitudeE7 / 10000000,
longitude: item.longitudeE7 / 10000000,
accuracy: item.accuracy,
elevation: item.altitude != null ? item.altitude : null,
};
});
fs.writeFileSync(inputFile.replace('.json', '.gpx'), createGpx(waypoints));
});
program.parse(process.argv);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment