giswqs commited on
Commit
9f738da
1 Parent(s): 8349bfb

Added country analysis

Browse files
Files changed (1) hide show
  1. pages/01_gswis.py +49 -6
pages/01_gswis.py CHANGED
@@ -42,6 +42,8 @@ def add_analysis_gui(m=None, position='topright', opened=True):
42
  """
43
 
44
  fc = ee.FeatureCollection('users/giswqs/public/countries')
 
 
45
  gswe = ee.ImageCollection("users/h2i_lab/gswe/gswe_datasets")
46
  image = gswe.mosaic()
47
  # esa = gswe.select("esa").mosaic()
@@ -76,17 +78,22 @@ def add_analysis_gui(m=None, position='topright', opened=True):
76
  style={"description_width": "initial"},
77
  )
78
 
 
 
 
 
 
 
79
  buttons = widgets.ToggleButtons(
80
  value=None,
81
  options=["Apply", "Reset", "Close"],
82
  tooltips=["Apply", "Reset", "Close"],
83
  button_style="primary",
 
84
  )
85
  buttons.style.button_width = "88px"
86
  label = widgets.Label("Draw an area on the map first.")
87
 
88
- output = widgets.Output(layout=widgets.Layout(width=widget_width, padding=padding))
89
-
90
  toolbar_widget = widgets.VBox()
91
  toolbar_widget.children = [toolbar_button]
92
  toolbar_header = widgets.HBox()
@@ -95,9 +102,30 @@ def add_analysis_gui(m=None, position='topright', opened=True):
95
  toolbar_footer.children = [
96
  radio,
97
  buttons,
98
- output,
99
  ]
100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  def toolbar_btn_click(change):
102
  if change["new"]:
103
  close_button.value = False
@@ -121,10 +149,16 @@ def add_analysis_gui(m=None, position='topright', opened=True):
121
 
122
  def button_clicked(change):
123
  if change["new"] == "Apply":
 
 
 
 
 
 
 
124
  with output:
125
  output.clear_output()
126
- # com_label = widgets.Label("PLEASE WAIT ...")
127
- # display(com_label)
128
  buttons.value = None
129
 
130
  if radio.value == "Draw an area":
@@ -134,9 +168,18 @@ def add_analysis_gui(m=None, position='topright', opened=True):
134
  else:
135
  chart = zonal_stats_chart(image, m.user_roi, scale=100)
136
  display(chart)
 
 
 
 
 
 
137
 
138
  elif change["new"] == "Reset":
139
- output.clear_output()
 
 
 
140
  elif change["new"] == "Close":
141
  if m is not None:
142
  if m.tool_control is not None and m.tool_control in m.controls:
 
42
  """
43
 
44
  fc = ee.FeatureCollection('users/giswqs/public/countries')
45
+ countries = fc.aggregate_array('NAME').getInfo()
46
+ countries.sort()
47
  gswe = ee.ImageCollection("users/h2i_lab/gswe/gswe_datasets")
48
  image = gswe.mosaic()
49
  # esa = gswe.select("esa").mosaic()
 
78
  style={"description_width": "initial"},
79
  )
80
 
81
+ country = widgets.Dropdown(
82
+ options=countries,
83
+ value=None,
84
+ layout=widgets.Layout(width=widget_width, padding=padding),
85
+ )
86
+
87
  buttons = widgets.ToggleButtons(
88
  value=None,
89
  options=["Apply", "Reset", "Close"],
90
  tooltips=["Apply", "Reset", "Close"],
91
  button_style="primary",
92
+ layout=widgets.Layout(padding="0px 2px 4px 2px"),
93
  )
94
  buttons.style.button_width = "88px"
95
  label = widgets.Label("Draw an area on the map first.")
96
 
 
 
97
  toolbar_widget = widgets.VBox()
98
  toolbar_widget.children = [toolbar_button]
99
  toolbar_header = widgets.HBox()
 
102
  toolbar_footer.children = [
103
  radio,
104
  buttons,
 
105
  ]
106
 
107
+ def change_radio(change):
108
+ if change["new"] == "Select a country":
109
+ toolbar_footer.children = [radio, country, buttons]
110
+ else:
111
+ toolbar_footer.children = [radio, buttons]
112
+
113
+ radio.observe(change_radio, "value")
114
+
115
+ m.selected_country = None
116
+
117
+ def change_country(change):
118
+ if change["new"]:
119
+ country_name = country.value
120
+ country_fc = fc.filter(ee.Filter.eq('NAME', country_name))
121
+ vec_style = {'color': '000000ff', 'width': 3, 'fillColor': '00000000'}
122
+ m.addLayer(country_fc.style(**vec_style), {}, 'Selected Country')
123
+ m.centerObject(country_fc)
124
+ m.selected_country = country_fc
125
+ toolbar_footer.children = [radio, country, buttons]
126
+
127
+ country.observe(change_country, "value")
128
+
129
  def toolbar_btn_click(change):
130
  if change["new"]:
131
  close_button.value = False
 
149
 
150
  def button_clicked(change):
151
  if change["new"] == "Apply":
152
+ output = widgets.Output(
153
+ layout=widgets.Layout(width=widget_width, padding=padding)
154
+ )
155
+ if radio.value == "Select a country":
156
+ toolbar_footer.children = [radio, country, buttons, output]
157
+ else:
158
+ toolbar_footer.children = [radio, buttons, output]
159
  with output:
160
  output.clear_output()
161
+
 
162
  buttons.value = None
163
 
164
  if radio.value == "Draw an area":
 
168
  else:
169
  chart = zonal_stats_chart(image, m.user_roi, scale=100)
170
  display(chart)
171
+ elif radio.value == "Select a country":
172
+ if m.selected_country is not None:
173
+ chart = zonal_stats_chart(
174
+ image, m.selected_country.geometry(), scale=100
175
+ )
176
+ display(chart)
177
 
178
  elif change["new"] == "Reset":
179
+ country.value = None
180
+ radio.value = "Draw an area"
181
+ toolbar_footer.children = [radio, buttons]
182
+
183
  elif change["new"] == "Close":
184
  if m is not None:
185
  if m.tool_control is not None and m.tool_control in m.controls: