Animate a line

Animate a line
This source code of this example is adapted from the MapLibre GL JS example - Animate a line.
Uncomment the following line to install leafmap if needed.
| # %pip install "leafmap[maplibre]"
|
| import time
import pandas as pd
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.
| # import os
# os.environ["MAPTILER_KEY"] = "YOUR_API_KEY"
|
| url = "https://github.com/opengeos/datasets/releases/download/world/animated_line_data.csv"
df = pd.read_csv(url)
df_sample = df.sample(n=1000, random_state=1).sort_index()
df_sample.loc[len(df_sample)] = df.iloc[-1]
df_sample.head()
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 | m = leafmap.Map(center=[0, 0], zoom=0.5, style="streets")
geojson = {
"type": "FeatureCollection",
"features": [
{"type": "Feature", "geometry": {"type": "LineString", "coordinates": [[0, 0]]}}
],
}
source = {"type": "geojson", "data": geojson}
m.add_source("line", source)
layer = {
"id": "line-animation",
"type": "line",
"source": "line",
"layout": {"line-cap": "round", "line-join": "round"},
"paint": {"line-color": "#ed6498", "line-width": 5, "line-opacity": 0.8},
}
m.add_layer(layer)
m
|
| run_times = 2
for i in range(run_times):
geojson["features"][0]["geometry"]["coordinates"] = [[0, 0]]
for row in df_sample.itertuples():
time.sleep(0.005)
geojson["features"][0]["geometry"]["coordinates"].append([row.x, row.y])
m.set_data("line", geojson)
|
