cygnal/src/tools/exportGPX.js

58 lines
1.7 KiB
JavaScript
Raw Normal View History

import FileSaver from 'file-saver';
2018-11-29 10:04:56 +01:00
import createGPX from 'gps-to-gpx/es/createGpx';
import { VERSION } from '@/constants';
import { formatDate } from '@/tools/date';
export default function (activityName, locationGPX) {
const eleKey = 'elevation';
const hdopKey = 'hdop';
const vdopKey = 'vdop';
2018-09-04 13:39:25 +02:00
// Extensions
const extKey = 'extensions';
const extensionsKeys = {
heading: 'course',
speed: 'speed',
};
const waypoints = [];
locationGPX.forEach((item) => {
const waypoint = Object.assign({}, item, { timestamp: new Date(item.timestamp) });
2018-09-04 13:39:25 +02:00
// Omit keys with empty values
[eleKey, hdopKey, vdopKey].forEach((key) => {
if (waypoint[key] === null || waypoint[key] === undefined) {
delete waypoint[key];
}
});
2018-09-04 13:39:25 +02:00
// Handle extensions
waypoint[extKey] = {};
Object.keys(extensionsKeys).forEach((key) => {
if (waypoint[key] !== null && waypoint[key] !== undefined) {
waypoint[extKey][extensionsKeys[key]] = waypoint[key];
delete waypoint[key];
}
});
waypoints.push(waypoint);
});
const gpx = createGPX(waypoints, {
activityName,
creator: `Cycl'Assist v${VERSION}`,
eleKey,
2018-09-04 13:39:25 +02:00
extKey,
hdopKey,
latKey: 'latitude',
lonKey: 'longitude',
startTime: waypoints[0].timestamp,
timeKey: 'timestamp',
vdopKey,
});
FileSaver.saveAs(
new Blob([gpx], { type: 'application/gpx+xml;charset=utf-8' }),
`cyclassist_${formatDate(new Date(), 'YYYY-MM-DD_HH-mm_ddd')}.gpx`,
);
}