Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Calculate Mileage (written in JS)
- */
- function(err, spots) {
- if (err) throw err;
- var totalDist = 0;
- var dist = 0;
- var radlat1, radlat2, theta, radtheta;
- var timeDiff = 0;
- var diffPerHour = 0;
- var milesPerHour = 0;
- var updateArr = [];
- for(var i=1;i<spots.length;i++){
- if (spots[i].latitude === spots[i-1].latitude && spots[i].longitude === spots[i-1].longitude) {
- /** check for identical lat/lon coords to last spot(duplicate) and mark as invalid */
- spots[i].isValid = false;
- /** push the spot to the pending updates array */
- updateArr.push(spots[i]);
- } else {
- /** lat/long is different so some distance was travelled, do the math */
- /** distance calc from last spot */
- radlat1 = Math.PI * spots[i-1].latitude/180;
- radlat2 = Math.PI * spots[i].latitude/180;
- theta = spots[i-1].longitude-spots[i].longitude;
- radtheta = Math.PI * theta/180;
- dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
- dist = Math.acos(dist);
- dist = dist * 180/Math.PI;
- dist = dist * 60 * 1.1515;
- /** 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. */
- if (dist != dist) {
- console.log('distance calc returned NaN for:',spots[i-1].id,'to',spots[i].id);
- } else {
- /** if distance was valid, but less the margin of error, set distance to 1 foot(in miles) */
- if (dist === 0) { dist = 0.00018939393; }
- /** calculate time since last spot */
- timeDiff = spots[i].unixTime - spots[i-1].unixTime;
- if (timeDiff > 7200) {
- /** if it's been more than two hours, log a possible handoff or stop */
- console.log(color.yellow('Possible Handoff'));
- }
- diffPerHour = 3600/timeDiff;
- /** calculate time since last spot */
- milesPerHour = dist * diffPerHour;
- var minutes = Math.floor(Math.floor(timeDiff)/60);
- var calc = timeDiff - (parseInt(minutes)*60);
- /** if speed between stops is 15MPH or greater, log a warning */
- if (milesPerHour > 14.99) {
- 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)));
- }
- if (timeDiff > 0 && timeDiff === timeDiff) { spots[i].timeFromLast = timeDiff }
- if (milesPerHour > 0 && milesPerHour === milesPerHour) { spots[i].speed = milesPerHour }
- spots[i].distFromLast = dist;
- /** push the spot to the pending updates array */
- updateArr.push(spots[i]);
- totalDist = totalDist + dist;
- }
- }
- }
- /** save each spot in the pending updates array */
- async.each(updateArr, function (data, callback) {
- data.save(function (err, savedSpot) {
- if (err) {
- console.log('Error updating mileage for spot ' + data.id, err);
- }
- });
- }, function(err) {
- if(err) {
- console.log('Errors occurred during spot mileage update iteration:', err);
- }
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement