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.
In [ ]:
Copied!
# %pip install "leafmap[maplibre]"
# %pip install "leafmap[maplibre]"
In [ ]:
Copied!
import time
import pandas as pd
import leafmap.maplibregl as leafmap
import time
import pandas as pd
import leafmap.maplibregl as leafmap
In [ ]:
Copied!
# import os
# os.environ["MAPTILER_KEY"] = "YOUR_API_KEY"
# import os
# os.environ["MAPTILER_KEY"] = "YOUR_API_KEY"
In [ ]:
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()
In [ ]:
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
In [ ]:
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)