Clean up GPX code and use EJS template

pull/108/head
Candid Dauth 2017-05-26 17:36:51 +02:00
rodzic 6cc1c5a447
commit c1dbe27d2d
3 zmienionych plików z 63 dodań i 93 usunięć

Wyświetl plik

@ -6,19 +6,19 @@
</metadata>
<<%=useTracks ? 'trk' : 'rte'%>>
<name><%=line.name || 'FacilMap route'%></name>
<% if(desc) { %>
<% if(desc) { -%>
<desc><%=desc%></desc>
<% } %>
<% if(useTracks) { %>
<% } -%>
<% if(useTracks) { -%>
<trkseg>
<% for(let trackPoint of line.trackPoints) { %>
<% for(let trackPoint of line.trackPoints) { -%>
<trkpt lat="<%=trackPoint.lat%>" lon="<%=trackPoint.lon%>"<% if(trackPoint.ele != null){ %> ele="<%=trackPoint.ele%>"<% } %> />
<% } %>
<% } -%>
</trkseg>
<% } else { %>
<% for(let routePoint of line.routePoints) { %>
<% } else { -%>
<% for(let routePoint of line.routePoints) { -%>
<rtept lat="<%=routePoint.lat%>" lon="<%=routePoint.lon%>" />
<% } %>
<% } %>
<% } -%>
<% } -%>
</<%=useTracks ? 'trk' : 'rte'%>>
</gpx>

Wyświetl plik

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" creator="FacilMap" version="1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">
<metadata>
<name><%=padData.name%></name>
<time><%=time%></time>
</metadata>
<% for(let marker of markers) { -%>
<wpt lat="<%=marker.lat%>" lon="<%=marker.lon%>"<% if(marker.ele != null) { %> ele="<%=marker.ele%>"<% } %>>
<name><%=marker.name%></name>
<desc><%=dataToText(types[marker.typeId].fields, marker.data)%></desc>
</wpt>
<% } -%>
<% for(let line of lines) { -%>
<% let t = (useTracks || line.mode == "track"); -%>
<<%=t ? 'trk' : 'rte'%>>
<name><%=line.name%></name>
<desc><%=dataToText(types[line.typeId].fields, line.data)%></desc>
<% if(t) { -%>
<trkseg>
<% for(let trackPoint of line.trackPoints) { -%>
<trkpt lat="<%=trackPoint.lat%>" lon="<%=trackPoint.lon%>"<% if(trackPoint.ele != null) { %> ele="<%=trackPoint.ele%>"<% } %> />
<% } -%>
</trkseg>
<% } else { -%>
<% for(let routePoint of line.routePoints) { %>
<rtept lat="<%=routePoint.lat%>" lon="<%=routePoint.lon%>" />
<% } -%>
<% } -%>
</<%=t ? 'trk' : 'rte'%>>
<% } -%>
</gpx>

Wyświetl plik

@ -4,7 +4,12 @@ const Promise = require("bluebird");
var utils = require("../utils");
var _e = utils.escapeXml;
let padTemplate;
Promise.promisify(fs.readFile)(`${__dirname}/gpx-pad.ejs`).then((t) => {
padTemplate = ejs.compile(t.toString());
}).catch((err) => {
console.error("Error reading GPX pad template", err.stack || err);
});
let lineTemplate;
Promise.promisify(fs.readFile)(`${__dirname}/gpx-line.ejs`).then((t) => {
@ -24,94 +29,28 @@ function _dataToText(fields, data) {
return text.join('\n\n');
}
function _textToData(fields, text) {
function hasField(fieldName) {
for(var i=0; i<fields.length; i++) {
if(fields[i].name == fieldName)
return true;
}
return false;
}
var textSplit = text.replace(/\r/g, '').split('\n\n');
var ret = { };
var lastField = null;
for(var i=0; i<textSplit.length; i++) {
if(textSplit[i].indexOf(': ') != -1) {
var spl = textSplit[i].split(': ', 2);
if(hasField(spl[0])) {
lastField = spl[0];
ret[lastField] = spl[1];
continue;
}
}
if(lastField == null) {
lastField = 'Description';
ret[lastField] = textSplit[i];
} else {
ret[lastField] += '\n\n' + textSplit[i];
}
}
return ret;
}
function exportGpx(database, padId, useTracks) {
return utils.promiseAuto({
padData: database.getPadData(padId),
typesObj: () => {
return Promise.all([
database.getPadData(padId),
Promise.resolve().then(() => {
var typesObj = { };
return utils.streamEachPromise(database.getTypes(padId), function(type) {
typesObj[type.id] = type;
}).then(() => typesObj);
},
markers: (typesObj) => {
var markers = '';
return utils.streamEachPromise(database.getPadMarkers(padId), function(marker) {
markers += '<wpt lat="' + _e(marker.lat) + '" lon="' + _e(marker.lon) + '"' + (marker.ele != null ? ' ele="' + _e(marker.ele) + '"' : '') + '>\n' +
'\t<name>' + _e(marker.name) + '</name>\n' +
'\t<desc>' + _e(_dataToText(typesObj[marker.typeId].fields, marker.data)) + '</desc>\n' +
'</wpt>\n';
}).then(() => markers);
},
lines: (typesObj) => {
var lines = '';
return utils.streamEachPromise(database.getPadLinesWithPoints(padId), function(line) {
var t = (useTracks || line.mode == "track");
lines += '<' + (t ? 'trk' : 'rte') + '>\n' +
'\t<name>' + _e(line.name) + '</name>\n' +
'\t<desc>' + _e(_dataToText(typesObj[line.typeId].fields, line.data)) + '</desc>\n';
if(t) {
lines += '\t<trkseg>\n';
for(var i=0; i<line.trackPoints.length; i++) {
lines += '\t\t<trkpt lat="' + _e(line.trackPoints[i].lat) + '" lon="' + _e(line.trackPoints[i].lon) + '"' + (line.trackPoints[i].ele != null ? ' ele="' + _e(line.trackPoints[i].ele) + '"' : '') + ' />\n';
}
lines += '\t</trkseg>\n';
} else {
for(var i=0; i<line.routePoints.length; i++) {
lines += '\t\t<rtept lat="' + _e(line.routePoints[i].lat) + '" lon="' + _e(line.trackPoints[i].lon) + '" />\n';
}
}
lines += '</' + (t ? 'trk' : 'rte') + '>\n';
}).then(() => lines);
}
}).then((res) => '<?xml version="1.0" encoding="UTF-8"?>\n' +
'<gpx xmlns="http://www.topografix.com/GPX/1/1" creator="FacilMap" version="1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" xmlns:fm="https://facilmap.org/">\n' +
'\t<metadata>\n' +
'\t\t<name>' + _e(res.padData.name) + '</name>\n' +
'\t\t<time>' + _e(utils.isoDate()) + '</time>\n' +
'\t</metadata>\n' +
res.markers.replace(/^(.)/gm, '\t$1') +
res.lines.replace(/^(.)/gm, '\t$1') +
'</gpx>'
);
}),
utils.streamToArrayPromise(database.getPadMarkers(padId)),
utils.streamToArrayPromise(database.getPadLinesWithPoints(padId))
]).then(([padData, types, markers, lines]) => {
return padTemplate({
time: utils.isoDate(),
padData,
types,
markers,
lines,
dataToText: _dataToText,
useTracks
});
});
}
function exportLine(line, type, useTracks) {