Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/user_guide/plugins/heatmap_with_time.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,19 @@ hm.add_to(m)

m
```

### Drawing on top of image overlays

Image overlays cover the heatmap by default, even when the heatmap is added later. To draw the heatmap on top, put it in a custom pane with a higher `z_index`.

```{code-cell} ipython3
m = folium.Map([48.0, 5.0], zoom_start=6)

folium.map.CustomPane("heatmap", z_index=450).add_to(m)

hm = folium.plugins.HeatMapWithTime(data, pane="heatmap")

hm.add_to(m)

m
```
26 changes: 24 additions & 2 deletions folium/plugins/heat_map_withtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,31 @@ class HeatMapWithTime(JSCSSMixin, Layer):
Whether the Layer will be included in LayerControls.
show: bool, default True
Whether the layer will be shown on opening.
pane: str, default "overlayPane"
Name of the map pane the heatmap is drawn in. Use a
:class:`folium.map.CustomPane` with a higher ``z_index`` to draw the
heatmap on top of image overlays, which otherwise cover it.

"""

_template = Template("""
{% macro header(this, kwargs) %}
<script>
var PanedHeatmapOverlay = HeatmapOverlay.extend({
initialize: function(config, pane) {
HeatmapOverlay.prototype.initialize.call(this, config);
this._paneName = pane || 'overlayPane';
},
onAdd: function(map) {
HeatmapOverlay.prototype.onAdd.call(this, map);
map.getPane(this._paneName).appendChild(this._el);
},
onRemove: function(map) {
L.DomUtil.remove(this._el);
map.off('moveend', this._reset, this);
}
});

var TDHeatmap = L.TimeDimension.Layer.extend({
initialize: function(data, options) {
var heatmapCfg = {
Expand All @@ -74,7 +93,7 @@ class HeatMapWithTime(JSCSSMixin, Layer):
defaultWeight : 1,
};
heatmapCfg = $.extend({}, heatmapCfg, options.heatmapOptions || {});
var layer = new HeatmapOverlay(heatmapCfg);
var layer = new PanedHeatmapOverlay(heatmapCfg, options.pane);
L.TimeDimension.Layer.prototype.initialize.call(this, layer, options);
this._currentLoadedTime = 0;
this._currentTimeData = {
Expand Down Expand Up @@ -170,7 +189,8 @@ class HeatMapWithTime(JSCSSMixin, Layer):
.addTo({{this._parent.get_name()}});

var {{this.get_name()}} = new TDHeatmap({{this.data}},
{heatmapOptions: {
{pane: {{this.pane|tojson}},
heatmapOptions: {
radius: {{this.radius}},
blur: {{this.blur}},
minOpacity: {{this.min_opacity}},
Expand Down Expand Up @@ -232,6 +252,7 @@ def __init__(
overlay=True,
control=True,
show=True,
pane="overlayPane",
):
super().__init__(name=name, overlay=overlay, control=control, show=show)
self._name = "HeatMap"
Expand All @@ -256,6 +277,7 @@ def __init__(
self.scale_radius = "true" if scale_radius else "false"
self.use_local_extrema = "true" if use_local_extrema else "false"
self.gradient = gradient
self.pane = pane

# Time dimension settings.
self.auto_play = "true" if auto_play else "false"
Expand Down
18 changes: 17 additions & 1 deletion tests/plugins/test_heat_map_withtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""

import numpy as np
import pytest

import folium
from folium import plugins
Expand Down Expand Up @@ -64,7 +65,8 @@ def test_heat_map_with_time():
.addTo({{this._parent.get_name()}});

var {{this.get_name()}} = new TDHeatmap({{this.data}},
{heatmapOptions: {
{pane: {{this.pane|tojson}},
heatmapOptions: {
radius: {{this.radius}},
blur: {{this.blur}},
minOpacity: {{this.min_opacity}},
Expand Down Expand Up @@ -93,3 +95,17 @@ def test_heat_map_with_time_bounds():

assert hm.get_bounds() == [[48.0, 3.0], [52.0, 6.0]]
assert m.get_bounds() == [[48.0, 3.0], [52.0, 7.0]]


@pytest.mark.parametrize("pane", ["overlayPane", "heatmap"])
def test_heat_map_with_time_pane(pane):
data = [[[48.0, 5.0]], [[48.1, 5.1]]]
m = folium.Map()
if pane != "overlayPane":
folium.map.CustomPane(pane, z_index=450).add_to(m)
hm = plugins.HeatMapWithTime(data, pane=pane).add_to(m)

out = normalize(m._parent.render())
assert hm.pane == pane
assert f'{{pane: "{pane}",heatmapOptions:' in out
assert "new PanedHeatmapOverlay(heatmapCfg,options.pane)" in out
Loading