-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmap_comparison_interface
283 lines (217 loc) · 10.1 KB
/
map_comparison_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
// released under Open Source GPL License Copyright © 2023 Ka Hei Chow
// ####################################### //
// ###### Map Comparison App ###### //
// ####################################### //
/**
* This is a script for comparing layers between national admin regions.
*
*
*/
// import modules
var palettes = require('users/gena/packages:palettes');
var analysis = require('users/pinkychow1010/WB_IntraUrban:analysis_utils');
var helper = require('users/pinkychow1010/WB_IntraUrban:helper');
var style = require('users/pinkychow1010/WB_IntraUrban:map_compare_style'); // dict for app styling
var basemap = require('users/pinkychow1010/WB_IntraUrban:basemap_resources');
/**
* Comparison Map: App which composes of a main panel with several widgets
* User can control the panel to visualize choropleth map for two different climate variables
*
* This function is used to construct the variable comparison app
*
* @author Ka Hei Chow.
*
* @fires add main panel with variable and location selections, add welcome instruction message
*
* @param {dict} layer_dict A dict with data layer as keys and get dataset functions as values.
* @param {dict} info_dict A dict with data layer as keys and data layer descriptions as values.
* @param {dict} url_dict A dict with data layer as keys and project url as values.
* @param {dict} log_dict A dict with data layer as keys and log visualization options as values.
* @param {dict} legend_dict A dict with data layer as keys and legend titles as values.
*
* @return {null} null.
*
*/
exports.comparison_app = function(layer_dict, info_dict, url_dict, log_dict, legend_dict, color_dict) {
// import project data layers for admin level selections
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");
// customized basemap to enhance visuals
var new_basemap = basemap.getCustomBasemap('MutedMonotone');
// set all map controls
var mapControl = style.mapControl;
// Create two maps with custom options
// left map
var map1 = ui.Map();
map1.style().set('cursor', 'crosshair'); // set cursor shape
map1.setControlVisibility(mapControl); // hide unnecessary map controls to give space
// right map
var map2 = ui.Map();
map2.style().set('cursor', 'crosshair'); // cursor shape
map2.setControlVisibility(mapControl); // set map control
map2.setControlVisibility({zoomControl: false}) ;
// customized basemap for both maps to enhance visuals
map1.setOptions(undefined, {'Custom': new_basemap});
map2.setOptions(undefined, {'Custom': new_basemap});
// link control between two maps
var linker = ui.Map.Linker([map1, map2]);
// create a grid of maps
var mapGrid = ui.Panel(
[ui.Panel([map1], null, {stretch: 'both'}),
ui.Panel([map2], null, {stretch: 'both'})],
ui.Panel.Layout.Flow('horizontal'), {stretch: 'both'}
);
// main panel on the side for argument selections
var mainPanel = ui.Panel();
mainPanel.style().set(style.mainPanel); // define style
// Add the maps and title to the ui.root.
ui.root.widgets().reset([mainPanel, mapGrid]);
ui.root.setLayout(ui.Panel.Layout.Flow('horizontal'));
// set up title
var title = ui.Label("Map Comparison 🗺️");
title.style().set(style.title);
mainPanel.add(title); // add title to main panel
var dropdownPanel = ui.Panel(); // panel to hold the drop-down boxes
dropdownPanel.style().set(style.dark_theme);
// set up location input session (dropdowns for admin0, admin1 and admin2 names)
// disable admin selections
var admin0Select = ui.Select({
placeholder: 'Please wait..',
}).setDisabled(true);
dropdownPanel.add(admin0Select); // add country drop down
// admin level for user selection for choropleth map display
var admin_dict = {
"Admin 1 Level": ADM1,
"Admin 2 Level": ADM2
};
var adminLevelSelect = ui.Select({
placeholder: 'Select admin level ...',
items: Object.keys(admin_dict),
});
dropdownPanel.add(adminLevelSelect); // add country drop down
// select variable 1 (left map)
var var1_selection = ui.Select({
placeholder: 'Select data layer 1...',
items: Object.keys(layer_dict),
});
var var2_selection = ui.Select({
placeholder: 'Select data layer 2...',
items: Object.keys(layer_dict),
});
dropdownPanel.add(var1_selection);
dropdownPanel.add(var2_selection);
// add drop down to side bar
mainPanel.add(dropdownPanel); // display on main panel
// start computing button to pass arguments to calculation
var start_button = ui.Button({
label: "Start Computing",
onClick: computeMap
});
start_button.style().set(style.start_button);
mainPanel.add(start_button);
var loading_button = ui.Label("⌛ Processing takes up to 1-2 min", style.dark_theme);
loading_button.style().set({color:"#FFFFFF75"});
mainPanel.add(loading_button);
var resultPanel = ui.Panel();
resultPanel.style().set(style.dark_theme);
mainPanel.add(resultPanel);
// center maps
map1.setCenter(70,30,5);
map2.setCenter(70,30,5);
// Update from country selection
// function after selection: zoom to country; no computation yet
// var admin0Selected = function(admin0Selection) {};
// admin0Select.onChange(admin0Selected); // set up response for drop down
// 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);
admin0Select.setDisabled(false); // enable menu
admin0Select.setPlaceholder('Select country'); // Change placeholder
});
// Get value from drop down and use it to compute layers for aoi
function computeMap(value) {
// waitPanel.widgets().reset([waitMessage]);
// loading_button.style().set({shown: true});
var admin_level = adminLevelSelect.getValue(); // get selected country
var ds = admin_dict[admin_level]; // dataset for the selected admin level
var country_name = admin0Select.getValue(); // get selected country
map1.clear(); // clear map
map2.clear();
var selectedAdmin0 = ADM0.filter(
ee.Filter.eq('shapeName', country_name)
);
map1.centerObject(selectedAdmin0,5); // center country
// customized basemap to enhance visuals
map1.setOptions(undefined, {'Custom': new_basemap});
map2.setOptions(undefined, {'Custom': new_basemap});
// add data layer label
var var1 = var1_selection.getValue();
var var2 = var2_selection.getValue();
// remove category label if applicable (only name of the dataset)
var var1_label = "👉 "+var1.replace(/.*(?=\|)/g, '').replace(/[|]/g,"").replace(/^\s/g,"");
var var2_label = "👉 "+var2.replace(/.*(?=\|)/g, '').replace(/[|]/g,"").replace(/^\s/g,"");
// use layer selection to control displayed labels
helper.add_popup(var1_label, info_dict[var1], map1, url_dict[var1]);
helper.add_popup(var2_label, info_dict[var2], map2, url_dict[var2]);
// use country name to compute aoi geometry
var selectedAdmin0 = ADM0.filter(ee.Filter.eq('shapeName', country_name));
var shapeGroup = ee.Feature(selectedAdmin0.first()).get('shapeGroup');
// get details at the admin 2 level
var aoi = ds.filter(ee.Filter.eq('shapeGroup', shapeGroup));
// var aoi = ADM2.filter(ee.Filter.eq('shapeGroup', shapeGroup));
// ger layer based on user selection, call aoi to get regional raster only
var layer1 = layer_dict[var1](aoi);
var layer2 = layer_dict[var2](aoi);
// do chart
var chart = helper.chart_climate_var(aoi, layer1, layer2, var1_label, var2_label);
// in case no missing chart (caused by invalid layers), display chart
if (chart < 1) {
var noChart = ui.Label(
"Invalid data layer(s) found. Please make sure both layers are valid for charting.",
style.dark_theme
);
noChart.style().set({color:"#FFFFFF"});
resultPanel.widgets().reset([noChart]);
} else {
resultPanel.widgets().reset([chart]);
}
// get choropleth maps for both maps
// vector, raster, map, reducer, log, output_name, legend_text
helper.zonalInspect(aoi, layer1, map1, "mean", log_dict[var1], var1, legend_dict[var1], color_dict[var1]);
helper.zonalInspect(aoi, layer2, map2, "mean", log_dict[var2], var2, legend_dict[var2], color_dict[var2]);
// set map control
map1.setControlVisibility(mapControl);
map2.setControlVisibility(mapControl);
map2.setControlVisibility({zoomControl: false});
}
var p1_heading = " Welcome! 👋";
var p2_heading = " Welcome! 👋";
var p3_heading = " Welcome! 👋";
var p1_message = "Want to retrieve high-resolution climate\n\
risk data from space? This climate hazard\n\
tool enables layer downloads and further \n\
GIS analysis in no time!";
var p2_message = "To begin with, select a country\n\
and two data layers which you\n\
would like to compare side to side.\n\
You could either compare among admin 1\n\
or admin 2 regions. Click start to\n\
compute and wait for a minute \n\
for the layers to load on-the-fly.\n\
You could download the derived table\n\
in csv by clicking on small icon located\n\
at the top-right corner of the chart.";
var p3_message = "Click layer label to view details\n\
of the dataset, including the project website.\n\
Let's get started!";
function addInstruction() {helper.addThreePage(
p1_heading, p1_message,
p2_heading, p2_message,
p3_heading, p3_message,
map1
)}
addInstruction();
};