-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRainfall and NDVI
345 lines (307 loc) · 8.13 KB
/
Rainfall and NDVI
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
// Model section.
var time = [];
for (var i = 1; i <= 10; i++) {
time.push(i);
}
time = time.map(function(n) {
return {
label: n.toString(),
value: n
};
});
var Ghana = ee.FeatureCollection("FAO/GAUL/2015/level2")
.filter(ee.Filter.eq("ADM0_CODE", 133));
var states = Ghana.aggregate_array("ADM1_NAME")
.distinct()
.remove("Name Unknown")
.sort();
var cities = states.map(function(state) {
return Ghana
.filter(ee.Filter.eq("ADM1_NAME", state))
.aggregate_array("ADM2_NAME")
.sort();
});
var statesMap = ee.Dictionary.fromLists(states, cities);
// ------------------------------
// CHIRPS Daily to CHIRPS Monthly
var startChirps = 1981;
var endChirps = new Date().getFullYear();
var years = ee.List.sequence(startChirps, endChirps);
var months = ee.List.sequence(1, 12);
var chirps = ee.ImageCollection("UCSB-CHG/CHIRPS/DAILY");
chirps = ee.ImageCollection.fromImages(years.map(function(y) {
return months.map(function(m) {
var imageYearYMonthM = chirps
.filter(ee.Filter.calendarRange(y, y, "year"))
.filter(ee.Filter.calendarRange(m, m, "month"))
.reduce(ee.Reducer.sum());
return imageYearYMonthM.set({
"system:time_start": ee.Date.fromYMD(y, m, 1).millis(),
});
});
}).flatten());
// ------------------------------
// -------------------------------------------------------
// MOD13Q1.061 Terra Vegetation Indices 16-Day Global 250m
var modis = ee.ImageCollection("MODIS/061/MOD13Q1").select("NDVI", "EVI");
modis = modis.map(function(image) {
return image.multiply(0.0001).copyProperties(image, image.propertyNames());
});
var ndvi = modis.select("NDVI");
var evi = modis.select("EVI");
// -------------------------------------------------------
var m = {};
m.states = states.getInfo();
m.statesMap = statesMap.getInfo();
m.data = {
"Precipitation": {
"CHIRPS": {
images: chirps,
scale: 5566
}
},
"Spectral Index": {
"NDVI": {
images: ndvi,
scale: 250
},
"EVI": {
images: evi,
scale: 250
}
}
};
// Some helper functions.
function show(widget) {
widget.style().set("shown", true);
}
function hide(widget) {
widget.style().set("shown", false);
}
function enable(widget) {
widget.setDisabled(false);
widget.style().set("color", "black");
}
function disable(widget) {
widget.setDisabled(true);
widget.style().set("color", "grey");
}
var App = {};
// ---------------
// Widgets section
var stateSelector = ui.Select(m.states);
var citySelector = ui.Select();
var dataSelector = ui.Select(Object.keys(m.data));
var indiceSelector = ui.Select(Object.keys(m.data["Spectral Index"]));
var yearSelector = ui.Select(time);
yearSelector.setValue(10);
hide(indiceSelector);
var statePanel = ui.Panel({
widgets: [
ui.Label("1. Select a Region"),
stateSelector
]
});
var cityPanel = ui.Panel({
widgets: [
ui.Label("2. Select a Municipality"),
citySelector
]
});
var dataPanel = ui.Panel({
widgets: [
ui.Label("3. Select the desired info"),
ui.Panel([dataSelector, indiceSelector], ui.Panel.Layout.flow("horizontal"))
]
});
var yearPanel = ui.Panel({
widgets: [
ui.Label("4. Select the Time series (in years)"),
yearSelector
]
});
var timeSeriesButton = ui.Button("Generate Time series");
var mainPanel = ui.Panel({
widgets: [
statePanel,
cityPanel,
dataPanel,
yearPanel,
timeSeriesButton
],
style: {
position: "middle-left"
}
});
var chartContainer = ui.Panel();
var chartText = ui.Label({
value: "Click this button to see the options \"Download CSV\" 👇",
style: {
textAlign: "right",
shown: false,
stretch: "horizontal"
}
}
);
var chartPanel = ui.Panel({
widgets: [
chartText,
chartContainer
],
style: {
width: "600px",
position: "middle-right"
}
});
function mapAndCenterSelectedState(id) {
var fv = ui.Map.FeatureViewLayer("FAO/GAUL/2015/level1_FeatureView");
fv.setVisParams({
rules: [
{
filter: ee.Filter.neq("ADM1_CODE", id),
isVisible: false
}
]
});
Map.layers().reset([fv]);
}
function mapAndCenterSelectedCity(id) {
var fv = ui.Map.FeatureViewLayer("FAO/GAUL/2015/level2_FeatureView");
fv.setVisParams({
rules: [
{
filter: ee.Filter.neq("ADM2_CODE", id),
isVisible: false
}
]
});
Map.layers().reset([fv]);
}
function showCityPanel(value) {
hide(dataPanel);
hide(indiceSelector);
hide(yearPanel);
hide(chartPanel);
disable(timeSeriesButton);
dataSelector.setValue(null);
var selected = Ghana.filter(ee.Filter.eq("ADM1_NAME", value));
Map.centerObject(selected);
var id = ee.Feature(selected.first()).get("ADM1_CODE");
id.evaluate(function(id) {
mapAndCenterSelectedState(id);
});
citySelector.items().reset([]);
var items = m.statesMap[value];
items.forEach(function(item) {
citySelector.items().add(item);
});
show(cityPanel);
}
function showDataPanel(value) {
var selected = Ghana.filter(ee.Filter.eq("ADM2_NAME", value));
Map.centerObject(selected);
var id = ee.Feature(selected.first()).get("ADM2_CODE");
id.evaluate(function(id) {
mapAndCenterSelectedCity(id);
});
show(dataPanel);
var isChartPanelVisible = chartPanel.style().get("shown") == true;
if (isChartPanelVisible) {
createChart();
}
App.region = ee.Feature(selected.first()).geometry();
}
function showYearPanel(value) {
enable(timeSeriesButton);
show(yearPanel);
indiceSelector.setValue(indiceSelector.items().get(0));
switch (value) {
case "Spectral Index":
var defaultValue = indiceSelector.getValue();
App.imageCollection = m.data["Spectral Index"][defaultValue].images;
App.scale = m.data["Spectral Index"][defaultValue].scale;
indiceSelector.onChange(function(value) {
App.imageCollection = m.data["Spectral Index"][value].images;
App.scale = m.data["Spectral Index"][value].scale;
});
show(indiceSelector);
break;
case "Precipitation":
hide(indiceSelector);
App.imageCollection = m.data["Precipitation"]["CHIRPS"].images;
App.scale = m.data["Precipitation"]["CHIRPS"].scale;
break;
}
}
function createChart() {
var thisYear = ee.Date(Date.now());
var delta = yearSelector.getValue();
var start = thisYear.advance(-delta, "years");
var dataType = dataSelector.getValue();
var chartType;
var options;
var to = new Date().getFullYear();
var from = to - yearSelector.getValue();
var city = citySelector.getValue();
var state = stateSelector.getValue();
switch (dataType) {
case "Precipitation":
chartType = "ColumnChart";
options = {
title: "Average Monthly Precipitation in " + city + ", " + state + " (" + from + "-" + to + ")",
hAxis: {
title: "Data",
format: (yearSelector.getValue() == 1) ? "MMM/yy" : "M/yy"
},
vAxis: {
title: "Precipitation (mm)"
},
legend: {
position: "none"
}
};
break;
case "Spectral index":
chartType = "LineChart";
var indice = indiceSelector.getValue();
options = {
title: "Série temporal do " + indice + " no período de " + from + " a " + to + " em " + city + ", " + state,
hAxis: {
title: "Data",
format: (yearSelector.getValue() == 1) ? "MMM/yy" : "M/yy"
},
curveType: "function",
legend: {
position: "none"
}
};
break;
}
var chart = ui.Chart.image.series({
imageCollection: App.imageCollection.filterDate(start, thisYear),
region: App.region,
scale: App.scale
}).setChartType(chartType)
.setOptions(options);
chartContainer.widgets().reset([chart]);
show(chartPanel);
ui.util.setTimeout(function() {
show(chartText);
}, 6000);
}
timeSeriesButton.onClick(createChart);
stateSelector.onChange(showCityPanel);
citySelector.onChange(showDataPanel);
dataSelector.onChange(showYearPanel);
yearSelector.onChange(function() {
enable(timeSeriesButton);
});
hide(cityPanel);
hide(dataPanel);
hide(yearPanel);
hide(chartPanel);
disable(timeSeriesButton);
Map.setControlVisibility(false);
Map.setCenter(-51.42, -15.13, 4);
Map.add(mainPanel);
Map.add(chartPanel);