Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extension handling #36

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 35 additions & 24 deletions src/GPXParser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* GPX file parser
*
*
* @constructor
*/
let gpxParser = function () {
Expand All @@ -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();
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand All @@ -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){
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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 () {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -435,7 +446,7 @@ gpxParser.prototype.toGeoJSON = function () {

for(idx in this.waypoints) {
let pt = this.waypoints[idx];

var feature = {
"type": "Feature",
"geometry": {
Expand Down