Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Timeline plugin #1870

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions docs/user_guide/plugins/timeline.md
Copy link
Collaborator Author

@hansthen hansthen Mar 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reference to the Skeate repo in line 42 is temporary, to make the tests pass. Once the PR is accepted I will change this to point to data inside our own repository.

Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
```{code-cell} ipython3
---
nbsphinx: hidden
---
import folium
import folium.plugins
```

## Timeline and TimelineSlider
Show changing geospatial data over time.

### Comparison to TimestampedGeoJson
This is a plugin with a similar purpose to `TimestampedGeoJson`. They both
show geospatial information that changes over time.

The main difference between the two is the input format.

In the `Timeline` plugin each `Feature` has its own `start` and `end` time among its properties.
In the `TimestampedGeojson` each `Feature` has an array of start times. Each start time in
the array corresponds to a part of the `Geometry` of that `Feature`.

`TimestampedGeojson` also does not have `end` times for each `Feature`. Instead you can
specify a global `duration` property that is valid for all features.

Depending on your input geojson, one plugin may be more convenient than the other.

### Comparison to Realtime
The `Timeline` plugin can only show data from the past. If you want live updates,
you need the `Realtime` plugin.

```{code-cell} ipython3
import folium
from folium.utilities import JsCode
from folium.features import GeoJsonPopup
from folium.plugins.timeline import Timeline, TimelineSlider
import requests

m = folium.Map()

data = requests.get(
"https://raw.githubusercontent.com/python-visualization/folium-example-data/main/historical_country_borders.json"
).json()

timeline = Timeline(
data,
style=JsCode("""
function (data) {
function getColorFor(str) {
var hash = 0;
for (var i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash);
}
var red = (hash >> 24) & 0xff;
var grn = (hash >> 16) & 0xff;
var blu = (hash >> 8) & 0xff;
return "rgb(" + red + "," + grn + "," + blu + ")";
}
return {
stroke: false,
color: getColorFor(data.properties.name),
fillOpacity: 0.5,
};
}
""")
).add_to(m)
GeoJsonPopup(fields=['name'], labels=True).add_to(timeline)
TimelineSlider(
auto_play=False,
show_ticks=True,
enable_keyboard_controls=True,
playback_duration=30000,
).add_timelines(timeline).add_to(m)
m
```
21 changes: 21 additions & 0 deletions docs/user_guide/plugins/timestamped_geojson.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,27 @@ import folium.plugins
```

## TimestampedGeoJson
Show changing geospatial data over time.

### Relation to Timeline
This is a plugin with a similar purpose to `Timeline`. They both
show geospatial information that changes over time.

The main difference between the two is the input format.

In the `TimestampedGeojson` each `Feature` has an array of start times. Each start time in
the array corresponds to a part of the `Geometry` of that `Feature`.
In the `Timeline` plugin each `Feature` has its own `start` and `end` time among its properties.

Also `TimestampedGeoJson` has a global `duration` property that is valid for all `Features`.
In the `Timeline` plugin each `Feature` has its own `end` time property.

Depending on your input geojson, one plugin may be more convenient than the other.

### Relation to Realtime
This plugin can only show data from the past. If you want live updates,
you need the `Realtime` plugin.


### Example 1

Expand Down
1 change: 1 addition & 0 deletions examples/data/historical_country_borders.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions folium/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from folium.plugins.tag_filter_button import TagFilterButton
from folium.plugins.terminator import Terminator
from folium.plugins.time_slider_choropleth import TimeSliderChoropleth
from folium.plugins.timeline import Timeline, TimelineSlider
from folium.plugins.timestamped_geo_json import TimestampedGeoJson
from folium.plugins.timestamped_wmstilelayer import TimestampedWmsTileLayers
from folium.plugins.vectorgrid_protobuf import VectorGridProtobuf
Expand Down Expand Up @@ -64,6 +65,8 @@
"TagFilterButton",
"Terminator",
"TimeSliderChoropleth",
"Timeline",
"TimelineSlider",
"TimestampedGeoJson",
"TimestampedWmsTileLayers",
"VectorGridProtobuf",
Expand Down
Loading