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 rest of Waypoint schema elements + tests #27

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
35 changes: 24 additions & 11 deletions lib/gpx-parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ var _getWayPoints = function(gpxWaypoints) {
var waypoints = [];
if (gpxWaypoints && gpxWaypoints.length) {
gpxWaypoints.forEach(function(wayPoint) {
var point = new GpxWaypoint(wayPoint.$.lat, wayPoint.$.lon, getFloat(wayPoint.ele), wayPoint.time, null, null, getString(wayPoint.name), null, getString(wayPoint.desc));
var point = new GpxWaypoint(wayPoint.$.lat, wayPoint.$.lon, getFloat(wayPoint.ele), wayPoint.time, getString(wayPoint.magvar),
getFloat(wayPoint.geoidheight), getString(wayPoint.name), getString(wayPoint.cmt), getString(wayPoint.desc), getString(wayPoint.src),
wayPoint.link, getString(wayPoint.sym), getString(wayPoint.type), getString(wayPoint.fixType), getFloat(wayPoint.sat), getFloat(wayPoint.hdop),
getFloat(wayPoint.vdop), getFloat(wayPoint.pdop), getFloat(wayPoint.ageofdgpsdata), getInt(wayPoint.dgpsid));
waypoints.push(point);
});
}
Expand Down Expand Up @@ -69,6 +72,17 @@ var getFloat = function(item) {
return value;
};

/**
* Gets an integer from an element
**/
var getInt = function(item) {
var value = null;
if (item && Array.isArray(item) && item.length > 0) {
value = parseInt(item[0]);
}
return value;
};

/**
* Gets a string from an element
**/
Expand All @@ -88,22 +102,22 @@ var _getTracks = function(gpxTracks) {
var tracks = [];

if (gpxTracks && gpxTracks.length) {

gpxTracks.forEach(function(currentTrack) {

var trackSegments = [];

currentTrack.trkseg.forEach(function(currentSegment) {
var trackSegement = [];

currentSegment.trkpt.forEach(function(trackPoint) {
var elevation = getFloat(trackPoint.ele);
trackSegement.push(new GpxWaypoint(trackPoint.$.lat, trackPoint.$.lon, elevation, trackPoint.time));
trackSegement.push(new GpxWaypoint(trackPoint.$.lat, trackPoint.$.lon, getFloat(trackPoint.ele), trackPoint.time, getString(trackPoint.magvar),
getFloat(trackPoint.geoidheight), getString(trackPoint.name), getString(trackPoint.cmt), getString(trackPoint.desc), getString(trackPoint.src),
trackPoint.link, getString(trackPoint.sym), getString(trackPoint.type), getString(trackPoint.fixType), getFloat(trackPoint.sat), getFloat(trackPoint.hdop),
getFloat(trackPoint.vdop), getFloat(trackPoint.pdop), getFloat(trackPoint.ageofdgpsdata), getInt(trackPoint.dgpsid)));
});

trackSegments.push(trackSegement);
});

tracks.push(new GpxTrack(trackSegments, getString(currentTrack.name)));
});
}
Expand All @@ -130,13 +144,12 @@ var _ParseV10 = function(gpx) {
*/
var _ParseV11 = function(gpx) {
var metadata;

if (gpx.metadata && gpx.metadata.length > 0) {
metadata = new GpxMetaData(gpx.$.creator, getString(gpx.metadata[0].time));
} else {
metadata = new GpxMetaData();
}

return new GpxResult(metadata, _getWayPoints(gpx.wpt), _getRoutes(gpx.rte), _getTracks(gpx.trk));
};

Expand All @@ -161,7 +174,7 @@ exports.parseGpx = function(gpxString, callback) {
if (!data.gpx) return callback(new Error("version not specified"), null);

version = data.gpx.$.version;

switch (version) {
case "1.0":
gpxResult = _ParseV10(data.gpx);
Expand All @@ -174,7 +187,7 @@ exports.parseGpx = function(gpxString, callback) {
}

callback(null, gpxResult);

} catch (error) {
return callback(error);
}
Expand Down
217 changes: 189 additions & 28 deletions lib/gpxWaypoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,106 @@
* @param {string} cmt A comment regarding the waypoint.
* @param {string} desc A description of the waypoint.
* @param {string} src The source of the waypoint.
* @param {string[]} links An array of links for the waypoint.
* @param {linkType[]} links An array of links for the waypoint.
* @param {string} sym The symbol of the waypoint.
* @param {string} type The type of waypoint.
* @param {string} fixType Type of fix to measure the waypoint, from list: {'none'|'2d'|'3d'|'dgps'|'pps'}
* @param {number} sat Number of satellites used to calculate the waypoint fix.
* @param {number} hdop Horizontal dilution of precision.
* @param {number} vdop Vertical dilution of precision.
* @param {number} pdop Position dilution of precision.
* @param {number} ageofdgpsdata Number of seconds since last DGPS update.
* @param {number} dgpsid ID of DGPS station used in differential correction.
**/
function GpxWaypoint(lat, lon, elevation, time, magvar, geoidheight, name, cmt, desc, src, links, sym, type) {
function GpxWaypoint(lat, lon, elevation, time, magvar, geoidheight, name, cmt, desc, src, link, sym, type, fixType, sat, hdop, vdop, pdop, ageofdgpsdata, dgpsid) {
// if (elevation===4.94) {console.log(
// lat, lon, elevation, time, magvar, geoidheight, name, cmt, desc, src, link, sym, type, fixType, sat, hdop, vdop, pdop, ageofdgpsdata, dgpsid
// );}
lat = parseFloat(lat) || -1;
lon = parseFloat(lon) || -1;
elevation = elevation || -1;
time = time ? new Date(time) : null;
magvar = parseFloat(magvar) || -1;
geoidheight = parseFloat(geoidheight) || -1;
name = name || null;
cmt = cmt || "";
desc = desc || null;
src = src || "";
link = link || [];
sym = sym || "";
type = type || "";
fixType = fixType || null;
sat = sat || -1
hdop = parseFloat(hdop) || -1;
vdop = parseFloat(vdop) || -1;
pdop = parseFloat(pdop) || -1;
ageofdgpsdata = parseFloat(ageofdgpsdata) || -1;
dgpsid = dgpsid || -1;
/**
* Latitude of the Waypoint
* @name lat
* @memberOf GpxWaypoint
* @instance
* @type {Number}
**/
this.__defineGetter__("lat", function() {
return lat;
});

/**
* Longtitude of the Waypoint
* @name lon
* @memberOf GpxWaypoint
* @instance
* @type {Number}
**/
this.__defineGetter__("lon", function() {
return lon;
});

/**
* Elevation at the Waypoint
* @name elevation
* @memberOf GpxWaypoint
* @instance
* @type {Number}
**/
this.__defineGetter__("elevation", function() {
return elevation;
});

/**
* Time associated with the Waypoint
* @name time
* @memberOf GpxWaypoint
* @instance
* @type {Date}
**/
this.__defineGetter__("time", function() {
return time;
});

/**
* Magnetic variation at the Waypoint.
* @name magvar
* @memberOf GpxWaypoint
* @instance
* @type {number}
**/
this.__defineGetter__("magvar", function() {
return magvar;
});

/**
* Geoid height at the Waypoint.
* @name geoidheight
* @memberOf GpxWaypoint
* @instance
* @type {number}
**/
this.__defineGetter__("geoidheight", function() {
return geoidheight;
});

/**
* Name of the Waypoint
Expand All @@ -38,9 +124,8 @@ function GpxWaypoint(lat, lon, elevation, time, magvar, geoidheight, name, cmt,
return name;
});


/**
* cmt of the Waypoint
* Comment on the Waypoint
* @name cmt
* @memberOf GpxWaypoint
* @instance
Expand All @@ -52,67 +137,143 @@ function GpxWaypoint(lat, lon, elevation, time, magvar, geoidheight, name, cmt,

/**
* Description of the Waypoint
* @name description
* @name desc
* @memberOf GpxWaypoint
* @instance
* @type {string}
**/
Object.defineProperty(this,'description', {
get: function() { return desc; }
});
this.__defineGetter__("description", function() {
return desc;
});

/**
* Source of the the Waypoint
* @name src
* @memberOf GpxWaypoint
* @instance
* @type {string}
**/
this.__defineGetter__("src", function() {
return src;
});

/**
* Links associated with the Waypoint
* @name link
* @memberOf GpxWaypoint
* @instance
* @type {Array<linkType>}
**/

this.__defineGetter__("links", function() {
if (link.length) {
return link.map(function(l) {
var href = l.hasOwnProperty('$') ? l.$.href : "";
var text = l.text&&l.text[0] ? l.text[0] : "";
var type = l.type&&l.type[0] ? l.type[0] : "";
return {href:href, text:text, type:type};
});
}
else { return []; }
});

/**
* Symbol used to mark the Waypoint
* @name sym
* @memberOf GpxWaypoint
* @instance
* @type {string}
**/
this.__defineGetter__("sym", function() {
return sym;
});

/**
* Type associated with the Waypoint
* @name type
* @memberOf GpxWaypoint
* @instance
* @type {string}
**/
this.__defineGetter__("type", function() {
return type;
});

/**
* Latitude of the Waypoint
* @name lat
* Fix tyoe used measure the waypoint, from list: {'none'|'2d'|'3d'|'dgps'|'pps'}
* @name fixType
* @memberOf GpxWaypoint
* @instance
* @type {Number}
* @type {string}
**/
this.__defineGetter__("lat", function() {
return lat;
this.__defineGetter__("fixType", function() {
return fixType;
});

/**
* Longtitude of the Waypoint
* @name lon
* Number of satellites used to calculate the waypoint fix.
* @name sat
* @memberOf GpxWaypoint
* @instance
* @type {Number}
* @type {number}
**/
this.__defineGetter__("lon", function() {
return lon;
this.__defineGetter__("sat", function() {
return sat;
});

/**
* Horizontal dilution of precision.
* @name hdop
* @memberOf GpxWaypoint
* @instance
* @type {number}
**/
this.__defineGetter__("hdop", function() {
return hdop;
});

/**
* Elevation at the Waypoint
* @name elevation
* Vertical dilution of precision.
* @name vdop
* @memberOf GpxWaypoint
* @instance
* @type {Number}
* @type {number}
**/
this.__defineGetter__("elevation", function() {
return elevation;
this.__defineGetter__("vdop", function() {
return vdop;
});

/**
* Position dilution of precision.
* @name pdop
* @memberOf GpxWaypoint
* @instance
* @type {number}
**/
this.__defineGetter__("pdop", function() {
return pdop;
});

/**
* Time associated with the Waypoint
* @name time
* Number of seconds since last DGPS update.
* @name ageofdgpsdata
* @memberOf GpxWaypoint
* @instance
* @type {Date}
* @type {number}
**/
this.__defineGetter__("time", function() {
return time;
this.__defineGetter__("ageofdgpsdata", function() {
return ageofdgpsdata;
});

/**
* ID of DGPS station used in differential correction.
* @name dgpsid
* @memberOf GpxWaypoint
* @instance
* @type {number}
**/
this.__defineGetter__("dgpsid", function() {
return dgpsid;
});
}

Expand Down
Loading