-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVegetation analysis.js
128 lines (103 loc) · 3.95 KB
/
Vegetation analysis.js
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
// OBJECTIVE: Calculate variation of NDVI on a specific period
///////////////// CODE FOR CALCULATING NDVI ///////////////////////
var drawingTools = Map.drawingTools();
drawingTools.setShown(false);
Map.setCenter(13.58, 20.31, 3);
// Setup a while loop to clear all existing geometries that have been added as imports from drawing tools (from previously running the script). The design of the app is to handle charting a time series for a single geometry, so remove any that exist.
while (drawingTools.layers().length() > 0) {
var layer = drawingTools.layers().get(0);
drawingTools.layers().remove(layer);
}
var dummyGeometry =
ui.Map.GeometryLayer({geometries: null, name: 'geometry', color: '23cba7'});
drawingTools.layers().add(dummyGeometry);
function clearGeometry() {
var layers = drawingTools.layers();
layers.get(0).geometries().remove(layers.get(0).geometries().get(0));
}
function drawRectangle() {
clearGeometry();
drawingTools.setShape('rectangle');
drawingTools.draw();
}
function drawPolygon() {
clearGeometry();
drawingTools.setShape('polygon');
drawingTools.draw();
}
var chartPanel = ui.Panel({
style:
{height: '235px', width: '600px', position: 'bottom-right', shown: false}
});
Map.add(chartPanel);
function chartNdviTimeSeries() {
// Make the chart panel visible the first time a geometry is drawn.
if (!chartPanel.style().get('shown')) {
chartPanel.style().set('shown', true);
}
// Get the drawn geometry; it will define the reduction region.
var aoi = drawingTools.layers().get(0).getEeObject();
// Set the drawing mode back to null; turns drawing off.
drawingTools.setShape(null);
// Reduction scale is based on map scale to avoid memory/timeout errors.
var mapScale = Map.getScale();
var scale = mapScale > 5000 ? mapScale * 2 : 5000;
// Chart NDVI time series for the selected area of interest.
var chart = ui.Chart.image
.seriesByRegion({
imageCollection: ee.ImageCollection('MODIS/006/MOD13A2'),
regions: aoi,
reducer: ee.Reducer.mean(),
band: 'NDVI',
scale: scale,
xProperty: 'system:time_start'
})
.setOptions({
titlePostion: 'none',
legend: {position: 'none'},
hAxis: {title: 'Date'},
vAxis: {title: 'NDVI (x1e4)'},
series: {0: {color: '23cba7'}}
});
// Replace the existing chart in the chart panel with the new chart.
chartPanel.widgets().reset([chart]);
}
drawingTools.onDraw(ui.util.debounce(chartNdviTimeSeries, 500));
drawingTools.onEdit(ui.util.debounce(chartNdviTimeSeries, 500));
var symbol = {
rectangle: '⬛',
polygon: '🔺',
};
var controlPanel = ui.Panel({
widgets: [
ui.Label('1. Select a drawing mode.'),
ui.Button({
label: symbol.rectangle + ' Rectangle',
onClick: drawRectangle,
style: {stretch: 'horizontal'}
}),
ui.Button({
label: symbol.polygon + ' Polygon',
onClick: drawPolygon,
style: {stretch: 'horizontal'}
}),
ui.Label('2. Draw a geometry.'),
ui.Label('3. Wait for chart to render.'),
ui.Label(
'4. Repeat 1-3 or edit/move\ngeometry for a new chart.',
{whiteSpace: 'pre'})
],
style: {position: 'bottom-left'},
layout: null,
});
Map.add(controlPanel);
Map.add(ui.Label('Automated vegetation estimations (NDVI)', {fontWeight: 'bold', fontSize: '24px'}))
var img2020 = ee.ImageCollection('MODIS/006/MOD13A2').filterDate('2021-01-01', '2021-02-01').select('NDVI');
var NDVIVis = {
min: 0.0,
max: 6000.0,
opacity: 0.4,
palette: ['24126c', 'd4ff50', '1fff4f'],
};
Map.addLayer(img2020, NDVIVis, 'NDVI')
///////////////////////////////////////////////////////////////////