File size: 4,798 Bytes
7b68ac7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import ee
import geemap
import ipywidgets as widgets
from IPython.display import display
import solara


class Map(geemap.Map):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.add_basemap("Esri.WorldImagery")
        self.add_ee_data()
        self.add_buttons(add_header=True)

    def add_ee_data(self):

        dataset = ee.Image("JRC/GSW1_4/GlobalSurfaceWater")
        image = dataset.select(["occurrence"])
        vis_params = {
            "min": 0.0,
            "max": 100.0,
            "palette": ["ffffff", "ffbbbb", "0000ff"],
        }
        self.addLayer(image, vis_params, "Occurrence")
        self.add_colorbar(
            vis_params, label="Water occurrence (%)", layer_name="Occurrence"
        )

    def add_buttons(self, position="topright", **kwargs):
        padding = "0px 5px 0px 5px"
        widget = widgets.VBox(layout=widgets.Layout(padding=padding))
        layout = widgets.Layout(width="auto")
        style = {"description_width": "initial"}
        hist_btn = widgets.Button(description="Occurrence", layout=layout)
        bar_btn = widgets.Button(description="Monthly history", layout=layout)
        reset_btn = widgets.Button(description="Reset", layout=layout)
        scale = widgets.IntSlider(
            min=30, max=1000, value=90, description="Scale", layout=layout, style=style
        )
        month_slider = widgets.IntRangeSlider(
            description="Months",
            value=[5, 10],
            min=1,
            max=12,
            step=1,
            layout=layout,
            style=style,
        )
        widget.children = [
            widgets.HBox([hist_btn, bar_btn, reset_btn]),
            month_slider,
            scale,
        ]
        self.add_widget(widget, position=position, **kwargs)
        output = widgets.Output()
        self.add_widget(output, position="bottomleft", add_header=False)

        def hist_btn_click(b):
            region = self.user_roi
            if region is not None:
                output.clear_output()
                output.append_stdout("Computing histogram...")
                image = ee.Image("JRC/GSW1_4/GlobalSurfaceWater").select(["occurrence"])
                self.default_style = {"cursor": "wait"}
                hist = geemap.image_histogram(
                    image,
                    region,
                    scale=scale.value,
                    height=350,
                    width=550,
                    x_label="Water Occurrence (%)",
                    y_label="Pixel Count",
                    layout_args={
                        "title": dict(x=0.5),
                        "margin": dict(l=0, r=0, t=10, b=0),
                    },
                    return_df=False,
                )

                with output:
                    output.clear_output()
                    display(hist)
                self.default_style = {"cursor": "default"}
            else:
                output.clear_output()
                with output:
                    output.append_stdout("Please draw a region of interest first.")

        hist_btn.on_click(hist_btn_click)

        def bar_btn_click(b):
            region = self.user_roi
            if region is not None:
                self.default_style = {"cursor": "wait"}
                output.clear_output()
                output.append_stdout("Computing monthly history...")
                bar = geemap.jrc_hist_monthly_history(
                    region=region,
                    scale=scale.value,
                    height=350,
                    width=550,
                    layout_args={
                        "title": dict(x=0.5),
                        "margin": dict(l=0, r=0, t=10, b=0),
                    },
                    frequency="month",
                    start_month=month_slider.value[0],
                    end_month=month_slider.value[1],
                    denominator=1e4,
                    y_label="Area (ha)",
                )

                with output:
                    output.clear_output()
                    display(bar)
                self.default_style = {"cursor": "default"}
            else:
                output.clear_output()
                with output:
                    output.append_stdout("Please draw a region of interest first.")

        bar_btn.on_click(bar_btn_click)

        def reset_btn_click(b):
            self._draw_control.clear()
            output.clear_output()

        reset_btn.on_click(reset_btn_click)


@solara.component
def Page():
    with solara.Column(style={"min-width": "500px"}):
        Map.element(
            center=[20, -0],
            zoom=2,
            height="750px",
            zoom_ctrl=False,
            measure_ctrl=False,
        )