Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Calculate Mileage (written in JS)
  3.  */
  4.  
  5. function(err, spots) {
  6.  
  7.   if (err) throw err;
  8.  
  9.   var totalDist = 0;
  10.   var dist = 0;
  11.   var radlat1, radlat2, theta, radtheta;
  12.   var timeDiff = 0;
  13.   var diffPerHour = 0;
  14.   var milesPerHour = 0;
  15.   var updateArr = [];
  16.  
  17.   for(var i=1;i<spots.length;i++){
  18.     if (spots[i].latitude === spots[i-1].latitude && spots[i].longitude === spots[i-1].longitude) {
  19.       /** check for identical lat/lon coords to last spot(duplicate) and mark as invalid */
  20.       spots[i].isValid = false;
  21.       /** push the spot to the pending updates array */
  22.       updateArr.push(spots[i]);
  23.  
  24.     } else {
  25.       /** lat/long is different so some distance was travelled, do the math */
  26.  
  27.       /** distance calc from last spot */
  28.       radlat1 = Math.PI * spots[i-1].latitude/180;
  29.       radlat2 = Math.PI * spots[i].latitude/180;
  30.       theta = spots[i-1].longitude-spots[i].longitude;
  31.       radtheta = Math.PI * theta/180;
  32.       dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
  33.       dist = Math.acos(dist);
  34.       dist = dist * 180/Math.PI;
  35.       dist = dist * 60 * 1.1515;
  36.  
  37.       /** sequential identical lat/lon coords will return NaN(you're trying to divide by 0), so log an error and skip the rest. NaN is special in that it is NOT equal to itself. */
  38.       if (dist != dist) {
  39.         console.log('distance calc returned NaN for:',spots[i-1].id,'to',spots[i].id);
  40.       } else {
  41.         /** if distance was valid, but less the margin of error, set distance to 1 foot(in miles) */
  42.         if (dist === 0) { dist = 0.00018939393; }
  43.  
  44.         /** calculate time since last spot */
  45.         timeDiff = spots[i].unixTime - spots[i-1].unixTime;
  46.         if (timeDiff > 7200) {
  47.           /** if it's been more than two hours, log a possible handoff or stop */
  48.           console.log(color.yellow('Possible Handoff'));
  49.         }
  50.         diffPerHour = 3600/timeDiff;
  51.  
  52.         /** calculate time since last spot */
  53.         milesPerHour = dist * diffPerHour;
  54.         var minutes = Math.floor(Math.floor(timeDiff)/60);
  55.         var calc = timeDiff - (parseInt(minutes)*60);
  56.  
  57.         /** if speed between stops is 15MPH or greater, log a warning */
  58.         if (milesPerHour > 14.99) {
  59.           console.log('Speed: ',spots[i-1].id,' to ',spots[i].id, color.red(dist.toFixed(2)+'mi in '+minutes+'min '+calc+' sec, mph: '+milesPerHour.toFixed(2)));
  60.         }
  61.         if (timeDiff > 0 && timeDiff === timeDiff) { spots[i].timeFromLast = timeDiff }
  62.         if (milesPerHour > 0 && milesPerHour === milesPerHour) { spots[i].speed = milesPerHour }
  63.         spots[i].distFromLast = dist;
  64.  
  65.         /** push the spot to the pending updates array */
  66.         updateArr.push(spots[i]);
  67.         totalDist = totalDist + dist;
  68.       }
  69.     }
  70.   }
  71.  
  72.   /** save each spot in the pending updates array */
  73.   async.each(updateArr, function (data, callback) {
  74.     data.save(function (err, savedSpot) {
  75.       if (err) {
  76.         console.log('Error updating mileage for spot ' + data.id, err);
  77.       }
  78.     });
  79.   }, function(err) {
  80.     if(err) {
  81.       console.log('Errors occurred during spot mileage update iteration:', err);
  82.     }
  83.   });
  84.  
  85. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement