Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function fixGpsTrack(gpsTrack) {
- // Make sure trackpoints are chronologically sorted by date
- 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;
- for (var i = 1; i < totalClimb.length; i++) {
- if (gpsTrack[i].ele > gpsTrack[i-1].ele) {
- totalClimb += gpsTrack[i-1].ele - gpsTrack[i].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 };
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement