Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
python-visualization
GitHub Repository: python-visualization/folium
Path: blob/main/folium/plugins/timestamped_wmstilelayer.py
1578 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
"""
67
{% macro script(this, kwargs) %}
68
{{ this._parent.get_name() }}.timeDimension = L.timeDimension(
69
{{ this.options|tojavascript }}
70
);
71
{{ this._parent.get_name() }}.timeDimensionControl =
72
L.control.timeDimension(
73
{{ this.options_control|tojavascript }}
74
);
75
{{ this._parent.get_name() }}.addControl(
76
{{ this._parent.get_name() }}.timeDimensionControl
77
);
78
79
{% for layer in this.layers %}
80
var {{ layer.get_name() }} = L.timeDimension.layer.wms(
81
{{ layer.get_name() }},
82
{
83
updateTimeDimension: false,
84
wmsVersion: {{ layer.options['version']|tojson }},
85
}
86
).addTo({{ this._parent.get_name() }});
87
{% endfor %}
88
{% endmacro %}
89
"""
90
)
91
92
default_js = [
93
(
94
"jquery3.7.1",
95
"https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js",
96
),
97
(
98
"jqueryui1.10.2",
99
"https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js",
100
),
101
(
102
"iso8601",
103
"https://cdn.jsdelivr.net/npm/[email protected]/iso8601.min.js",
104
),
105
(
106
"leaflet.timedimension",
107
"https://cdn.jsdelivr.net/npm/[email protected]/dist/leaflet.timedimension.min.js",
108
),
109
]
110
default_css = [
111
(
112
"highlight.js_css",
113
"https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/styles/default.min.css",
114
),
115
(
116
"leaflet.timedimension_css",
117
"https://cdn.jsdelivr.net/npm/[email protected]/dist/leaflet.timedimension.control.css",
118
),
119
]
120
121
def __init__(
122
self,
123
data,
124
transition_time=200,
125
loop=False,
126
auto_play=False,
127
period="P1D",
128
time_interval=False,
129
):
130
super().__init__()
131
self._name = "TimestampedWmsTileLayers"
132
self.options = remove_empty(
133
period=period,
134
time_interval=time_interval,
135
)
136
self.options_control = dict(
137
position="bottomleft",
138
auto_play=auto_play,
139
player_options={
140
"transitionTime": int(transition_time),
141
"loop": loop,
142
},
143
)
144
if isinstance(data, WmsTileLayer):
145
self.layers = [data]
146
else:
147
self.layers = data # Assume iterable
148
149