Path: blob/main/folium/plugins/timestamped_wmstilelayer.py
2541 views
from branca.element import MacroElement12from folium.elements import JSCSSMixin3from folium.raster_layers import WmsTileLayer4from folium.template import Template5from folium.utilities import remove_empty678class TimestampedWmsTileLayers(JSCSSMixin, MacroElement):9"""10Creates a TimestampedWmsTileLayer that takes a WmsTileLayer and adds time11control with the Leaflet.TimeDimension plugin.1213Parameters14----------15data: WmsTileLayer.16The WmsTileLayer that you want to add time support to.17Must be created like a typical WmsTileLayer and added to the map18before being passed to this class.1920transition_time: int, default 200.21The duration in ms of a transition from between timestamps.22loop: bool, default False23Whether the animation shall loop, default is to reduce load on WMS24services.25auto_play: bool, default False26Whether the animation shall start automatically at startup, default27is to reduce load on WMS services.28period: str, default 'P1D'29Used to construct the array of available times starting30from the first available time. Format: ISO8601 Duration31ex: 'P1M' -> 1/month, 'P1D' -> 1/day, 'PT1H' -> 1/hour, and 'PT1M' -> 1/minute32Note: this seems to be overridden by the WMS Tile Layer GetCapabilities.3334Examples35--------36>>> w0 = WmsTileLayer(37... "http://this.wms.server/ncWMS/wms",38... name="Test WMS Data",39... styles="",40... fmt="image/png",41... transparent=True,42... layers="test_data",43... COLORSCALERANGE="0,10",44... )45>>> w0.add_to(m)46>>> w1 = WmsTileLayer(47... "http://this.wms.server/ncWMS/wms",48... name="Test WMS Data",49... styles="",50... fmt="image/png",51... transparent=True,52... layers="test_data_2",53... COLORSCALERANGE="0,5",54... )55>>> w1.add_to(m)56>>> # Add WmsTileLayers to time control.57>>> time = TimestampedWmsTileLayers([w0, w1])58>>> time.add_to(m)5960See https://github.com/socib/Leaflet.TimeDimension for more information.6162"""6364_template = Template("""65{% macro script(this, kwargs) %}66{{ this._parent.get_name() }}.timeDimension = L.timeDimension(67{{ this.options|tojavascript }}68);69{{ this._parent.get_name() }}.timeDimensionControl =70L.control.timeDimension(71{{ this.options_control|tojavascript }}72);73{{ this._parent.get_name() }}.addControl(74{{ this._parent.get_name() }}.timeDimensionControl75);7677{% for layer in this.layers %}78var {{ layer.get_name() }} = L.timeDimension.layer.wms(79{{ layer.get_name() }},80{81updateTimeDimension: false,82wmsVersion: {{ layer.options['version']|tojson }},83}84).addTo({{ this._parent.get_name() }});85{% endfor %}86{% endmacro %}87""")8889default_js = [90(91"jquery3.7.1",92"https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js",93),94(95"jqueryui1.10.2",96"https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js",97),98(99"iso8601",100"https://cdn.jsdelivr.net/npm/[email protected]/iso8601.min.js",101),102(103"leaflet.timedimension",104"https://cdn.jsdelivr.net/npm/[email protected]/dist/leaflet.timedimension.min.js",105),106]107default_css = [108(109"highlight.js_css",110"https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/styles/default.min.css",111),112(113"leaflet.timedimension_css",114"https://cdn.jsdelivr.net/npm/[email protected]/dist/leaflet.timedimension.control.css",115),116]117118def __init__(119self,120data,121transition_time=200,122loop=False,123auto_play=False,124period="P1D",125time_interval=False,126):127super().__init__()128self._name = "TimestampedWmsTileLayers"129self.options = remove_empty(130period=period,131time_interval=time_interval,132)133self.options_control = dict(134position="bottomleft",135auto_play=auto_play,136player_options={137"transitionTime": int(transition_time),138"loop": loop,139},140)141if isinstance(data, WmsTileLayer):142self.layers = [data]143else:144self.layers = data # Assume iterable145146147