Mapbox - Vector Data & Heatmap¶
GeoJSON, heatmaps, source updates, and tile layers.
In [ ]:
Copied!
# %pip install anymap-ts
# %pip install anymap-ts
Add GeoJSON (Points, Lines, Polygons)¶
In [ ]:
Copied!
from anymap_ts import MapboxMap
m = MapboxMap(center=[-122.4, 37.8], zoom=10)
geojson = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-122.4194, 37.7749]},
"properties": {"name": "Point A"},
},
{
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [[-122.4, 37.8], [-122.3, 37.85]],
},
"properties": {"name": "Line 1"},
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[-122.45, 37.75],
[-122.35, 37.75],
[-122.35, 37.85],
[-122.45, 37.85],
[-122.45, 37.75],
]
],
},
"properties": {"name": "Polygon 1"},
},
],
}
m.add_geojson(geojson, name="vector-layer")
m
from anymap_ts import MapboxMap
m = MapboxMap(center=[-122.4, 37.8], zoom=10)
geojson = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-122.4194, 37.7749]},
"properties": {"name": "Point A"},
},
{
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [[-122.4, 37.8], [-122.3, 37.85]],
},
"properties": {"name": "Line 1"},
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[-122.45, 37.75],
[-122.35, 37.75],
[-122.35, 37.85],
[-122.45, 37.85],
[-122.45, 37.75],
]
],
},
"properties": {"name": "Polygon 1"},
},
],
}
m.add_geojson(geojson, name="vector-layer")
m
Add Heatmap from Point Data¶
In [ ]:
Copied!
heatmap_geojson = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-122.4 + i * 0.02, 37.8]},
"properties": {"intensity": 1},
}
for i in range(25)
],
}
m.add_heatmap(heatmap_geojson, name="heatmap", radius=25, intensity=1.5)
m
heatmap_geojson = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-122.4 + i * 0.02, 37.8]},
"properties": {"intensity": 1},
}
for i in range(25)
],
}
m.add_heatmap(heatmap_geojson, name="heatmap", radius=25, intensity=1.5)
m
Update GeoJSON Source¶
In [ ]:
Copied!
updated_geojson = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-122.38, 37.82]},
"properties": {"name": "Updated Point"},
},
],
}
m.update_geojson_source("vector-layer-source", updated_geojson)
updated_geojson = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-122.38, 37.82]},
"properties": {"name": "Updated Point"},
},
],
}
m.update_geojson_source("vector-layer-source", updated_geojson)
Add Tile Layer¶
In [ ]:
Copied!
tile_url = "https://tile.openstreetmap.org/{z}/{x}/{y}.png"
m.add_tile_layer(tile_url, name="osm-tiles", attribution="© OpenStreetMap")
m
tile_url = "https://tile.openstreetmap.org/{z}/{x}/{y}.png"
m.add_tile_layer(tile_url, name="osm-tiles", attribution="© OpenStreetMap")
m