Py.Cafe

lopezv.oliver/

solara-ipyleaflet-remove-layers-demo

Solara Interactive Map Visualizer

DocsPricing
  • app.py
  • components.py
  • requirements.txt
  • utils.py
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import solara
import ipyleaflet
from solara import reactive
from components import LayersControlPrototype

LOCAL_TILE_SERVER_URL = "http://localhost:5000/{path}"  
XYZ_URL = LOCAL_TILE_SERVER_URL.format(path="{z}/{x}/{y}")
ndvi_url = f"{XYZ_URL}?url=/data/ndvi.tif&colormap=ylgnbu&vmin=0&vmax=1"
false_url = f"{XYZ_URL}?url=/data/hyperspectral1.tif&b=27&b=20&b=11&vmin=1460&vmin=1427&vmin=1131&vmax=2898&vmax=3510&vmax=2475"
natural_url = f"{XYZ_URL}?url=/data/hyperspectral1.tif&b=16&b=10&b=7&vmin=790&vmin=855&vmin=634&vmax=4178&vmax=2912&vmax=2197"

layer_defs = {
    'Natural': dict(
        visible = reactive(True),
        opacity = reactive(1.0),
        url = natural_url,
    ),
    'False color': dict(
        visible = reactive(True),
        opacity = reactive(1.0),
        url = false_url,
        removable = True, # Allow this layer to be removed
    ),
    'NDVI': dict(
        visible = reactive(True),
        opacity = reactive(1.0),
        url = ndvi_url,
        removable = True,   # Allow this layer to be removed
    ),
}

layer_names = reactive([key for key in layer_defs])  
meta_layer_defs = reactive(layer_defs)
center = solara.reactive((30,38))
zoom = solara.reactive(12)

@solara.component
def Page():
    layers_to_add = {key:layer_defs[key] for key in layer_names.value}

    layers = [
        ipyleaflet.TileLayer.element(
            url = layer['url'],  # 👀 In this notebook we defined url directly, 
                                 # NOT as a reactive variable. 
            opacity = layer['opacity'].value,
            visible = layer['visible'].value,
        )
        for _,layer in layers_to_add.items()
    ]


    with solara.Columns([1,2]):
        LayersControlPrototype(
            layers_to_add,
            layer_names = layer_names,
            meta=meta_layer_defs
        )
    
        ipyleaflet.Map.element(
            scroll_wheel_zoom = True,
            zoom = zoom.value,
            on_zoom = zoom.set,
            center = center.value,
            on_center = center.set,
            layers = layers,
            controls = []
        )

Page()