-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·48 lines (43 loc) · 1.59 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
const fs = require('fs');
const { promisify } = require('@ctheory/promisify');
// Promisify the fs implementation for async/await use
const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);
const xml2js = require('xml2js');
const parseString = promisify(xml2js.parseString);
const idx = require('idx');
// Parse gpx file content
async function mergeGpxFiles(...gpxDataFiles) {
if (gpxDataFiles.length < 2) {
throw new Error('Must supply at least 2 gpx files');
}
const files = await readFiles(gpxDataFiles);
// If the xml reads as an array flatten the root
const _firstXml = await parseString(files.shift());
const firstXml = _firstXml[0] ? _firstXml[0] : _firstXml;
const parsedFiles = await Promise.all(files.map(file => parseString(file)));
const _trkpts = parsedFiles.map(_xml => {
const xml = _xml[0] ? _xml[0] : _xml;
return idx(xml, _ => _.gpx.trk[0].trkseg[0].trkpt) || [];
});
// Flatten the [ [ nested arrays ] ]
const trkpts = [].concat(..._trkpts);
try {
firstXml.gpx.trk[0].trkseg[0].trkpt.push(...trkpts);
} catch (err) {
throw new Error('Error parsing first gpx file.');
}
const builder = new xml2js.Builder();
return builder.buildObject(firstXml);
}
// An array of strings that are the files, parsed as utf-8
async function readFiles(gpsFilePaths) {
// Make sure all paths exist
for (file of gpsFilePaths) {
if (!fs.existsSync(file)) throw new Error(`File ${file} doesn't exist.`);
}
return await Promise.all(
gpsFilePaths.map(async file => await readFile(file, 'utf-8'))
);
}
module.exports = mergeGpxFiles;