[PLUGINS] +clavettes et dependances
[lhc/web/clavette_www.git] / www / plugins / gis / lib / leaflet / plugins / GPX.Speed.js
1 //#include 'GPX.js'
2
3 (function() {
4
5 function d2h(d) {
6 var hex = '0123456789ABCDEF';
7 var r = '';
8 d = Math.floor(d);
9 while (d !== 0) {
10 r = hex[d % 16] + r;
11 d = Math.floor(d / 16);
12 }
13 while (r.length < 2) r = '0' + r;
14 return r;
15 }
16
17 function gradient(color) {
18 // First arc (0, PI) in HSV colorspace
19 function f2h(d) { return d2h(256 * d); }
20 if (color < 0)
21 return '#FF0000';
22 else if (color < 1.0/3)
23 return '#FF' + f2h(3 * color) + '00';
24 else if (color < 2.0/3)
25 return '#' + f2h(2 - 3 * color) + 'FF00';
26 else if (color < 1)
27 return '#00FF' + f2h(3 * color - 2);
28 else
29 return '#00FFFF';
30 }
31
32 function gpx2time(s) {
33 // 2011-09-24T12:07:53Z
34 if (s.length !== 10 + 1 + 8 + 1)
35 return new Date();
36 return new Date(s);
37 }
38
39 L.GPX.include({
40 options: {
41 maxSpeed: 110,
42 chunks: 200
43 },
44
45 speedSplitEnable: function(options) {
46 L.Util.setOptions(this, options);
47 return this.on('addline', this.speed_split, this);
48 },
49
50 speedSplitDisable: function() {
51 return this.off('addline', this.speed_split, this);
52 },
53
54 speed_split: function(e) {
55 var l = e.line.pop(), ll = l.getLatLngs();
56 var chunk = Math.floor(ll.length / this.options.chunks);
57 if (chunk < 3) chunk = 3;
58 var p = null;
59 for (var i = 0; i < ll.length; i += chunk) {
60 var d = 0, t = null;
61 if (i + chunk > ll.length)
62 chunk = ll.length - i;
63 for (var j = 0; j < chunk; j++) {
64 if (p) d += p.distanceTo(ll[i+j]);
65 p = ll[i + j];
66 if (!t) t = gpx2time(p.meta.time);
67 }
68 p = ll[i + chunk - 1];
69 t = (gpx2time(p.meta.time) - t) / (3600 * 1000);
70 var speed = 0.001 * d / t;
71 //console.info('Dist: ' + d + '; Speed: ' + speed);
72 var color = gradient(speed / this.options.maxSpeed);
73 var poly = new L.Polyline(ll.slice(i, i+chunk+1), {color: color, weight: 2, opacity: 1});
74 poly.bindPopup('Dist: ' + d.toFixed() + 'm; Speed: ' + speed.toFixed(2) + ' km/h');
75 e.line.push(poly);
76 }
77 }
78
79 });
80 })();