@raruto/leaflet-elevation 1.9.3 → 1.9.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/speed.js CHANGED
@@ -13,6 +13,49 @@ Elevation.addInitHook(function() {
13
13
  speed.label = opts.speedLabel || L._(opts.imperial ? 'mph' : 'km/h');
14
14
  opts.speedFactor = opts.speedFactor || 1;
15
15
 
16
+
17
+ this._registerDataAttribute({
18
+ name: 'speed',
19
+ init: () => {
20
+ // this.track_info.speed_max = -Infinity; // Speed Max
21
+ // this.track_info.speed_min = +Infinity; // Speed Min
22
+ // this.track_info.speed_avg = 0; // Speed Avg
23
+ },
24
+ fetch: (i) => {
25
+ let data = this._data;
26
+ let dx = (data[i].dist - data[i > 0 ? i - 1 : i].dist) * 1000;
27
+ let dt = data[i].time - data[ i > 0 ? i - 1 : i].time;
28
+ return dt > 0 ? Math.abs((dx / dt) * opts.timeFactor) * opts.speedFactor : 0;
29
+ },
30
+ update: (speed, i) => {
31
+ this.track_info.speed_max = this.track_info.speed_max || -Infinity; // Speed Max
32
+ this.track_info.speed_min = this.track_info.speed_min || +Infinity; // Speed Min
33
+ this.track_info.speed_avg = this.track_info.speed_avg || 0; // Speed Avg
34
+
35
+ let data = this._data;
36
+ let track = this.track_info;
37
+
38
+ // Try to smooth "crazy" speed values.
39
+ if (this.options.speedDeltaMax) {
40
+ let delta = speed - data[i > 0 ? i - 1 : i].speed;
41
+ let deltaMax = this.options.speedDeltaMax;
42
+ if (Math.abs(delta) > deltaMax) {
43
+ speed = data[i - 1].speed + deltaMax * Math.sign(delta);
44
+ }
45
+ }
46
+
47
+ // Range of acceptable speed values.
48
+ speed = _.clamp(speed, this.options.speedRange);
49
+
50
+ if (speed > track.speed_max) track.speed_max = speed;
51
+ if (speed < track.speed_min) track.speed_min = speed;
52
+
53
+ track.speed_avg = (speed + track.speed_avg) / 2.0;
54
+
55
+ return L.Util.formatNum(speed, 2);
56
+ },
57
+ });
58
+
16
59
  if (this.options.speed && this.options.speed != "summary") {
17
60
 
18
61
  this._registerAxisScale({
@@ -26,16 +69,16 @@ Elevation.addInitHook(function() {
26
69
  label : speed.label,
27
70
  labelX : 25,
28
71
  labelY : -8,
29
- name : "speed"
72
+ name : "speed",
30
73
  });
31
74
 
32
75
  this._registerAreaPath({
33
- name : 'speed',
34
- label : 'Speed',
35
- yAttr : "speed",
36
- scaleX : 'distance',
37
- scaleY : 'speed',
38
- color : '#03ffff',
76
+ name : 'speed',
77
+ label : 'Speed',
78
+ yAttr : "speed",
79
+ scaleX : 'distance',
80
+ scaleY : 'speed',
81
+ color : '#03ffff',
39
82
  strokeColor : '#000',
40
83
  strokeOpacity: "0.5",
41
84
  fillOpacity : "0.25",
@@ -43,65 +86,30 @@ Elevation.addInitHook(function() {
43
86
 
44
87
  }
45
88
 
46
- this.on('elepoint_added', function(e) {
47
- let data = this._data;
48
- let i = e.index;
49
-
50
- let currT = data[i].time;
51
- let prevT = i > 0 ? data[i - 1].time : currT;
52
-
53
- let deltaT = currT - prevT;
54
-
55
- let track = this.track_info;
56
- let sMax = track.speed_max || -Infinity; // Speed Max
57
- let sMin = track.speed_min || +Infinity; // Speed Min
58
- let sAvg = track.speed_avg || 0; // Speed Avg
59
- let speed = 0;
60
-
61
- if (deltaT > 0) {
62
- let delta = (data[i].dist - data[i > 0 ? i - 1 : i].dist) * 1000;
63
- speed = Math.abs((delta / deltaT) * opts.timeFactor) * opts.speedFactor;
64
- }
65
-
66
- // Try to smooth "crazy" speed values.
67
- if (this.options.speedDeltaMax) {
68
- let deltaS = i > 0 ? speed - data[i - 1].speed : 0;
69
- let maxDeltaS = this.options.speedDeltaMax;
70
- if (Math.abs(deltaS) > maxDeltaS) {
71
- speed = data[i - 1].speed + maxDeltaS * Math.sign(deltaS);
72
- }
73
- }
74
-
75
- // Range of acceptable speed values.
76
- speed = _.clamp(speed, this.options.speedRange);
77
-
78
- track.speed_max = speed > sMax ? speed : sMax;
79
- track.speed_min = speed < sMin ? speed : sMin;
80
- track.speed_avg = (speed + sAvg) / 2.0;
81
-
82
- data[i].speed = L.Util.formatNum(speed, 2);
83
- });
84
-
85
89
  if (this.options.speed) {
86
90
 
87
91
  this._registerTooltip({
88
92
  name: 'speed',
89
93
  chart: (item) => L._('v: ') + item.speed + " " + speed.label,
90
94
  marker: (item) => Math.round(item.speed) + " " + speed.label,
95
+ order: 50,
91
96
  });
92
97
 
93
98
  this._registerSummary({
94
99
  "minspeed" : {
95
100
  label: "Min Speed: ",
96
- value: (track) => Math.round(track.speed_min || 0) + '&nbsp;' + speed.label
101
+ value: (track) => Math.round(track.speed_min || 0) + '&nbsp;' + speed.label,
102
+ order: 50
97
103
  },
98
104
  "maxspeed" : {
99
105
  label: "Max Speed: ",
100
- value: (track) => Math.round(track.speed_max || 0) + '&nbsp;' + speed.label
106
+ value: (track) => Math.round(track.speed_max || 0) + '&nbsp;' + speed.label,
107
+ order: 50
101
108
  },
102
109
  "avgspeed": {
103
110
  label: "Avg Speed: ",
104
- value: (track) => Math.round(track.speed_avg || 0) + '&nbsp;' + speed.label
111
+ value: (track) => Math.round(track.speed_avg || 0) + '&nbsp;' + speed.label,
112
+ order: 50
105
113
  }
106
114
  });
107
115
 
package/src/summary.js CHANGED
@@ -6,6 +6,7 @@ export var Summary = L.Class.extend({
6
6
  initialize: function(opts, control) {
7
7
  this.options = opts;
8
8
  this.control = control;
9
+ this.labels = {};
9
10
 
10
11
  let summary = this._container = _.create("div", "elevation-summary " + (opts.summary ? opts.summary + "-summary" : ''));
11
12
  _.style(summary, 'max-width', opts.width ? opts.width + 'px' : '');
@@ -24,9 +25,19 @@ export var Summary = L.Class.extend({
24
25
  return this;
25
26
  },
26
27
 
28
+ update: function() {
29
+ Object
30
+ .keys(this.labels)
31
+ .sort((a, b) => this.labels[a].order - this.labels[b].order) // TODO: any performance issues?
32
+ .forEach((i)=> {
33
+ this.append(i, L._(this.labels[i].label), typeof this.labels[i].value !== "function" ? this.labels[i].value : this.labels[i].value(this.control.track_info));
34
+ });
35
+ },
36
+
27
37
  _registerSummary: function(data) {
28
38
  for (let i in data) {
29
- this.append(i, L._(data[i].label), typeof data[i].value !== "function" ? data[i].value : data[i].value(this.control.track_info));
39
+ data[i].order = data[i].order ?? 100;
40
+ this.labels[i] = data[i];
30
41
  }
31
42
  }
32
43
 
package/src/time.js CHANGED
@@ -15,7 +15,7 @@ Elevation.addInitHook(function() {
15
15
  * Common AVG speeds:
16
16
  * ----------------------
17
17
  * slow walk = 1.8 km/h
18
- * walking = 3.6 km/h
18
+ * walking = 3.6 km/h <-- default: 3.6
19
19
  * running = 10.8 km/h
20
20
  * cycling = 18 km/h
21
21
  * driving = 72 km/h
@@ -33,6 +33,43 @@ Elevation.addInitHook(function() {
33
33
 
34
34
  opts.xTimeFormat = opts.xTimeFormat || ((t) => _.formatTime(t).split("'")[0]);
35
35
 
36
+ this._registerDataAttribute({
37
+ name: 'time',
38
+ init: function({ point, props, id }) {
39
+ // "coordinateProperties" property is generated inside "@tmcw/toGeoJSON"
40
+ if (props) {
41
+ if("coordTimes" in props) point.meta.time = new Date(Date.parse(props.coordTimes[id]));
42
+ else if("times" in props) point.meta.time = new Date(Date.parse(props.times[id]));
43
+ else if("time" in props) point.meta.time = new Date(Date.parse((typeof props.time === 'object' ? props.time[id] : props.time)));
44
+ }
45
+ },
46
+ fetch: function(i, point) {
47
+ // Add missing timestamps (see: options.timeAVGSpeed)
48
+ if (!point.meta || !point.meta.time) {
49
+ point.meta = point.meta || {};
50
+ if(i > 0) {
51
+ let dx = (this._data[i].dist - this._data[i - 1].dist);
52
+ let t0 = this._data[i - 1].time.getTime();
53
+ let dt = ( dx / this._timeAVGSpeed) * this.options.timeFactor * 1000;
54
+ point.meta.time = new Date(t0 + dt);
55
+ } else {
56
+ point.meta.time = new Date(Date.now())
57
+ }
58
+ }
59
+ let time = point.meta.time;
60
+ // Handle timezone offset
61
+ if (time.getTime() - time.getTimezoneOffset() * 60 * 1000 === 0) {
62
+ time = 0;
63
+ }
64
+ return time;
65
+ },
66
+ update: function(time, i) {
67
+ this.track_info.time = (this.track_info.time || 0) + Math.abs(this._data[i].time - this._data[i > 0 ? i - 1 : i].time);
68
+ this._data[i].duration = this.track_info.time;
69
+ return time;
70
+ }
71
+ });
72
+
36
73
  if (opts.time && opts.time != "summary" && !L.Browser.mobile) {
37
74
 
38
75
  this._registerAxisScale({
@@ -63,60 +100,25 @@ Elevation.addInitHook(function() {
63
100
 
64
101
  }
65
102
 
66
- this.on('elepoint_added', function(e) {
67
- let data = this._data;
68
- let i = e.index;
69
-
70
- // Add missing timestamps (see: options.timeAVGSpeed)
71
- if (!e.point.meta || !e.point.meta.time) {
72
- e.point.meta = e.point.meta || {};
73
- let delta = (data[i].dist - data[i > 0 ? i - 1 : i].dist);
74
- let speed = this._timeAVGSpeed;
75
- e.point.meta.time = new Date(
76
- (
77
- i > 0
78
- ? data[i - 1].time.getTime() + ((( delta / speed) * opts.timeFactor) * 1000)
79
- : Date.now()
80
- )
81
- );
82
- }
83
-
84
- let time = e.point.meta.time;
85
-
86
- // Handle timezone offset
87
- if (time.getTime() - time.getTimezoneOffset() * 60 * 1000 === 0) {
88
- time = 0;
89
- }
90
-
91
- let currT = time;
92
- let prevT = i > 0 ? data[i - 1].time : currT;
93
-
94
- let deltaT = Math.abs(currT - prevT);
95
- let duration = (this.track_info.time || 0) + deltaT;
96
-
97
- this.track_info.time = duration;
98
-
99
- data[i].time = time;
100
- data[i].duration = duration;
101
- });
102
-
103
-
104
103
  if (this.options.timestamps) {
105
104
  this._registerTooltip({
106
105
  name: 'date',
107
- chart: (item) => L._("t: ") + this.options.timeFormat(item.time)
106
+ chart: (item) => L._("t: ") + this.options.timeFormat(item.time),
107
+ order: 20,
108
108
  });
109
109
  }
110
110
 
111
111
  if (this.options.time) {
112
112
  this._registerTooltip({
113
113
  name: 'time',
114
- chart: (item) => L._("T: ") + _.formatTime(item.duration || 0)
114
+ chart: (item) => L._("T: ") + _.formatTime(item.duration || 0),
115
+ order: 20
115
116
  });
116
117
  this._registerSummary({
117
118
  "tottime" : {
118
119
  label: "Total Time: ",
119
- value: (track) => _.formatTime(track.time || 0)
120
+ value: (track) => _.formatTime(track.time || 0),
121
+ order: 20
120
122
  }
121
123
  });
122
124
  }