Skip to content

Commit 007fa8c

Browse files
hansthenpre-commit-ci[bot]Conengmo
authored
Add Timeline plugin (#1870)
* Add interval support to timestamped_geo_json * Try with separate timeline * Fix error in documentation * Fixed breaking test * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Based on review comments * Add classes to namespace per review comments * Added documentation for timeline plugin Updated the documentation for timestamped_geojson to clarify the difference with timeline. Also used a simpler code example in timeline.py docstring. * Add test + fix CDN import * Piggyback on borders.json from the Skeat timeline repo * Update docs/user_guide/plugins/timestamped_geojson.md Co-authored-by: Frank Anema <33519926+Conengmo@users.noreply.github.com> * Updated after review comments * Made the example file smaller * Update folium/plugins/timeline.py Co-authored-by: Frank Anema <33519926+Conengmo@users.noreply.github.com> --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Frank Anema <33519926+Conengmo@users.noreply.github.com>
1 parent 5c2f3d1 commit 007fa8c

7 files changed

Lines changed: 469 additions & 0 deletions

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
```{code-cell} ipython3
2+
---
3+
nbsphinx: hidden
4+
---
5+
import folium
6+
import folium.plugins
7+
```
8+
9+
## Timeline and TimelineSlider
10+
Show changing geospatial data over time.
11+
12+
### Comparison to TimestampedGeoJson
13+
This is a plugin with a similar purpose to `TimestampedGeoJson`. They both
14+
show geospatial information that changes over time.
15+
16+
The main difference between the two is the input format.
17+
18+
In the `Timeline` plugin each `Feature` has its own `start` and `end` time among its properties.
19+
In the `TimestampedGeojson` each `Feature` has an array of start times. Each start time in
20+
the array corresponds to a part of the `Geometry` of that `Feature`.
21+
22+
`TimestampedGeojson` also does not have `end` times for each `Feature`. Instead you can
23+
specify a global `duration` property that is valid for all features.
24+
25+
Depending on your input geojson, one plugin may be more convenient than the other.
26+
27+
### Comparison to Realtime
28+
The `Timeline` plugin can only show data from the past. If you want live updates,
29+
you need the `Realtime` plugin.
30+
31+
```{code-cell} ipython3
32+
import folium
33+
from folium.utilities import JsCode
34+
from folium.features import GeoJsonPopup
35+
from folium.plugins.timeline import Timeline, TimelineSlider
36+
import requests
37+
38+
m = folium.Map()
39+
40+
data = requests.get(
41+
"https://raw.githubusercontent.com/python-visualization/folium-example-data/main/historical_country_borders.json"
42+
).json()
43+
44+
timeline = Timeline(
45+
data,
46+
style=JsCode("""
47+
function (data) {
48+
function getColorFor(str) {
49+
var hash = 0;
50+
for (var i = 0; i < str.length; i++) {
51+
hash = str.charCodeAt(i) + ((hash << 5) - hash);
52+
}
53+
var red = (hash >> 24) & 0xff;
54+
var grn = (hash >> 16) & 0xff;
55+
var blu = (hash >> 8) & 0xff;
56+
return "rgb(" + red + "," + grn + "," + blu + ")";
57+
}
58+
return {
59+
stroke: false,
60+
color: getColorFor(data.properties.name),
61+
fillOpacity: 0.5,
62+
};
63+
}
64+
""")
65+
).add_to(m)
66+
GeoJsonPopup(fields=['name'], labels=True).add_to(timeline)
67+
TimelineSlider(
68+
auto_play=False,
69+
show_ticks=True,
70+
enable_keyboard_controls=True,
71+
playback_duration=30000,
72+
).add_timelines(timeline).add_to(m)
73+
m
74+
```

docs/user_guide/plugins/timestamped_geojson.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,27 @@ import folium.plugins
77
```
88

99
## TimestampedGeoJson
10+
Show changing geospatial data over time.
11+
12+
### Relation to Timeline
13+
This is a plugin with a similar purpose to `Timeline`. They both
14+
show geospatial information that changes over time.
15+
16+
The main difference between the two is the input format.
17+
18+
In the `TimestampedGeojson` each `Feature` has an array of start times. Each start time in
19+
the array corresponds to a part of the `Geometry` of that `Feature`.
20+
In the `Timeline` plugin each `Feature` has its own `start` and `end` time among its properties.
21+
22+
Also `TimestampedGeoJson` has a global `duration` property that is valid for all `Features`.
23+
In the `Timeline` plugin each `Feature` has its own `end` time property.
24+
25+
Depending on your input geojson, one plugin may be more convenient than the other.
26+
27+
### Relation to Realtime
28+
This plugin can only show data from the past. If you want live updates,
29+
you need the `Realtime` plugin.
30+
1031

1132
### Example 1
1233

examples/data/historical_country_borders.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

folium/plugins/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from folium.plugins.tag_filter_button import TagFilterButton
3030
from folium.plugins.terminator import Terminator
3131
from folium.plugins.time_slider_choropleth import TimeSliderChoropleth
32+
from folium.plugins.timeline import Timeline, TimelineSlider
3233
from folium.plugins.timestamped_geo_json import TimestampedGeoJson
3334
from folium.plugins.timestamped_wmstilelayer import TimestampedWmsTileLayers
3435
from folium.plugins.treelayercontrol import TreeLayerControl
@@ -65,6 +66,8 @@
6566
"TagFilterButton",
6667
"Terminator",
6768
"TimeSliderChoropleth",
69+
"Timeline",
70+
"TimelineSlider",
6871
"TimestampedGeoJson",
6972
"TimestampedWmsTileLayers",
7073
"TreeLayerControl",

0 commit comments

Comments
 (0)