1313 Eventually, you may have Point features with a "times" property being an array of length 1.
1414"""
1515import json
16+ from jinja2 import Template
1617
17- from . plugin import Plugin
18+ from folium . element import MacroElement , Figure , JavascriptLink , CssLink
1819
19- class TimestampedGeoJson (Plugin ):
20- """Adds a TimestampedGeoJson layer on the map."""
20+ class TimestampedGeoJson (MacroElement ):
2121 def __init__ (self , data , transition_time = 200 , loop = True , auto_play = True ):
2222 """Creates a TimestampedGeoJson plugin to append into a map with
23- Map.add_plugin .
23+ Map.add_children .
2424
2525 Parameters
2626 ----------
@@ -69,8 +69,9 @@ def __init__(self, data, transition_time=200, loop=True, auto_play=True):
6969
7070 """
7171 super (TimestampedGeoJson , self ).__init__ ()
72- self .plugin_name = 'TimestampedGeoJson'
73- self .template = self .env .get_template ('timestamped_geo_json.tpl' )
72+ self ._name = 'TimestampedGeoJson'
73+
74+ #self.template = self.env.get_template('timestamped_geo_json.tpl')
7475 if 'read' in dir (data ):
7576 self .data = data .read ()
7677 elif type (data ) is dict :
@@ -81,13 +82,53 @@ def __init__(self, data, transition_time=200, loop=True, auto_play=True):
8182 self .loop = bool (loop )
8283 self .auto_play = bool (auto_play )
8384
84- def render_header (self , nb ):
85- """Generates the header part of the plugin."""
86- header = self .template .module .__dict__ .get ('header' ,None )
87- assert header is not None , "This template must have a 'header' macro."
88- return header (nb )
89- def render_js (self , nb ):
90- """Generates the Javascript part of the plugin."""
91- js = self .template .module .__dict__ .get ('js' ,None )
92- assert js is not None , "This template must have a 'js' macro."
93- return js (nb ,self )
85+ self ._template = Template ("""
86+ {% macro script(this, kwargs) %}
87+ {{this._parent.get_name()}}.timeDimension = L.timeDimension();
88+ {{this._parent.get_name()}}.timeDimensionControl = L.control.timeDimension({
89+ position: 'bottomleft',
90+ autoPlay: {{'true' if this.auto_play else 'false'}},
91+ playerOptions: {
92+ transitionTime: {{this.transition_time}},
93+ loop: {{'true' if this.loop else 'false'}}}
94+ });
95+ {{this._parent.get_name()}}.addControl({{this._parent.get_name()}}.timeDimensionControl);
96+
97+ var {{this.get_name()}} = L.timeDimension.layer.geoJson(
98+ L.geoJson({{this.data}}),
99+ {updateTimeDimension: true,addlastPoint: true}
100+ ).addTo({{this._parent.get_name()}});
101+ {% endmacro %}
102+ """ )
103+
104+ def render (self , ** kwargs ):
105+ super (TimestampedGeoJson , self ).render ()
106+
107+ figure = self .get_root ()
108+ assert isinstance (figure ,Figure ), ("You cannot render this Element "
109+ "if it's not in a Figure." )
110+
111+ figure .header .add_children (\
112+ JavascriptLink ("https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.0/jquery.min.js" ),
113+ name = 'jquery2.0.0' )
114+
115+ figure .header .add_children (\
116+ JavascriptLink ("https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js" ),
117+ name = 'jqueryui1.10.2' )
118+
119+ figure .header .add_children (\
120+ JavascriptLink ("https://raw.githubusercontent.com/nezasa/iso8601-js-period/master/iso8601.min.js" ),
121+ name = 'iso8601' )
122+
123+ figure .header .add_children (\
124+ JavascriptLink ("https://raw.githubusercontent.com/socib/Leaflet.TimeDimension/master/"
125+ "dist/leaflet.timedimension.min.js" ),
126+ name = 'leaflet.timedimension' )
127+
128+ figure .header .add_children (\
129+ CssLink ("https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/styles/default.min.css" ),
130+ name = 'highlight.js_css' )
131+
132+ figure .header .add_children (\
133+ CssLink ("http://apps.socib.es/Leaflet.TimeDimension/dist/leaflet.timedimension.control.min.css" ),
134+ name = 'leaflet.timedimension_css' )
0 commit comments