tracks.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. class GPS {
  2. parse(trips) {
  3. this.trips = trips
  4. this.regions = this.getRegions()
  5. this.data = this.trips.map(t => t.path).reduce((a, b) => a.concat(b), [])
  6. }
  7. getRegions() {
  8. var trips = [];
  9. var s = 0;
  10. for(let i=0; i<=this.trips.length; i++) {
  11. while(i < this.trips.length - 2) {
  12. const cur = this.trips[i];
  13. const nxt = this.trips[i+1];
  14. if((nxt.startTime - cur.endTime) > 3600 * 24 * 7 || distance(cur.center, nxt.center) > 100000) {
  15. break;
  16. }
  17. i++;
  18. }
  19. let trip = this.trips.slice(s, i);
  20. let dist = trip.reduce((sum, p) => p.distance + sum, 0);
  21. if (dist > 30000) {
  22. trips.push({
  23. lat: median(trip.map(v => v.center.lat)),
  24. lng: median(trip.map(v => v.center.lng)),
  25. timestamp: median(trip.map(v => v.startTime)),
  26. distance: dist
  27. });
  28. }
  29. s = i;
  30. }
  31. return trips;
  32. }
  33. getInfo() {
  34. let i = {
  35. points: this.trips.reduce((sum, p) => p.path.length + sum, 0),
  36. distance: this.trips.reduce((sum, p) => p.distance + sum, 0),
  37. topSpeed: this.trips.reduce((max, p) => p.topSpeed > max ? p.topSpeed : max, 0),
  38. ascendHeight: this.trips.reduce((sum, p) => p.ascendHeight + sum, 0),
  39. descendHeight: this.trips.reduce((sum, p) => p.descendHeight + sum, 0),
  40. movementTime: this.trips.reduce((sum, p) => p.movementTime + sum, 0),
  41. totalTime: this.trips.reduce((sum, p) => p.totalTime + sum, 0)
  42. };
  43. i.avgSpeed = i.distance / i.movementTime * 3.6;
  44. return i;
  45. }
  46. }
  47. function distance (a, b) {
  48. // Convert degrees to radians
  49. var lat1 = a.lat * Math.PI / 180.0;
  50. var lon1 = a.lng * Math.PI / 180.0;
  51. var lat2 = b.lat * Math.PI / 180.0;
  52. var lon2 = b.lng * Math.PI / 180.0;
  53. // radius of earth in metres
  54. var r = 6378100;
  55. // P
  56. var rho1 = r * Math.cos(lat1);
  57. var z1 = r * Math.sin(lat1);
  58. var x1 = rho1 * Math.cos(lon1);
  59. var y1 = rho1 * Math.sin(lon1);
  60. // Q
  61. var rho2 = r * Math.cos(lat2);
  62. var z2 = r * Math.sin(lat2);
  63. var x2 = rho2 * Math.cos(lon2);
  64. var y2 = rho2 * Math.sin(lon2);
  65. // Dot product
  66. var dot = (x1 * x2 + y1 * y2 + z1 * z2);
  67. var cos_theta = dot / (r * r);
  68. var theta = Math.acos(cos_theta);
  69. // Distance in Metres
  70. return r * theta;
  71. }
  72. function timeFormat(sec, x = "time") {
  73. if(isNaN(sec))
  74. return "NaN";
  75. let d = new Date(sec*1000);
  76. let t = new Date(null,null,null,null,null,sec).toTimeString().match(/\d{2}:\d{2}:\d{2}/)[0];
  77. switch(x) {
  78. case "time":
  79. if(sec < 3600*24)
  80. return t;
  81. else
  82. return Math.floor(sec/3600/24) + "d " + t;
  83. case "datetime":
  84. return d.toLocaleDateString('de-DE', {
  85. year: "numeric",
  86. month: "2-digit",
  87. day: "2-digit",
  88. hour: "2-digit",
  89. minute: "2-digit",
  90. second: "2-digit",
  91. });
  92. case "date":
  93. return d.toLocaleDateString('de-DE', {
  94. year: "numeric",
  95. month: "2-digit",
  96. day: "2-digit"
  97. });
  98. case "short":
  99. return d.toLocaleDateString('de-DE', {
  100. year: "numeric",
  101. month: "2-digit"
  102. });
  103. }
  104. }
  105. function median(values) {
  106. values.sort( function(a,b) {return a - b;} );
  107. var half = Math.floor(values.length/2);
  108. return values[half];
  109. }
  110. function avg(v) {
  111. return v.reduce((a,b) => a+b, 0)/v.length;
  112. }
  113. function smoothOut(vector, variance) {
  114. var t_avg = avg(vector)*variance;
  115. var ret = Array(vector.length);
  116. for (var i = 0; i < vector.length; i++) {
  117. (function () {
  118. var prev = i>0 ? ret[i-1] : vector[i];
  119. var next = i<vector.length ? vector[i] : vector[i-1];
  120. ret[i] = avg([t_avg, avg([prev, vector[i], next])]);
  121. })();
  122. }
  123. return ret;
  124. }