Py.Cafe

lopezv.oliver/

solara-interactive-layers-control

Solara Interactive Map Visualizer

DocsPricing
  • app.py
  • colormaps.json
  • 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import solara
import ipyleaflet
from components import LayersControl
import json

with open('colormaps.json') as f:
    available_colormaps = json.load(f)

colormaps = [key for key in available_colormaps]
bounds = [
  [
    29.382175075145366,
    37.35351562499985
  ],
  [
    30.145127183376204,
    38.759765624999844
  ]
]


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

layer_defs = {
    'Natural': dict(
        visible = solara.reactive(True),
        opacity = solara.reactive(1.0),
        url = solara.reactive(natural_url),
        bounds = bounds,
    ),
    'NDVI': dict(
        visible = solara.reactive(True),
        opacity = solara.reactive(1.0),
        url = solara.reactive(ndvi_url),
        bounds = bounds,
        colormap = solara.reactive("ylgnbu"),
        vmin = solara.reactive(0.),
        vmax = solara.reactive(1.),
    ),
    'False color': dict(
        visible = solara.reactive(True),
        opacity = solara.reactive(1.0),
        url = solara.reactive(false_url),
        bounds = bounds,
        lookup_index_by_band = {f"b{n}":n for n in range(1,32)},
        lookup_band_by_index = {n:f"b{n}" for n in range(1,32)},
        bands = {
            "r": dict(band=solara.reactive('b27'), vmin=solara.reactive(1460), vmax=solara.reactive(2898)),
            "g": dict(band=solara.reactive('b20'), vmin=solara.reactive(1427), vmax=solara.reactive(3510)),
            "b": dict(band=solara.reactive('b11'), vmin=solara.reactive(1131), vmax=solara.reactive(2475))
        }
    )
}

center = solara.reactive((30,38))
zoom = solara.reactive(12)

@solara.component
def Page():
    layers = [
            ipyleaflet.TileLayer.element(
                url = layer['url'].value,
                opacity = layer['opacity'].value,
                visible = layer['visible'].value
            )
            for _, layer in layer_defs.items()
    ]

    with solara.Columns([1,2]):   # Give 1/3 of the width to the first column (Control), 2/3 to the second (Map)
        LayersControl(layer_defs, 
            colormaps=colormaps,
            available_colormaps=available_colormaps
        )

        ipyleaflet.Map.element(
            scroll_wheel_zoom = True,
            center = center.value,
            on_center = center.set,
            zoom = zoom.value,
            on_zoom = zoom.set,
            layers = layers, 
            controls = []
        )

Page()