Skip to content

Animate images

image image image

Animate a series of images

This source code of this example is adapted from the MapLibre GL JS example - Animate a series of images.

Uncomment the following line to install leafmap if needed.

1
# %pip install "leafmap[maplibre]"
1
2
import time
import leafmap.maplibregl as leafmap

To run this notebook, you will need an API key from MapTiler. Once you have the API key, you can uncomment the following code block and replace YOUR_API_KEY with your actual API key. Then, run the code block code to set the API key as an environment variable.

1
2
# import os
# os.environ["MAPTILER_KEY"] = "YOUR_API_KEY"
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
m = leafmap.Map(
    center=[-75.789, 41.874], zoom=5, min_zoom=4, max_zoom=6, style="streets"
)


def get_path(index):
    return f"https://maplibre.org/maplibre-gl-js/docs/assets/radar{index}.gif"


source = {
    "type": "image",
    "url": get_path(0),
    "coordinates": [
        [-80.425, 46.437],
        [-71.516, 46.437],
        [-71.516, 37.936],
        [-80.425, 37.936],
    ],
}
m.add_source("radar", source)

layer = {
    "id": "radar-layer",
    "type": "raster",
    "source": "radar",
    "paint": {"raster-fade-duration": 0},
}
m.add_layer(layer)
m

The following step does not work properly yet. Will revisit this when it becomes possible.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
for i in range(5):
    time.sleep(1)
    source = {
        "type": "image",
        "url": get_path(i),
        "coordinates": [
            [-80.425, 46.437],
            [-71.516, 46.437],
            [-71.516, 37.936],
            [-80.425, 37.936],
        ],
    }
    m.set_data("radar", source)