tracks.js 3.8 KB

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