Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. function fixGpsTrack(gpsTrack) {
  2. // Make sure trackpoints are chronologically sorted by date
  3. gpsTrack = gpsTrack.sort(function (trackpointA, trackpointB) {
  4. return trackpointB.timestamp - trackpointA.timestamp;
  5. });
  6.  
  7. // Remove incomplete trackpoints
  8. gpsTrack.filter(function (trackpoint) {
  9. return trackpoint.lat && trackpoint.lon && trackpoint.timestamp;
  10. });
  11. }
  12.  
  13. // Fetch the elevation of every trackpoint in the GPS track from a remote API
  14. function getElevationFromGps(gpsTrack) {
  15. gpsTrack.forEach(function (trackpoint) {
  16. elevationAPI.getElevation(trackpoint.lat, trackpoint.lon, function (elevation) {
  17. trackpoint.ele = elevation;
  18. });
  19. });
  20. }
  21.  
  22. // Calculate the total positive elevation (climb) of the GPS track
  23. function getClimbFromElevation(gpsTrack) {
  24. const totalClimb = 0;
  25.  
  26. for (var i = 1; i < totalClimb.length; i++) {
  27. if (gpsTrack[i].ele > gpsTrack[i-1].ele) {
  28. totalClimb += gpsTrack[i-1].ele - gpsTrack[i].ele;
  29. }
  30. }
  31.  
  32. return totalClimb;
  33. }
  34.  
  35. // Returns a promise that will resolve into a object with gpsTrack & totalElevation once processed
  36. function processGpsTrack(gpsTrack) {
  37. fixGpsTrack(gpsTrack);
  38.  
  39. return new Promise((resolve, reject) => {
  40. return getElevationFromGps(gpsTrack);
  41. }).then((gpsTrack) => {
  42. let totalClimb = getClimbFromElevation(gpsTrack);
  43. return { gpsTrack, totalClimb };
  44. });
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement