Py.Cafe

lopezv.oliver/

extended-layer-control

Interactive Hyperspectral & NDVI Viewer

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# check out https://solara.dev/ for documentation
# or https://github.com/widgetti/solara/
# And check out https://py.cafe/maartenbreddels for more examples
import solara
import ipyleaflet
from utils import handle_url_vis_change
from components import LayersControl
import json

LOCAL_TILE_SERVER_URL = "http://localhost:5000"  
XYZ_URL = LOCAL_TILE_SERVER_URL + "/{z}/{x}/{y}"
with open('colormaps.json') as f:
    available_colormaps = json.load(f)

colormaps = [key for key in available_colormaps]
colorbar_kwargs=dict(
    length=250,         # width in pixels
    orientation='horizontal',
    breadth=15,         # How "thick" is the colorbar itself 
    padding=10,
    axis_padding=6,
)
xmin, ymin, xmax, ymax  = [37.35351562499985,29.382175075145366,38.759765624999844,30.145127183376204]
bounds = [[ymin, xmin], [ymax, xmax]]
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(
        base_url = f'{XYZ_URL}?url=/data/hyperspectral1.tif',
        visible = solara.reactive(True),
        opacity = solara.reactive(1.0),
        url = solara.reactive(natural_url),
        bounds = bounds
    ),
    'NDVI': dict(
        base_url = f'{XYZ_URL}?url=/data/ndvi.tif',
        visible = solara.reactive(True),
        opacity = solara.reactive(1.0),
        url = solara.reactive(ndvi_url),
        render_type = 'singleband',
        singleband_options = dict(
            colormap = solara.reactive("ylgnbu"),
            vmin = solara.reactive(0.),
            vmax = solara.reactive(1.)
        ),
        bounds=bounds
    ),
    'False color': dict(
        base_url = f'{XYZ_URL}?url=/data/hyperspectral1.tif',
        visible = solara.reactive(True),
        opacity = solara.reactive(1.0),
        url = solara.reactive(false_url),
        render_type = 'multiband',
        multiband_options= {
            "red": dict(band=solara.reactive(27), vmin=solara.reactive(1460), vmax=solara.reactive(2898)),
            "green": dict(band=solara.reactive(20), vmin=solara.reactive(1427), vmax=solara.reactive(3510)),
            "blue": dict(band=solara.reactive(11), vmin=solara.reactive(1131), vmax=solara.reactive(2475))
        },
        dtype='uint16',
        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=list(range(1,32))
    )
}

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,
                bounds = layer['bounds']
            )
            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)
                        style={'min-height': '700px'}):  
        LayersControl(layer_defs,
                      colormaps = ['ndvi', 'ylgnbu', 'greens'],
                      available_colormaps=available_colormaps,
                      colorbar_kwargs=colorbar_kwargs
                      )

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

Page()