File size: 7,364 Bytes
7b68ac7
 
2ea9c28
 
 
 
 
 
 
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/opengeos/solara-geemap/blob/main/notebooks/07_jrc.ipynb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# %pip install -U geemap solara"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "import geemap\n",
    "import ipywidgets as widgets\n",
    "from IPython.display import display\n",
    "import solara\n",
    "\n",
    "\n",
    "class Map(geemap.Map):\n",
    "    def __init__(self, **kwargs):\n",
    "        super().__init__(**kwargs)\n",
    "        self.add_basemap(\"Esri.WorldImagery\")\n",
    "        self.add_ee_data()\n",
    "        self.add_buttons(add_header=True)\n",
    "\n",
    "    def add_ee_data(self):\n",
    "\n",
    "        dataset = ee.Image(\"JRC/GSW1_4/GlobalSurfaceWater\")\n",
    "        image = dataset.select([\"occurrence\"])\n",
    "        vis_params = {\n",
    "            \"min\": 0.0,\n",
    "            \"max\": 100.0,\n",
    "            \"palette\": [\"ffffff\", \"ffbbbb\", \"0000ff\"],\n",
    "        }\n",
    "        self.addLayer(image, vis_params, \"Occurrence\")\n",
    "        self.add_colorbar(\n",
    "            vis_params, label=\"Water occurrence (%)\", layer_name=\"Occurrence\"\n",
    "        )\n",
    "\n",
    "    def add_buttons(self, position=\"topright\", **kwargs):\n",
    "        padding = \"0px 5px 0px 5px\"\n",
    "        widget = widgets.VBox(layout=widgets.Layout(padding=padding))\n",
    "        layout = widgets.Layout(width=\"auto\")\n",
    "        style = {\"description_width\": \"initial\"}\n",
    "        hist_btn = widgets.Button(description=\"Occurrence\", layout=layout)\n",
    "        bar_btn = widgets.Button(description=\"Monthly history\", layout=layout)\n",
    "        reset_btn = widgets.Button(description=\"Reset\", layout=layout)\n",
    "        scale = widgets.IntSlider(\n",
    "            min=30, max=1000, value=90, description=\"Scale\", layout=layout, style=style\n",
    "        )\n",
    "        month_slider = widgets.IntRangeSlider(\n",
    "            description=\"Months\",\n",
    "            value=[5, 10],\n",
    "            min=1,\n",
    "            max=12,\n",
    "            step=1,\n",
    "            layout=layout,\n",
    "            style=style,\n",
    "        )\n",
    "        widget.children = [\n",
    "            widgets.HBox([hist_btn, bar_btn, reset_btn]),\n",
    "            month_slider,\n",
    "            scale,\n",
    "        ]\n",
    "        self.add_widget(widget, position=position, **kwargs)\n",
    "        output = widgets.Output()\n",
    "        self.add_widget(output, position=\"bottomleft\", add_header=False)\n",
    "\n",
    "        def hist_btn_click(b):\n",
    "            region = self.user_roi\n",
    "            if region is not None:\n",
    "                output.clear_output()\n",
    "                output.append_stdout(\"Computing histogram...\")\n",
    "                image = ee.Image(\"JRC/GSW1_4/GlobalSurfaceWater\").select([\"occurrence\"])\n",
    "                self.default_style = {\"cursor\": \"wait\"}\n",
    "                hist = geemap.image_histogram(\n",
    "                    image,\n",
    "                    region,\n",
    "                    scale=scale.value,\n",
    "                    height=350,\n",
    "                    width=550,\n",
    "                    x_label=\"Water Occurrence (%)\",\n",
    "                    y_label=\"Pixel Count\",\n",
    "                    layout_args={\n",
    "                        \"title\": dict(x=0.5),\n",
    "                        \"margin\": dict(l=0, r=0, t=10, b=0),\n",
    "                    },\n",
    "                    return_df=False,\n",
    "                )\n",
    "\n",
    "                with output:\n",
    "                    output.clear_output()\n",
    "                    display(hist)\n",
    "                self.default_style = {\"cursor\": \"default\"}\n",
    "            else:\n",
    "                output.clear_output()\n",
    "                with output:\n",
    "                    output.append_stdout(\"Please draw a region of interest first.\")\n",
    "\n",
    "        hist_btn.on_click(hist_btn_click)\n",
    "\n",
    "        def bar_btn_click(b):\n",
    "            region = self.user_roi\n",
    "            if region is not None:\n",
    "                self.default_style = {\"cursor\": \"wait\"}\n",
    "                output.clear_output()\n",
    "                output.append_stdout(\"Computing monthly history...\")\n",
    "                bar = geemap.jrc_hist_monthly_history(\n",
    "                    region=region,\n",
    "                    scale=scale.value,\n",
    "                    height=350,\n",
    "                    width=550,\n",
    "                    layout_args={\n",
    "                        \"title\": dict(x=0.5),\n",
    "                        \"margin\": dict(l=0, r=0, t=10, b=0),\n",
    "                    },\n",
    "                    frequency=\"month\",\n",
    "                    start_month=month_slider.value[0],\n",
    "                    end_month=month_slider.value[1],\n",
    "                    denominator=1e4,\n",
    "                    y_label=\"Area (ha)\",\n",
    "                )\n",
    "\n",
    "                with output:\n",
    "                    output.clear_output()\n",
    "                    display(bar)\n",
    "                self.default_style = {\"cursor\": \"default\"}\n",
    "            else:\n",
    "                output.clear_output()\n",
    "                with output:\n",
    "                    output.append_stdout(\"Please draw a region of interest first.\")\n",
    "\n",
    "        bar_btn.on_click(bar_btn_click)\n",
    "\n",
    "        def reset_btn_click(b):\n",
    "            self._draw_control.clear()\n",
    "            output.clear_output()\n",
    "\n",
    "        reset_btn.on_click(reset_btn_click)\n",
    "\n",
    "\n",
    "@solara.component\n",
    "def Page():\n",
    "    with solara.Column(style={\"min-width\": \"500px\"}):\n",
    "        Map.element(\n",
    "            center=[20, -0],\n",
    "            zoom=2,\n",
    "            height=\"750px\",\n",
    "            zoom_ctrl=False,\n",
    "            measure_ctrl=False,\n",
    "        )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Page()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "geo",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}