diff --git a/src/GPXParser.js b/src/GPXParser.js index 1034d51..b9d85aa 100644 --- a/src/GPXParser.js +++ b/src/GPXParser.js @@ -1,6 +1,6 @@ /** * GPX file parser - * + * * @constructor */ let gpxParser = function () { @@ -13,12 +13,13 @@ let gpxParser = function () { /** * Parse a gpx formatted string to a GPXParser Object - * + * * @param {string} gpxstring - A GPX formatted String - * + * @param {array} parseExts - an array of strings of extensions to parse + * * @return {gpxParser} A GPXParser object */ -gpxParser.prototype.parse = function (gpxstring) { +gpxParser.prototype.parse = function (gpxstring, parseExts=[]) { let keepThis = this; let domParser = new window.DOMParser(); @@ -71,7 +72,7 @@ gpxParser.prototype.parse = function (gpxstring) { pt.lat = parseFloat(wpt.getAttribute("lat")); pt.lon = parseFloat(wpt.getAttribute("lon")); - let floatValue = parseFloat(keepThis.getElementValue(wpt, "ele")); + let floatValue = parseFloat(keepThis.getElementValue(wpt, "ele")); pt.ele = isNaN(floatValue) ? null : floatValue; pt.cmt = keepThis.getElementValue(wpt, "cmt"); @@ -114,7 +115,7 @@ gpxParser.prototype.parse = function (gpxstring) { pt.lat = parseFloat(rtept.getAttribute("lat")); pt.lon = parseFloat(rtept.getAttribute("lon")); - let floatValue = parseFloat(keepThis.getElementValue(rtept, "ele")); + let floatValue = parseFloat(keepThis.getElementValue(rtept, "ele")); pt.ele = isNaN(floatValue) ? null : floatValue; let time = keepThis.getElementValue(rtept, "time"); @@ -162,12 +163,22 @@ gpxParser.prototype.parse = function (gpxstring) { pt.lat = parseFloat(trkpt.getAttribute("lat")); pt.lon = parseFloat(trkpt.getAttribute("lon")); - let floatValue = parseFloat(keepThis.getElementValue(trkpt, "ele")); + let floatValue = parseFloat(keepThis.getElementValue(trkpt, "ele")); pt.ele = isNaN(floatValue) ? null : floatValue; let time = keepThis.getElementValue(trkpt, "time"); pt.time = time == null ? null : new Date(time); + // Probably want to pass in an array of extensions user wants here. + // This is all I want though + let extensions = trkpt.querySelector("extensions"); + if (extensions!="null"){ + for (let extension of parseExts){ + let value = parseFloat(keepThis.getElementValue(extensions, extension)); + pt[extension] = isNaN(value) ? null : value; + } + } + trackpoints.push(pt); } track.distance = keepThis.calculDistance(trackpoints); @@ -181,10 +192,10 @@ gpxParser.prototype.parse = function (gpxstring) { /** * Get value from a XML DOM element - * + * * @param {Element} parent - Parent DOM Element * @param {string} needle - Name of the searched element - * + * * @return {} The element value */ gpxParser.prototype.getElementValue = function(parent, needle){ @@ -198,10 +209,10 @@ gpxParser.prototype.getElementValue = function(parent, needle){ /** * Search the value of a direct child XML DOM element - * + * * @param {Element} parent - Parent DOM Element * @param {string} needle - Name of the searched element - * + * * @return {} The element value */ gpxParser.prototype.queryDirectSelector = function(parent, needle) { @@ -225,9 +236,9 @@ gpxParser.prototype.queryDirectSelector = function(parent, needle) { /** * Calcul the Distance Object from an array of points - * + * * @param {} points - An array of points with lat and lon properties - * + * * @return {DistanceObject} An object with total distance and Cumulative distances */ gpxParser.prototype.calculDistance = function(points) { @@ -248,10 +259,10 @@ gpxParser.prototype.calculDistance = function(points) { /** * Calcul Distance between two points with lat and lon - * + * * @param {} wpt1 - A geographic point with lat and lon properties * @param {} wpt2 - A geographic point with lat and lon properties - * + * * @returns {float} The distance between the two points */ gpxParser.prototype.calcDistanceBetween = function (wpt1, wpt2) { @@ -273,9 +284,9 @@ gpxParser.prototype.calcDistanceBetween = function (wpt1, wpt2) { /** * Generate Elevation Object from an array of points - * + * * @param {} points - An array of points with ele property - * + * * @returns {ElevationObject} An object with negative and positive height difference and average, max and min altitude data */ gpxParser.prototype.calcElevation = function (points) { @@ -321,11 +332,11 @@ gpxParser.prototype.calcElevation = function (points) { }; /** - * Generate slopes Object from an array of Points and an array of Cumulative distance - * + * Generate slopes Object from an array of Points and an array of Cumulative distance + * * @param {} points - An array of points with ele property * @param {} cumul - An array of cumulative distance - * + * * @returns {SlopeObject} An array of slopes */ gpxParser.prototype.calculSlope = function(points, cumul) { @@ -346,7 +357,7 @@ gpxParser.prototype.calculSlope = function(points, cumul) { /** * Export the GPX object to a GeoJSON formatted Object - * + * * @returns {} a GeoJSON formatted Object */ gpxParser.prototype.toGeoJSON = function () { @@ -385,7 +396,7 @@ gpxParser.prototype.toGeoJSON = function () { for(idx in track.points) { let pt = track.points[idx]; - + var geoPt = []; geoPt.push(pt.lon); geoPt.push(pt.lat); @@ -421,7 +432,7 @@ gpxParser.prototype.toGeoJSON = function () { for(idx in track.points) { let pt = track.points[idx]; - + var geoPt = []; geoPt.push(pt.lon); geoPt.push(pt.lat); @@ -435,7 +446,7 @@ gpxParser.prototype.toGeoJSON = function () { for(idx in this.waypoints) { let pt = this.waypoints[idx]; - + var feature = { "type": "Feature", "geometry": {