Py.Cafe

lopezv.oliver/

solara-interactive-map-visualizer

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
import solara
import ipyleaflet
from utils import update_url, get_layer_url_update_options
from components import LayersControl
import json

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,
)

layer_definitions = {
    'Natural': dict(
        base_url = "http://localhost:5000/{z}/{x}/{y}?url=/data/hyperspectral1.tif",
        visible = solara.reactive(True),
        opacity = solara.reactive(1.0),
        url = "", #  👈 Not reactive for this layer (empty for now..)
    ),
    'NDVI': dict(
        base_url = "http://localhost:5000/{z}/{x}/{y}?url=/data/ndvi.tif",
        visible = solara.reactive(True),
        opacity = solara.reactive(1.0),
        url = solara.reactive("http://localhost:5000/{z}/{x}/{y}?url=%2Fdata%2Fndvi.tif&colormap=ylgnbu&vmin=0.0&vmax=1.0"),
        # Optional:
        url_options = dict(
            colormap = solara.reactive("ylgnbu"), 
            vmin = solara.reactive(0.),
            vmax = solara.reactive(1.)
        )
    )
}

layer_definitions['Natural']['url'] = update_url(
    layer_definitions['Natural']['base_url'],
    dict(b=[16,10,7],vmin=[790,855,634], vmax=[4178,2912,2197])
)

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

@solara.component
def Page():
    layers = [
            ipyleaflet.TileLayer.element(
                url = layer['url'].value if hasattr(layer['url'], 'value') else layer['url'],
                #          ☝️ The Natural layer doesn't have a reactive url 
                opacity = layer['opacity'].value,
                visible = layer['visible'].value
            )
            for _, layer in layer_definitions.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_definitions,
                      colormaps = colormaps,
                      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()