Chenxi Zeng
Chenxi Zeng

Reputation: 487

How to display a time series of folium maps?

I want to plot folium maps in display by time (like show a video of all maps).

I know how to plot one map, and successfully did so. However, if I do multiple plot, only the first one is shown. I want to know if there is a viable way to do the task I want? Perhaps display one, and then delete one (how?) and display the next?

for path in paths:
    polyline.show_polyline(path, point_list)
    display(polyline.m)
    time.sleep(3)

but note this script only show the first map, the latter ones will not be shown.

Expect a plot of all maps (display a map for 3 seconds and then do the next).

Upvotes: 1

Views: 4691

Answers (1)

Alexandre B.
Alexandre B.

Reputation: 5502

Folium can't dynamically update data. I suggest you to have a look at this discussion.

However, folium is delivered with the plugin TimestampedGeoJson designed for this task. Here is an example:

import folium
from folium import plugins
m = folium.Map(
    location=[35.68159659061569, 139.76451516151428],
    zoom_start=16
)

# Lon, Lat order.
lines = [
    {
        'coordinates': [
            [139.76451516151428, 35.68159659061569],
            [139.75964426994324, 35.682590062684206],
        ],
        'dates': [
            '2017-06-02T00:00:00',
            '2017-06-02T00:10:00'
        ],
        'color': 'red'
    },
    {
        'coordinates': [
            [139.75964426994324, 35.682590062684206],
            [139.7575843334198, 35.679505030038506],
        ],
        'dates': [
            '2017-06-02T00:10:00',
            '2017-06-02T00:20:00'
        ],
        'color': 'blue'
    },
    {
        'coordinates': [
            [139.7575843334198, 35.679505030038506],
            [139.76337790489197, 35.678040905014065],
        ],
        'dates': [
            '2017-06-02T00:20:00',
            '2017-06-02T00:30:00'
        ],
        'color': 'green',
        'weight': 15,
    },
    {
        'coordinates': [
            [139.76337790489197, 35.678040905014065],
            [139.76451516151428, 35.68159659061569],
        ],
        'dates': [
            '2017-06-02T00:30:00',
            '2017-06-02T00:40:00'
        ],
        'color': '#FFFFFF',
    },
]

features = [
    {
        'type': 'Feature',
        'geometry': {
            'type': 'LineString',
            'coordinates': line['coordinates'],
        },
        'properties': {
            'times': line['dates'],
            'style': {
                'color': line['color'],
                'weight': line['weight'] if 'weight' in line else 5
            }
        }
    }
    for line in lines
]

plugins.TimestampedGeoJson({
    'type': 'FeatureCollection',
    'features': features,
}, period='PT1M', add_last_point=True).add_to(m)

display(m)

Source

I tried it and it's working under Python 3.7.

Upvotes: 4

Related Questions