-
Notifications
You must be signed in to change notification settings - Fork 6
/
app_interface
424 lines (335 loc) · 14 KB
/
app_interface
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// released under Open Source GPL License Copyright © 2023 Ka Hei Chow
// ############################################# //
// ### World Bank Intra-Urban Analysis Tool #### //
// ############################################# //
// Updated: 03.03.2023
// Author: Ka Hei Chow
// This is a reuseable function designed for intra-urban analysis
// The function aoi_analysis_app will take a function (one argument which is the aoi vector) and embed it into the predefined aoi
// If this funciton return undefined, nothing will be add to the result panel.
// Anything else return will be added to the result panel so make sure the type is valid for this purpose.
//
// To use the interface, add the following line in the script:
// var intraUrban = require('users/pinkychow1010/GEE:WorldBank/app_interface');
//
// An example usage will be:
// intraUrban.aoi_analysis_app(action_call)
//
// with action_call function defined below:
//
// function action_call(aoi) {
// Map.addLayer(aoi,{},"AOI")
// return undefined;
// }
var style = require('users/pinkychow1010/WB_IntraUrban:explorer_style'); // dict for explorer styling
var basemap = require('users/pinkychow1010/WB_IntraUrban:basemap_resources'); // customized basemap to enhance visuals
exports.app = function aoi_analysis_app(action_call, title_name) {
// define app name
if (title_name === undefined || title_name === null){var title_name = "World Bank Intra-Urban Analysis Tool"}
// Import geometry boundary data for user selection
var ADM0 = ee.FeatureCollection("projects/sat-io/open-datasets/geoboundaries/CGAZ_ADM0");
var ADM1 = ee.FeatureCollection("projects/sat-io/open-datasets/geoboundaries/CGAZ_ADM1");
var ADM2 = ee.FeatureCollection("projects/sat-io/open-datasets/geoboundaries/CGAZ_ADM2");
// ################################# //
// ########### GUI APP ############# //
// ################################# //
reset();
var mainPanel = ui.Panel();
mainPanel.style().set(style.mainPanel); // styling
ui.root.insert(0,mainPanel);
var title = ui.Label(title_name);
title.style().set(style.title);
mainPanel.add(title);
// Create a panel to hold the drop-down boxes
var dropdownPanel = ui.Panel();
dropdownPanel.style().set(style.dark_theme);
// Create a panel to hold the result
var resultPanel = ui.Panel();
resultPanel.style().set(style.dark_theme);
// Define 3 dropdowns for admin0, admin1 and admin2 names
// Keep them disbled. We will add items later
var admin0Select = ui.Select({
placeholder: 'Please wait..',
}).setDisabled(true)
var admin1Select = ui.Select({
placeholder: 'Select a country first',
}).setDisabled(true)
var admin2Select = ui.Select({
placeholder: 'Select admin 1 unit first',
}).setDisabled(true);
dropdownPanel.add(admin0Select);
dropdownPanel.add(admin1Select);
dropdownPanel.add(admin2Select);
mainPanel.add(dropdownPanel);
mainPanel.add(resultPanel);
// Define the onChange() function for admin0Select
var admin0Selected = function(admin0Selection) {
resultPanel.clear();
Map.clear(); reset();
admin1Select.items().reset();
admin2Select.items().reset();
admin1Select.setPlaceholder('Please wait..');
admin2Select.setPlaceholder('Select a admin 1 unit first..');
// Now we have admin0 values, fetch admin1 values for that country
var selectedAdmin0 = ADM0.filter(ee.Filter.eq('shapeName', admin0Selection));
var shapeID = ee.Feature(selectedAdmin0.first()).get('shapeID');
var selectedAdmin1 = ADM1.filter(ee.Filter.eq('ADM0_shape', shapeID));
var admin1Names = selectedAdmin1
.aggregate_array('shapeName')
.sort()
// Use evaluate() to not block the UI
admin1Names.evaluate(function(items){
admin1Select.setPlaceholder('Select admin1 unit')
admin1Select.items().reset(items)
// Now that we have items, enable the menu
admin1Select.setDisabled(false)
})
}
// Define the onChange() function for admin1Select
var admin1Selected = function(admin1Selection) {
resultPanel.clear();
Map.clear(); reset();
admin2Select.setPlaceholder('Please wait..');
var selectedAdmin1 = ADM1.filter(ee.Filter.eq('shapeName', admin1Selection));
var shapeID = ee.Feature(selectedAdmin1.first()).get('shapeID');
var selectedAdmin2 = ADM2.filter(ee.Filter.eq('ADM1_shape', shapeID));
var admin2Names = selectedAdmin2
.aggregate_array('shapeName')
.sort()
admin2Names.evaluate(function(items){
if (items.length == 0) {
items = ['Entire Admin1 Unit']
admin2Select.items().reset(items)
}
else {
admin2Select.items().reset(items)
}
admin2Select.setPlaceholder('Select admin2 unit')
admin2Select.setDisabled(false)
})
}
// Define the onChange() function for admin2Select
var admin2Selected = function(admin1Selection) {
resultPanel.clear();
Map.clear(); reset();
var admin0Value = admin0Select.getValue();
var admin1Value = admin1Select.getValue();
var admin2Value = admin2Select.getValue();
var selectedAdmin1 = ADM1.filter(ee.Filter.eq('shapeName', admin1Value));
var shapeGroup = ee.Feature(selectedAdmin1.first()).get('shapeGroup');
// Some regions do not have admin2 values
// We are on client-side, so using if/else is ok
if (admin2Value == 'Entire Admin1 Unit') {
var result = admin1Value + ',' + admin0Value
var filtered = ADM1
.filter(ee.Filter.eq('shapeName', admin1Value))
.filter(ee.Filter.eq('shapeGroup', shapeGroup))
}
else {
var result = admin2Value + ',' + admin1Value + ',' + admin0Value
var filtered = ADM2
.filter(ee.Filter.eq('shapeName', admin2Value))
.filter(ee.Filter.eq('shapeGroup', shapeGroup))
.filter(ee.Filter.intersects('.geo', selectedAdmin1.geometry()));
}
// Action
Map.centerObject(filtered);
draw_bounds(filtered);
var output = action_call(filtered);
// if (typeof(output) !== 'undefined') {
// resultPanel.add(output);
// };
var n = 0;
if (typeof(output) !== 'undefined') {
if (output.length > 1) {
while (n < output.length) {
resultPanel.add(output[n]);
n++;
}
} else {
resultPanel.add(output);
}
}
};
// Register the callback functions
admin0Select.onChange(admin0Selected);
admin1Select.onChange(admin1Selected);
admin2Select.onChange(admin2Selected);
// Get all country names and sort them
var admin0Names = ADM0.aggregate_array('shapeName').sort().distinct()
// Fetch the value using evaluate() to not block the UI
admin0Names.evaluate(function(items){
admin0Select.items().reset(items);
// Now that we have items, enable the menu
admin0Select.setDisabled(false);
// Change placeholder
admin0Select.setPlaceholder('Select a country for analysis')
});
};
// draw admin boundary selected by user
function draw_bounds(geometry){
var empty = ee.Image().byte();
var outlines = empty.paint({
featureCollection: geometry,
color: 'red',
width: 2
});
var palette = ['FF0000', '00FF00', '0000FF'];
Map.addLayer(outlines, {palette: palette}, 'Boundary');
}
// config base map on reset
function reset(){
basemap.addCustomBasemap('MutedMonotone');
Map.setCenter(0, 20, 1.8);
Map.setControlVisibility({drawingToolsControl:false});
}
// exports.app = function aoi_analysis_app(action_call, title_name) {
// // define app name
// if (title_name === undefined || title_name === null){var title_name = "World Bank Intra-Urban Analysis Tool"}
// // Import geometry boundary data for user selection
// var ADM0 = ee.FeatureCollection("projects/sat-io/open-datasets/geoboundaries/CGAZ_ADM0");
// var ADM1 = ee.FeatureCollection("projects/sat-io/open-datasets/geoboundaries/CGAZ_ADM1");
// var ADM2 = ee.FeatureCollection("projects/sat-io/open-datasets/geoboundaries/CGAZ_ADM2");
// // ################################# //
// // ########### GUI APP ############# //
// // ################################# //
// var mainPanel = ui.Panel();
// mainPanel.style().set({
// width: '240px',
// backgroundColor: 'white'
// });
// ui.root.insert(0,mainPanel);
// var title = ui.Label(title_name);
// title.style().set({
// 'position': 'top-center',
// 'fontSize': '24px',
// 'color': '#002244',
// 'fontWeight': 'bold',
// 'fontFamily': 'monospace'
// });
// mainPanel.add(title);
// // Create a panel to hold the drop-down boxes
// var dropdownPanel = ui.Panel();
// // Create a panel to hold the result
// var resultPanel = ui.Panel();
// // Define 3 dropdowns for admin0, admin1 and admin2 names
// // Keep them disbled. We will add items later
// var admin0Select = ui.Select({
// placeholder: 'please wait..',
// }).setDisabled(true)
// var admin1Select = ui.Select({
// placeholder: 'select a country first',
// }).setDisabled(true)
// var admin2Select = ui.Select({
// placeholder: 'select admin 1 unit first',
// }).setDisabled(true)
// dropdownPanel.add(admin0Select)
// dropdownPanel.add(admin1Select)
// dropdownPanel.add(admin2Select)
// mainPanel.add(dropdownPanel)
// mainPanel.add(resultPanel)
// // Define the onChange() function for admin0Select
// var admin0Selected = function(admin0Selection) {
// resultPanel.clear()
// Map.clear()
// admin1Select.items().reset()
// admin2Select.items().reset()
// admin1Select.setPlaceholder('please wait..')
// admin2Select.setPlaceholder('Select a admin 1 unit first..')
// // Now we have admin0 values, fetch admin1 values for that country
// var selectedAdmin0 = ADM0.filter(ee.Filter.eq('shapeName', admin0Selection));
// var shapeID = ee.Feature(selectedAdmin0.first()).get('shapeID');
// var selectedAdmin1 = ADM1.filter(ee.Filter.eq('ADM0_shape', shapeID));
// var admin1Names = selectedAdmin1
// .aggregate_array('shapeName')
// .sort()
// // Use evaluate() to not block the UI
// admin1Names.evaluate(function(items){
// admin1Select.setPlaceholder('select admin1 unit')
// admin1Select.items().reset(items)
// // Now that we have items, enable the menu
// admin1Select.setDisabled(false)
// })
// }
// // Define the onChange() function for admin1Select
// var admin1Selected = function(admin1Selection) {
// resultPanel.clear()
// Map.clear()
// admin2Select.setPlaceholder('please wait..')
// var selectedAdmin1 = ADM1.filter(ee.Filter.eq('shapeName', admin1Selection));
// var shapeID = ee.Feature(selectedAdmin1.first()).get('shapeID');
// var selectedAdmin2 = ADM2.filter(ee.Filter.eq('ADM1_shape', shapeID));
// var admin2Names = selectedAdmin2
// .aggregate_array('shapeName')
// .sort()
// admin2Names.evaluate(function(items){
// if (items.length == 0) {
// items = ['No Admin2 Units']
// admin2Select.items().reset(items)
// }
// else {
// admin2Select.items().reset(items)
// }
// admin2Select.setPlaceholder('select admin2 unit')
// admin2Select.setDisabled(false)
// })
// }
// // Define the onChange() function for admin2Select
// var admin2Selected = function(admin1Selection) {
// resultPanel.clear()
// Map.clear()
// var admin0Value = admin0Select.getValue()
// var admin1Value = admin1Select.getValue()
// var admin2Value = admin2Select.getValue()
// var selectedAdmin1 = ADM1.filter(ee.Filter.eq('shapeName', admin1Value));
// var shapeGroup = ee.Feature(selectedAdmin1.first()).get('shapeGroup');
// // Some regions do not have admin2 values
// // We are on client-side, so using if/else is ok
// if (admin2Value == 'No Admin2 Units') {
// var result = admin1Value + ',' + admin0Value
// var filtered = ADM1
// .filter(ee.Filter.eq('shapeName', admin1Value))
// .filter(ee.Filter.eq('shapeGroup', shapeGroup))
// }
// else {
// var result = admin2Value + ',' + admin1Value + ',' + admin0Value
// var filtered = ADM2
// .filter(ee.Filter.eq('shapeName', admin2Value))
// .filter(ee.Filter.eq('shapeGroup', shapeGroup))
// .filter(ee.Filter.intersects('.geo', selectedAdmin1.geometry()));
// }
// var label = ui.Label('You selected: ' + result)
// resultPanel.add
// // Action
// Map.centerObject(filtered)
// var output = action_call(filtered);
// // if (typeof(output) !== 'undefined') {
// // resultPanel.add(output);
// // };
// var n = 0;
// if (typeof(output) !== 'undefined') {
// if (output.length > 1) {
// while (n < output.length) {
// resultPanel.add(output[n]);
// n++;
// }
// } else {
// resultPanel.add(output);
// }
// }
// };
// // Register the callback functions
// admin0Select.onChange(admin0Selected)
// admin1Select.onChange(admin1Selected)
// admin2Select.onChange(admin2Selected)
// // Get all country names and sort them
// var admin0Names = ADM0.aggregate_array('shapeName').sort().distinct()
// // Fetch the value using evaluate() to not block the UI
// admin0Names.evaluate(function(items){
// admin0Select.items().reset(items)
// // Now that we have items, enable the menu
// admin0Select.setDisabled(false)
// // Change placeholder
// admin0Select.setPlaceholder('Select a country for analysis')
// })
// }