OpenLayers Heatmap¶
This notebook demonstrates the native OpenLayers heatmap layer for visualizing point density.
In [ ]:
Copied!
# %pip install anymap-ts
# %pip install anymap-ts
In [ ]:
Copied!
import json
import random
from anymap_ts import OpenLayersMap
import json
import random
from anymap_ts import OpenLayersMap
Generate Sample Point Data¶
In [ ]:
Copied!
random.seed(42)
features = []
centers = [
(-122.4, 37.8),
(-118.2, 34.05),
(-73.98, 40.75),
(-87.63, 41.88),
(-95.37, 29.76),
]
for cx, cy in centers:
count = random.randint(80, 200)
for _ in range(count):
lng = cx + random.gauss(0, 0.3)
lat = cy + random.gauss(0, 0.3)
magnitude = random.uniform(1, 10)
features.append(
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [lng, lat]},
"properties": {"magnitude": magnitude},
}
)
point_data = {"type": "FeatureCollection", "features": features}
print(f"Generated {len(features)} points")
random.seed(42)
features = []
centers = [
(-122.4, 37.8),
(-118.2, 34.05),
(-73.98, 40.75),
(-87.63, 41.88),
(-95.37, 29.76),
]
for cx, cy in centers:
count = random.randint(80, 200)
for _ in range(count):
lng = cx + random.gauss(0, 0.3)
lat = cy + random.gauss(0, 0.3)
magnitude = random.uniform(1, 10)
features.append(
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [lng, lat]},
"properties": {"magnitude": magnitude},
}
)
point_data = {"type": "FeatureCollection", "features": features}
print(f"Generated {len(features)} points")
Basic Heatmap¶
In [ ]:
Copied!
m = OpenLayersMap(center=[-98, 38], zoom=4, height="600px")
m.add_basemap("CartoDB.DarkMatter")
m.add_heatmap(
point_data,
name="heatmap",
blur=20,
radius=12,
opacity=0.8,
)
m
m = OpenLayersMap(center=[-98, 38], zoom=4, height="600px")
m.add_basemap("CartoDB.DarkMatter")
m.add_heatmap(
point_data,
name="heatmap",
blur=20,
radius=12,
opacity=0.8,
)
m
Weighted Heatmap¶
Use a property as weight to control heatmap intensity.
In [ ]:
Copied!
m = OpenLayersMap(center=[-98, 38], zoom=4, height="600px")
m.add_basemap("CartoDB.DarkMatter")
m.add_heatmap(
point_data,
name="weighted-heatmap",
weight="magnitude",
blur=25,
radius=15,
opacity=0.9,
)
m
m = OpenLayersMap(center=[-98, 38], zoom=4, height="600px")
m.add_basemap("CartoDB.DarkMatter")
m.add_heatmap(
point_data,
name="weighted-heatmap",
weight="magnitude",
blur=25,
radius=15,
opacity=0.9,
)
m
Custom Gradient¶
In [ ]:
Copied!
m = OpenLayersMap(center=[-98, 38], zoom=4, height="600px")
m.add_basemap("CartoDB.DarkMatter")
m.add_heatmap(
point_data,
name="custom-gradient",
blur=20,
radius=12,
gradient=["#00f", "#0ff", "#0f0", "#ff0", "#f00"],
)
m
m = OpenLayersMap(center=[-98, 38], zoom=4, height="600px")
m.add_basemap("CartoDB.DarkMatter")
m.add_heatmap(
point_data,
name="custom-gradient",
blur=20,
radius=12,
gradient=["#00f", "#0ff", "#0f0", "#ff0", "#f00"],
)
m