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
19 changes: 14 additions & 5 deletions folium/plugins/heat_map_withtime.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from folium.elements import JSCSSMixin
from folium.map import Layer
from folium.template import Template
from folium.utilities import none_max, none_min
from folium.utilities import if_pandas_df_convert_to_numpy, none_max, none_min


class HeatMapWithTime(JSCSSMixin, Layer):
Expand Down Expand Up @@ -147,7 +147,7 @@ class HeatMapWithTime(JSCSSMixin, Layer):
{times : times, currentTime: new Date(1)}
);

var {{this._control_name}} = new L.Control.TimeDimensionCustom({{this.index}}, {
var {{this._control_name}} = new L.Control.TimeDimensionCustom({{ this.index|tojson }}, {
autoPlay: {{this.auto_play}},
backwardButton: {{this.backward_button}},
displayDate: {{this.display_index}},
Expand All @@ -169,7 +169,7 @@ class HeatMapWithTime(JSCSSMixin, Layer):
})
.addTo({{this._parent.get_name()}});

var {{this.get_name()}} = new TDHeatmap({{this.data}},
var {{this.get_name()}} = new TDHeatmap({{ this.data|tojson }},
{heatmapOptions: {
radius: {{this.radius}},
blur: {{this.blur}},
Expand Down Expand Up @@ -238,9 +238,18 @@ def __init__(
self._control_name = self.get_name() + "Control"

# Input data.
self.data = data
# Plain floats and strings, so that numpy and pandas input renders as JSON.
self.data = [
[
[float(value) for value in point]
for point in if_pandas_df_convert_to_numpy(frame)
]
for frame in data
]
self.index = (
index if index is not None else [str(i) for i in range(1, len(data) + 1)]
[str(label) for label in index]
if index is not None
else [str(i) for i in range(1, len(data) + 1)]
)
if len(self.data) != len(self.index):
raise ValueError(
Expand Down
30 changes: 28 additions & 2 deletions 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 @@ -41,7 +42,7 @@ def test_heat_map_with_time():
{times : times, currentTime: new Date(1)}
);

var {{this._control_name}} = new L.Control.TimeDimensionCustom({{this.index}}, {
var {{this._control_name}} = new L.Control.TimeDimensionCustom({{ this.index|tojson }}, {
autoPlay: {{this.auto_play}},
backwardButton: {{this.backward_button}},
displayDate: {{this.display_index}},
Expand All @@ -63,7 +64,7 @@ def test_heat_map_with_time():
})
.addTo({{this._parent.get_name()}});

var {{this.get_name()}} = new TDHeatmap({{this.data}},
var {{this.get_name()}} = new TDHeatmap({{ this.data|tojson }},
{heatmapOptions: {
radius: {{this.radius}},
blur: {{this.blur}},
Expand Down Expand Up @@ -93,3 +94,28 @@ 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]]


def test_heat_map_with_time_numpy_and_pandas_input():
pd = pytest.importorskip("pandas")
data = np.array([[[48.0, 5.0], [49.0, 6.0]], [[48.5, 5.5], [49.5, 6.5]]])
index = pd.to_datetime(["2020-01-01", "2020-01-02"])
m = folium.Map()
hm = plugins.HeatMapWithTime(data, index=index).add_to(m)

out = normalize(m._parent.render())

expected_data = "[[[48.0,5.0],[49.0,6.0]],[[48.5,5.5],[49.5,6.5]]]"
assert f"new TDHeatmap({expected_data}," in out
expected_index = '["2020-01-01 00:00:00","2020-01-02 00:00:00"]'
assert f"new L.Control.TimeDimensionCustom({expected_index}," in out
assert hm.get_bounds() == [[48.0, 5.0], [49.5, 6.5]]

frames = [
pd.DataFrame({"lat": [48.0, 49.0], "lng": [5.0, 6.0], "w": [0.5, 1.0]}),
pd.DataFrame({"lat": [50.0], "lng": [7.0], "w": [0.2]}),
]
hm = plugins.HeatMapWithTime(frames).add_to(m)
out = normalize(m._parent.render())
expected_data = "[[[48.0,5.0,0.5],[49.0,6.0,1.0]],[[50.0,7.0,0.2]]]"
assert f"new TDHeatmap({expected_data}," in out