Skip to content

Commit d45e306

Browse files
committed
Cleanup MacroElements
Do heat_map_withtime.py
1 parent 8025125 commit d45e306

File tree

1 file changed

+81
-97
lines changed

1 file changed

+81
-97
lines changed

folium/plugins/heat_map_withtime.py

Lines changed: 81 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from branca.element import Element, Figure
2-
31
from folium.elements import JSCSSMixin
42
from folium.map import Layer
53
from folium.template import Template
@@ -61,6 +59,87 @@ class HeatMapWithTime(JSCSSMixin, Layer):
6159

6260
_template = Template(
6361
"""
62+
{% macro header(this, kwargs) %}
63+
<script>
64+
var TDHeatmap = L.TimeDimension.Layer.extend({
65+
initialize: function(data, options) {
66+
var heatmapCfg = {
67+
radius: 15,
68+
blur: 0.8,
69+
maxOpacity: 1.,
70+
scaleRadius: false,
71+
useLocalExtrema: false,
72+
latField: 'lat',
73+
lngField: 'lng',
74+
valueField: 'count',
75+
defaultWeight : 1,
76+
};
77+
heatmapCfg = $.extend({}, heatmapCfg, options.heatmapOptions || {});
78+
var layer = new HeatmapOverlay(heatmapCfg);
79+
L.TimeDimension.Layer.prototype.initialize.call(this, layer, options);
80+
this._currentLoadedTime = 0;
81+
this._currentTimeData = {
82+
data: []
83+
};
84+
this.data= data;
85+
this.defaultWeight = heatmapCfg.defaultWeight || 1;
86+
},
87+
onAdd: function(map) {
88+
L.TimeDimension.Layer.prototype.onAdd.call(this, map);
89+
map.addLayer(this._baseLayer);
90+
if (this._timeDimension) {
91+
this._getDataForTime(this._timeDimension.getCurrentTime());
92+
}
93+
},
94+
_onNewTimeLoading: function(ev) {
95+
this._getDataForTime(ev.time);
96+
return;
97+
},
98+
isReady: function(time) {
99+
return (this._currentLoadedTime == time);
100+
},
101+
_update: function() {
102+
this._baseLayer.setData(this._currentTimeData);
103+
return true;
104+
},
105+
_getDataForTime: function(time) {
106+
delete this._currentTimeData.data;
107+
this._currentTimeData.data = [];
108+
var data = this.data[time-1];
109+
for (var i = 0; i < data.length; i++) {
110+
this._currentTimeData.data.push({
111+
lat: data[i][0],
112+
lng: data[i][1],
113+
count: data[i].length>2 ? data[i][2] : this.defaultWeight
114+
});
115+
}
116+
this._currentLoadedTime = time;
117+
if (this._timeDimension && time == this._timeDimension.getCurrentTime() && !this._timeDimension.isLoading()) {
118+
this._update();
119+
}
120+
this.fire('timeload', {
121+
time: time
122+
});
123+
}
124+
});
125+
126+
L.Control.TimeDimensionCustom = L.Control.TimeDimension.extend({
127+
initialize: function(index, options) {
128+
var playerOptions = {
129+
buffer: 1,
130+
minBufferReady: -1
131+
};
132+
options.playerOptions = $.extend({}, playerOptions, options.playerOptions || {});
133+
L.Control.TimeDimension.prototype.initialize.call(this, options);
134+
this.index = index;
135+
},
136+
_getDisplayDateFormat: function(date) {
137+
return this.index[date.getTime()-1];
138+
}
139+
});
140+
</script>
141+
{% endmacro %}
142+
64143
{% macro script(this, kwargs) %}
65144
66145
var times = {{this.times}};
@@ -202,101 +281,6 @@ def __init__(
202281
self.time_slider_drag_update = "false"
203282
self.style_NS = "leaflet-control-timecontrol"
204283

205-
def render(self, **kwargs):
206-
super().render(**kwargs)
207-
208-
figure = self.get_root()
209-
assert isinstance(
210-
figure, Figure
211-
), "You cannot render this Element if it is not in a Figure."
212-
213-
figure.header.add_child(
214-
Element(
215-
"""
216-
<script>
217-
var TDHeatmap = L.TimeDimension.Layer.extend({
218-
219-
initialize: function(data, options) {
220-
var heatmapCfg = {
221-
radius: 15,
222-
blur: 0.8,
223-
maxOpacity: 1.,
224-
scaleRadius: false,
225-
useLocalExtrema: false,
226-
latField: 'lat',
227-
lngField: 'lng',
228-
valueField: 'count',
229-
defaultWeight : 1,
230-
};
231-
heatmapCfg = $.extend({}, heatmapCfg, options.heatmapOptions || {});
232-
var layer = new HeatmapOverlay(heatmapCfg);
233-
L.TimeDimension.Layer.prototype.initialize.call(this, layer, options);
234-
this._currentLoadedTime = 0;
235-
this._currentTimeData = {
236-
data: []
237-
};
238-
this.data= data;
239-
this.defaultWeight = heatmapCfg.defaultWeight || 1;
240-
},
241-
onAdd: function(map) {
242-
L.TimeDimension.Layer.prototype.onAdd.call(this, map);
243-
map.addLayer(this._baseLayer);
244-
if (this._timeDimension) {
245-
this._getDataForTime(this._timeDimension.getCurrentTime());
246-
}
247-
},
248-
_onNewTimeLoading: function(ev) {
249-
this._getDataForTime(ev.time);
250-
return;
251-
},
252-
isReady: function(time) {
253-
return (this._currentLoadedTime == time);
254-
},
255-
_update: function() {
256-
this._baseLayer.setData(this._currentTimeData);
257-
return true;
258-
},
259-
_getDataForTime: function(time) {
260-
delete this._currentTimeData.data;
261-
this._currentTimeData.data = [];
262-
var data = this.data[time-1];
263-
for (var i = 0; i < data.length; i++) {
264-
this._currentTimeData.data.push({
265-
lat: data[i][0],
266-
lng: data[i][1],
267-
count: data[i].length>2 ? data[i][2] : this.defaultWeight
268-
});
269-
}
270-
this._currentLoadedTime = time;
271-
if (this._timeDimension && time == this._timeDimension.getCurrentTime() && !this._timeDimension.isLoading()) {
272-
this._update();
273-
}
274-
this.fire('timeload', {
275-
time: time
276-
});
277-
}
278-
});
279-
280-
L.Control.TimeDimensionCustom = L.Control.TimeDimension.extend({
281-
initialize: function(index, options) {
282-
var playerOptions = {
283-
buffer: 1,
284-
minBufferReady: -1
285-
};
286-
options.playerOptions = $.extend({}, playerOptions, options.playerOptions || {});
287-
L.Control.TimeDimension.prototype.initialize.call(this, options);
288-
this.index = index;
289-
},
290-
_getDisplayDateFormat: function(date){
291-
return this.index[date.getTime()-1];
292-
}
293-
});
294-
</script>
295-
""", # noqa
296-
template_name="timeControlScript",
297-
)
298-
)
299-
300284
def _get_self_bounds(self):
301285
"""
302286
Computes the bounds of the object itself (not including it's children)

0 commit comments

Comments
 (0)