Skip to content

Instantly share code, notes, and snippets.

@gaspaonrocks
Created August 16, 2017 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gaspaonrocks/49ea986920b524202db859f3a18cc68b to your computer and use it in GitHub Desktop.
Save gaspaonrocks/49ea986920b524202db859f3a18cc68b to your computer and use it in GitHub Desktop.
test SportHeroesGroup
function fixGpsTrack(gpsTrack) {
// Make sure trackpoints are chronologically sorted by date
// il n'y a pas besoin de réaffecter gpsTrack.
gpsTrack.sort(function (trackpointA, trackpointB) {
return trackpointB.timestamp - trackpointA.timestamp;
});
// Remove incomplete trackpoints
gpsTrack.filter(function (trackpoint) {
return trackpoint.lat && trackpoint.lon && trackpoint.timestamp;
});
}
// Fetch the elevation of every trackpoint in the GPS track from a remote API
function getElevationFromGps(gpsTrack) {
gpsTrack.forEach(function (trackpoint) {
elevationAPI.getElevation(trackpoint.lat, trackpoint.lon, function (elevation) {
trackpoint.ele = elevation;
});
});
}
// Calculate the total positive elevation (climb) of the GPS track
function getClimbFromElevation(gpsTrack) {
const totalClimb = 0;
// on itère sur le tableau en entrée, donc on part du premier élément [0] jusqu'au dernier.
for (var i = 0, len = gpsTrack.length; i < len; i++) {
if (gpsTrack[i].ele > gpsTrack[i - 1].ele) {
// si l'altitude de [i] est supérieure, on calcule sa différence avec le précédent, pas le suivant.
totalClimb += gpsTrack[i].ele - gpsTrack[i - 1].ele;
}
}
return totalClimb;
}
// Returns a promise that will resolve into a object with gpsTrack & totalElevation once processed
function processGpsTrack(gpsTrack) {
fixGpsTrack(gpsTrack);
return new Promise((resolve, reject) => {
return getElevationFromGps(gpsTrack);
}).then((gpsTrack) => {
let totalClimb = getClimbFromElevation(gpsTrack);
return { gpsTrack, totalClimb };
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment