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