Created
August 16, 2017 09:09
-
-
Save gaspaonrocks/49ea986920b524202db859f3a18cc68b to your computer and use it in GitHub Desktop.
test SportHeroesGroup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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