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
12 changes: 11 additions & 1 deletion folium/plugins/heat_map_withtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class HeatMapWithTime(JSCSSMixin, Layer):
Display the index (usually time) in the time control.
index_steps: default 1
Steps to take in the index dimension between animation steps.
start_index: int, default 0
Position (0-based) in `data` of the time step shown when the map opens.
Negative values count from the end, so -1 starts at the last time step.
min_speed: default 0.1
Minimum fps speed for animation.
max_speed: default 10
Expand Down Expand Up @@ -144,7 +147,7 @@ class HeatMapWithTime(JSCSSMixin, Layer):
var times = {{this.times}};

{{this._parent.get_name()}}.timeDimension = L.timeDimension(
{times : times, currentTime: new Date(1)}
{times : times, currentTime: new Date({{this.start_index + 1}})}
);

var {{this._control_name}} = new L.Control.TimeDimensionCustom({{this.index}}, {
Expand Down Expand Up @@ -225,6 +228,7 @@ def __init__(
auto_play=False,
display_index=True,
index_steps=1,
start_index=0,
min_speed=0.1,
max_speed=10,
speed_step=0.1,
Expand All @@ -247,6 +251,12 @@ def __init__(
"Input data and index are not of compatible lengths."
) # noqa
self.times = list(range(1, len(data) + 1))
if data and not -len(data) <= start_index < len(data):
raise ValueError(
f"start_index must be between {-len(data)} and {len(data) - 1}, "
f"got {start_index}."
)
self.start_index = start_index % len(data) if data else 0

# Heatmap settings.
self.radius = radius
Expand Down
21 changes: 20 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 @@ -38,7 +39,7 @@ def test_heat_map_with_time():
var times = {{this.times}};

{{this._parent.get_name()}}.timeDimension = L.timeDimension(
{times : times, currentTime: new Date(1)}
{times : times, currentTime: new Date({{this.start_index + 1}})}
);

var {{this._control_name}} = new L.Control.TimeDimensionCustom({{this.index}}, {
Expand Down Expand Up @@ -93,3 +94,21 @@ 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("start_index, expected", [(0, 1), (5, 6), (-1, 10)])
def test_heat_map_with_time_start_index(start_index, expected):
data = [[[48.0, 5.0]] for _ in range(10)]
m = folium.Map()
hm = plugins.HeatMapWithTime(data, start_index=start_index).add_to(m)

out = normalize(m._parent.render())
assert f"currentTime: new Date({expected})" in out
assert hm.start_index == expected - 1


@pytest.mark.parametrize("start_index", [10, -11])
def test_heat_map_with_time_start_index_out_of_range(start_index):
data = [[[48.0, 5.0]] for _ in range(10)]
with pytest.raises(ValueError, match="start_index"):
plugins.HeatMapWithTime(data, start_index=start_index)
Loading