Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
python-visualization
GitHub Repository: python-visualization/folium
Path: blob/main/folium/plugins/timestamped_wmstilelayer.py
2541 views
1
from branca.element import MacroElement
2
3
from folium.elements import JSCSSMixin
4
from folium.raster_layers import WmsTileLayer
5
from folium.template import Template
6
from folium.utilities import remove_empty
7
8
9
class TimestampedWmsTileLayers(JSCSSMixin, MacroElement):
10
"""
11
Creates a TimestampedWmsTileLayer that takes a WmsTileLayer and adds time
12
control with the Leaflet.TimeDimension plugin.
13
14
Parameters
15
----------
16
data: WmsTileLayer.
17
The WmsTileLayer that you want to add time support to.
18
Must be created like a typical WmsTileLayer and added to the map
19
before being passed to this class.
20
21
transition_time: int, default 200.
22
The duration in ms of a transition from between timestamps.
23
loop: bool, default False
24
Whether the animation shall loop, default is to reduce load on WMS
25
services.
26
auto_play: bool, default False
27
Whether the animation shall start automatically at startup, default
28
is to reduce load on WMS services.
29
period: str, default 'P1D'
30
Used to construct the array of available times starting
31
from the first available time. Format: ISO8601 Duration
32
ex: 'P1M' -> 1/month, 'P1D' -> 1/day, 'PT1H' -> 1/hour, and 'PT1M' -> 1/minute
33
Note: this seems to be overridden by the WMS Tile Layer GetCapabilities.
34
35
Examples
36
--------
37
>>> w0 = WmsTileLayer(
38
... "http://this.wms.server/ncWMS/wms",
39
... name="Test WMS Data",
40
... styles="",
41
... fmt="image/png",
42
... transparent=True,
43
... layers="test_data",
44
... COLORSCALERANGE="0,10",
45
... )
46
>>> w0.add_to(m)
47
>>> w1 = WmsTileLayer(
48
... "http://this.wms.server/ncWMS/wms",
49
... name="Test WMS Data",
50
... styles="",
51
... fmt="image/png",
52
... transparent=True,
53
... layers="test_data_2",
54
... COLORSCALERANGE="0,5",
55
... )
56
>>> w1.add_to(m)
57
>>> # Add WmsTileLayers to time control.
58
>>> time = TimestampedWmsTileLayers([w0, w1])
59
>>> time.add_to(m)
60
61
See https://github.com/socib/Leaflet.TimeDimension for more information.
62
63
"""
64
65
_template = Template("""
66
{% macro script(this, kwargs) %}
67
{{ this._parent.get_name() }}.timeDimension = L.timeDimension(
68
{{ this.options|tojavascript }}
69
);
70
{{ this._parent.get_name() }}.timeDimensionControl =
71
L.control.timeDimension(
72
{{ this.options_control|tojavascript }}
73
);
74
{{ this._parent.get_name() }}.addControl(
75
{{ this._parent.get_name() }}.timeDimensionControl
76
);
77
78
{% for layer in this.layers %}
79
var {{ layer.get_name() }} = L.timeDimension.layer.wms(
80
{{ layer.get_name() }},
81
{
82
updateTimeDimension: false,
83
wmsVersion: {{ layer.options['version']|tojson }},
84
}
85
).addTo({{ this._parent.get_name() }});
86
{% endfor %}
87
{% endmacro %}
88
""")
89
90
default_js = [
91
(
92
"jquery3.7.1",
93
"https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js",
94
),
95
(
96
"jqueryui1.10.2",
97
"https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js",
98
),
99
(
100
"iso8601",
101
"https://cdn.jsdelivr.net/npm/[email protected]/iso8601.min.js",
102
),
103
(
104
"leaflet.timedimension",
105
"https://cdn.jsdelivr.net/npm/[email protected]/dist/leaflet.timedimension.min.js",
106
),
107
]
108
default_css = [
109
(
110
"highlight.js_css",
111
"https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/styles/default.min.css",
112
),
113
(
114
"leaflet.timedimension_css",
115
"https://cdn.jsdelivr.net/npm/[email protected]/dist/leaflet.timedimension.control.css",
116
),
117
]
118
119
def __init__(
120
self,
121
data,
122
transition_time=200,
123
loop=False,
124
auto_play=False,
125
period="P1D",
126
time_interval=False,
127
):
128
super().__init__()
129
self._name = "TimestampedWmsTileLayers"
130
self.options = remove_empty(
131
period=period,
132
time_interval=time_interval,
133
)
134
self.options_control = dict(
135
position="bottomleft",
136
auto_play=auto_play,
137
player_options={
138
"transitionTime": int(transition_time),
139
"loop": loop,
140
},
141
)
142
if isinstance(data, WmsTileLayer):
143
self.layers = [data]
144
else:
145
self.layers = data # Assume iterable
146
147