BR.Gpx = {
format: function (geoJson) {
const gpx = togpx(geoJson, {
featureDescription: function () {},
});
const statsComment = BR.Gpx._statsComment(geoJson);
const xml = '' + statsComment + gpx;
return BR.Gpx.pretty(xml, 1);
},
//
_statsComment: function (geoJson) {
const props = geoJson.features?.[0].properties;
if (!props) return '';
let comment = '';
return comment;
},
// modified version of
// https://gist.github.com/sente/1083506#gistcomment-2254622
// MIT License, Copyright (c) 2016 Stuart Powers, ES6 version by Jonathan Gruber
pretty: function (xml, indentSize = 2) {
const PADDING = ' '.repeat(indentSize);
const newline = '\n';
// break into lines, keep trkpt with subelement ele in single line
const reg = /(>)(<)(?!ele|\/trkpt)(\/?)/g;
let pad = 0;
xml = xml.replace('', '');
xml = xml.replace(reg, `$1${newline}$2$3`);
let lines = xml.split(newline);
lines = lines.map((node, index) => {
let indent = 0;
if (node.match(/.+<\/\w[^>]*>$/)) {
indent = 0;
} else if (node.match(/^<\/\w/) && pad > 0) {
pad -= 1;
} else if (node.match(/^<\w[^>]*[^\/]>.*$/)) {
indent = 1;
} else {
indent = 0;
}
pad += indent;
return PADDING.repeat(pad - indent) + node;
});
for (const [i, line] of lines.entries()) {
// break gpx attributes into separate lines
if (line.includes('