Layer Management Example¶
This notebook demonstrates the layer management utilities including setting paint/layout properties, moving layers, and getting layer information.
In [ ]:
Copied!
# %pip install anymap-ts
# %pip install anymap-ts
Setup: Create Layers¶
In [ ]:
Copied!
from anymap_ts import Map
# Create sample data
points = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-122.4, 37.8]},
"properties": {"name": "A"},
},
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-122.3, 37.7]},
"properties": {"name": "B"},
},
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-122.5, 37.9]},
"properties": {"name": "C"},
},
],
}
polygons = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[-122.5, 37.7],
[-122.3, 37.7],
[-122.3, 37.9],
[-122.5, 37.9],
[-122.5, 37.7],
]
],
},
"properties": {"name": "Zone 1"},
}
],
}
m = Map(center=[-122.4, 37.8], zoom=11)
m.add_vector(
polygons, name="zones", paint={"fill-color": "#3388ff", "fill-opacity": 0.3}
)
m.add_vector(
points, name="points", paint={"circle-color": "#ff0000", "circle-radius": 8}
)
m
from anymap_ts import Map
# Create sample data
points = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-122.4, 37.8]},
"properties": {"name": "A"},
},
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-122.3, 37.7]},
"properties": {"name": "B"},
},
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-122.5, 37.9]},
"properties": {"name": "C"},
},
],
}
polygons = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[-122.5, 37.7],
[-122.3, 37.7],
[-122.3, 37.9],
[-122.5, 37.9],
[-122.5, 37.7],
]
],
},
"properties": {"name": "Zone 1"},
}
],
}
m = Map(center=[-122.4, 37.8], zoom=11)
m.add_vector(
polygons, name="zones", paint={"fill-color": "#3388ff", "fill-opacity": 0.3}
)
m.add_vector(
points, name="points", paint={"circle-color": "#ff0000", "circle-radius": 8}
)
m
Get Layer IDs¶
In [ ]:
Copied!
# List all layer IDs
layer_ids = m.get_layer_ids()
print("Layer IDs:", layer_ids)
# List all layer IDs
layer_ids = m.get_layer_ids()
print("Layer IDs:", layer_ids)
Get Layer Configuration¶
In [ ]:
Copied!
# Get configuration for a specific layer
layer_config = m.get_layer("points")
print("Points layer config:", layer_config)
# Get configuration for a specific layer
layer_config = m.get_layer("points")
print("Points layer config:", layer_config)
Set Paint Properties¶
In [ ]:
Copied!
# Change the circle color dynamically
m.set_paint_property("points", "circle-color", "#00ff00")
m.set_paint_property("points", "circle-radius", 12)
# Change the circle color dynamically
m.set_paint_property("points", "circle-color", "#00ff00")
m.set_paint_property("points", "circle-radius", 12)
Set Layout Properties¶
In [ ]:
Copied!
# Toggle layer visibility
m.set_layout_property("zones", "visibility", "none")
# Toggle layer visibility
m.set_layout_property("zones", "visibility", "none")
In [ ]:
Copied!
# Show the layer again
m.set_layout_property("zones", "visibility", "visible")
# Show the layer again
m.set_layout_property("zones", "visibility", "visible")
Move Layer Order¶
In [ ]:
Copied!
# Move points layer below zones layer
m.move_layer("points", "zones")
# Move points layer below zones layer
m.move_layer("points", "zones")
In [ ]:
Copied!
# Move points back to top
m.move_layer("points")
# Move points back to top
m.move_layer("points")
Set Visibility and Opacity¶
In [ ]:
Copied!
# Set layer opacity
m.set_opacity("zones", 0.7)
# Set layer opacity
m.set_opacity("zones", 0.7)
In [ ]:
Copied!
# Toggle visibility
m.set_visibility("points", False)
# Toggle visibility
m.set_visibility("points", False)
In [ ]:
Copied!
# Show again
m.set_visibility("points", True)
# Show again
m.set_visibility("points", True)
Export to HTML¶
In [ ]:
Copied!
m.to_html("layer_management_example.html")
m.to_html("layer_management_example.html")