Skip to content

Commit ba45248

Browse files
authored
Merge pull request #627 from soymsk/add-style-for-timestamped-geojson
Add style for timestamped geojson
2 parents 5d1c722 + 42e133d commit ba45248

File tree

4 files changed

+116
-7
lines changed

4 files changed

+116
-7
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
0.3.0
22
~~~~~
33

4+
- Added style option to 'Timestamped geojson' plugin (soymsk #627)
45
- Switched to `leaflet 1.0.1` (juoceano #531 and ocefpaf #535)
56
- Added `continuous_world`, `world_copy_jump`, and `no_wrap` options (ocefpaf #508)
67
- Update font-awesome to 4.6.3 (ocefpaf #478)

examples/Plugins.ipynb

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,104 @@
277277
"\n",
278278
"m # Click on the top right button."
279279
]
280+
},
281+
{
282+
"cell_type": "markdown",
283+
"metadata": {},
284+
"source": [
285+
"## Timestamped GeoJSON"
286+
]
287+
},
288+
{
289+
"cell_type": "code",
290+
"execution_count": null,
291+
"metadata": {
292+
"collapsed": false
293+
},
294+
"outputs": [],
295+
"source": [
296+
"from folium import plugins\n",
297+
"\n",
298+
"m = folium.Map(location=[35.68159659061569, 139.76451516151428], zoom_start=16)\n",
299+
"\n",
300+
"lines = [\n",
301+
" {\n",
302+
" \"coordinates\": [\n",
303+
" [139.76451516151428, 35.68159659061569], # Lon, Lat\n",
304+
" [139.75964426994324, 35.682590062684206],\n",
305+
" ],\n",
306+
" \"dates\": [\n",
307+
" \"2017-06-02T00:00:00\",\n",
308+
" \"2017-06-02T00:10:00\"\n",
309+
" ],\n",
310+
" \"color\": \"red\"\n",
311+
" },\n",
312+
" {\n",
313+
" \"coordinates\": [\n",
314+
" [139.75964426994324, 35.682590062684206],\n",
315+
" [139.7575843334198, 35.679505030038506],\n",
316+
" ],\n",
317+
" \"dates\": [\n",
318+
" \"2017-06-02T00:10:00\",\n",
319+
" \"2017-06-02T00:20:00\"\n",
320+
" ],\n",
321+
" \"color\": \"blue\"\n",
322+
" },\n",
323+
" {\n",
324+
" \"coordinates\": [\n",
325+
" [139.7575843334198, 35.679505030038506],\n",
326+
" [139.76337790489197, 35.678040905014065],\n",
327+
" ],\n",
328+
" \"dates\": [\n",
329+
" \"2017-06-02T00:20:00\",\n",
330+
" \"2017-06-02T00:30:00\"\n",
331+
" ],\n",
332+
" \"color\": \"green\",\n",
333+
" \"weight\": 15,\n",
334+
" },\n",
335+
" {\n",
336+
" \"coordinates\": [\n",
337+
" [139.76337790489197, 35.678040905014065],\n",
338+
" [139.76451516151428, 35.68159659061569],\n",
339+
" ],\n",
340+
" \"dates\": [\n",
341+
" \"2017-06-02T00:30:00\",\n",
342+
" \"2017-06-02T00:40:00\"\n",
343+
" ],\n",
344+
" \"color\": \"#FFFFFF\",\n",
345+
" },\n",
346+
" \n",
347+
"]\n",
348+
"\n",
349+
"\n",
350+
"features = [\n",
351+
" {\n",
352+
" \"type\": \"Feature\",\n",
353+
" \"geometry\": {\n",
354+
" \"type\": \"LineString\",\n",
355+
" \"coordinates\": line[\"coordinates\"],\n",
356+
" },\n",
357+
" \"properties\": {\n",
358+
" \"times\": line[\"dates\"],\n",
359+
" \"style\": {\n",
360+
" \"color\": line[\"color\"],\n",
361+
" \"weight\": line[\"weight\"] if \"weight\" in line else 5\n",
362+
" }\n",
363+
" }\n",
364+
" }\n",
365+
"for line in lines]\n",
366+
"\n",
367+
"\n",
368+
"plugins.TimestampedGeoJson({\n",
369+
" \"type\": \"FeatureCollection\",\n",
370+
" \"features\": features,\n",
371+
"}, period=\"PT1M\", add_last_point=True).add_to(m)\n",
372+
"\n",
373+
"\n",
374+
"m.save(os.path.join('results', 'Plugins_5.html'))\n",
375+
"\n",
376+
"m"
377+
]
280378
}
281379
],
282380
"metadata": {

folium/plugins/timestamped_geo_json.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828

2929
class TimestampedGeoJson(MacroElement):
30-
def __init__(self, data, transition_time=200, loop=True, auto_play=True,
30+
def __init__(self, data, transition_time=200, loop=True, auto_play=True, add_last_point=True,
3131
period="P1D"):
3232
"""Creates a TimestampedGeoJson plugin to append into a map with
3333
Map.add_child.
@@ -82,6 +82,8 @@ def __init__(self, data, transition_time=200, loop=True, auto_play=True,
8282
Whether the animation shall loop.
8383
auto_play : bool, default True
8484
Whether the animation shall start automatically at startup.
85+
add_last_point: bool, default True
86+
Whether a point is added at the last valid coordinate of a LineString.
8587
period : str, default "P1D"
8688
Used to construct the array of available times starting
8789
from the first available time. Format: ISO8601 Duration
@@ -106,6 +108,7 @@ def __init__(self, data, transition_time=200, loop=True, auto_play=True,
106108
self.transition_time = int(transition_time)
107109
self.loop = bool(loop)
108110
self.auto_play = bool(auto_play)
111+
self.add_last_point = bool(add_last_point)
109112
self.period = period
110113

111114
self._template = Template("""
@@ -121,8 +124,10 @@ def __init__(self, data, transition_time=200, loop=True, auto_play=True,
121124
{{this._parent.get_name()}}.addControl({{this._parent.get_name()}}.timeDimensionControl);
122125
123126
var {{this.get_name()}} = L.timeDimension.layer.geoJson(
124-
L.geoJson({{this.data}}),
125-
{updateTimeDimension: true,addlastPoint: true}
127+
L.geoJson({{this.data}}, {'style': function (feature) {
128+
return feature.properties.style
129+
}}),
130+
{updateTimeDimension: true,addlastPoint: {{'true' if this.add_last_point else 'false'}}}
126131
).addTo({{this._parent.get_name()}});
127132
{% endmacro %}
128133
""") # noqa

tests/plugins/test_timestamped_geo_json.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,11 @@ def test_timestamped_geo_json():
5050
},
5151
"properties": {
5252
"times": [1435708800000+i*86400000 for
53-
i in np.linspace(0, 25, 25)]
54-
}
53+
i in np.linspace(0, 25, 25)],
54+
"style": {
55+
"color": "red"
56+
},
57+
},
5558
},
5659
{
5760
"type": "Feature",
@@ -123,8 +126,10 @@ def test_timestamped_geo_json():
123126
{{this._parent.get_name()}}.addControl({{this._parent.get_name()}}.timeDimensionControl);
124127
125128
var {{this.get_name()}} = L.timeDimension.layer.geoJson(
126-
L.geoJson({{this.data}}),
127-
{updateTimeDimension: true,addlastPoint: true}
129+
L.geoJson({{this.data}}, {'style': function (feature) {
130+
return feature.properties.style
131+
}}),
132+
{updateTimeDimension: true,addlastPoint: {{'true' if this.add_last_point else 'false'}}}
128133
).addTo({{this._parent.get_name()}});
129134
""") # noqa
130135

0 commit comments

Comments
 (0)