MapLibre Heatmap Example¶
This notebook demonstrates how to create heatmap visualizations using MapLibre's native heatmap layer.
In [ ]:
Copied!
# %pip install anymap-ts
# %pip install anymap-ts
Basic Heatmap¶
In [ ]:
Copied!
import random
from anymap_ts import Map
# Generate random earthquake-like data
features = []
for _ in range(500):
features.append(
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-122.4 + random.uniform(-1, 1),
37.8 + random.uniform(-1, 1),
],
},
"properties": {"magnitude": random.uniform(1, 6)},
}
)
geojson = {"type": "FeatureCollection", "features": features}
m = Map(center=[-122.4, 37.8], zoom=9)
m.add_basemap("CartoDB.DarkMatter")
m.add_heatmap(geojson, radius=25)
m
import random
from anymap_ts import Map
# Generate random earthquake-like data
features = []
for _ in range(500):
features.append(
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-122.4 + random.uniform(-1, 1),
37.8 + random.uniform(-1, 1),
],
},
"properties": {"magnitude": random.uniform(1, 6)},
}
)
geojson = {"type": "FeatureCollection", "features": features}
m = Map(center=[-122.4, 37.8], zoom=9)
m.add_basemap("CartoDB.DarkMatter")
m.add_heatmap(geojson, radius=25)
m
Weighted Heatmap¶
In [ ]:
Copied!
m2 = Map(center=[-122.4, 37.8], zoom=9)
m2.add_basemap("CartoDB.DarkMatter")
m2.add_heatmap(geojson, weight_property="magnitude", radius=30, intensity=0.8)
m2
m2 = Map(center=[-122.4, 37.8], zoom=9)
m2.add_basemap("CartoDB.DarkMatter")
m2.add_heatmap(geojson, weight_property="magnitude", radius=30, intensity=0.8)
m2
Custom Colormap¶
In [ ]:
Copied!
m3 = Map(center=[-122.4, 37.8], zoom=9)
m3.add_basemap("CartoDB.DarkMatter")
# Custom colormap from blue to red
custom_colormap = [
[0, "rgba(0,0,255,0)"],
[0.2, "blue"],
[0.4, "cyan"],
[0.6, "lime"],
[0.8, "yellow"],
[1, "red"],
]
m3.add_heatmap(
geojson, weight_property="magnitude", radius=35, colormap=custom_colormap
)
m3
m3 = Map(center=[-122.4, 37.8], zoom=9)
m3.add_basemap("CartoDB.DarkMatter")
# Custom colormap from blue to red
custom_colormap = [
[0, "rgba(0,0,255,0)"],
[0.2, "blue"],
[0.4, "cyan"],
[0.6, "lime"],
[0.8, "yellow"],
[1, "red"],
]
m3.add_heatmap(
geojson, weight_property="magnitude", radius=35, colormap=custom_colormap
)
m3
Heatmap from URL¶
In [ ]:
Copied!
m4 = Map(center=[-120, 37], zoom=5)
m4.add_basemap("CartoDB.DarkMatter")
# Load earthquake data from USGS
earthquake_url = (
"https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.geojson"
)
m4.add_heatmap(earthquake_url, weight_property="mag", radius=20, intensity=0.5)
m4
m4 = Map(center=[-120, 37], zoom=5)
m4.add_basemap("CartoDB.DarkMatter")
# Load earthquake data from USGS
earthquake_url = (
"https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.geojson"
)
m4.add_heatmap(earthquake_url, weight_property="mag", radius=20, intensity=0.5)
m4
Export to HTML¶
In [ ]:
Copied!
m4.to_html("maplibre_heatmap_example.html")
m4.to_html("maplibre_heatmap_example.html")