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.
In [1]:
Copied!
# %pip install "leafmap[maplibre]"
# %pip install "leafmap[maplibre]"
In [2]:
Copied!
import time
import pandas as pd
import leafmap.maplibregl as leafmap
import time
import pandas as pd
import leafmap.maplibregl as leafmap
In [3]:
Copied!
# import os
# os.environ["MAPTILER_KEY"] = "YOUR_API_KEY"
# import os
# os.environ["MAPTILER_KEY"] = "YOUR_API_KEY"
In [4]:
Copied!
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()
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()
Out[4]:
x | y | |
---|---|---|
38 | 1.266667 | 3.532592 |
44 | 1.466667 | 4.088553 |
45 | 1.500000 | 4.181139 |
58 | 1.933333 | 5.382508 |
82 | 2.733333 | 7.586668 |
In [5]:
Copied!
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
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
Failed to retrieve the MapTiler style. Defaulting to OpenFreeMap 'liberty' style.
In [6]:
Copied!
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)
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)