-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathee_advanceAnalytics.js
278 lines (232 loc) · 7.97 KB
/
ee_advanceAnalytics.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
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
/**
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* This Earth Engine script provides the ability to map tree cover change over
* Palm Oil Mill polygons and provides more in depth analytics. The script also
* generates an Earth Engine task which exports a CSV containing statistics
* aboout the mills to a Cloud Storage bucket in a CSV.
*/
var hansen = ee.Image("UMD/hansen/global_forest_change_2020_v1_8"),
project = 'PROJECT_ID',
// asset = 'ASSET_ID',
errorMargin = 1e7,
importBucket = 'PROJECT_ID-ee_export_bucket',
table = ee.FeatureCollection("projects/"+project+"/assets/"+ asset);
//Set UI components
/**************
* Components *
**************/
var rootPanel = ui.Panel();
var mapPanel = ui.Panel();
var outputPanel = ui.Panel();
// Configure the map panel.
var mainMap = ui.Map();
mainMap.setCenter(116.4194,0.5387, 7)
.setOptions('Satellite');
// Add map layers
// Displaying forest, loss, gain, and pixels where both loss and gain occur.
var lossImage = hansen.select(['loss']);
var gainImage = hansen.select(['gain']);
var treeCover = hansen.select(['treecover2000']);
// Use the and() method to create the lossAndGain image.
var gainAndLoss = gainImage.and(lossImage);
// Set each layer to "false" so the user can turn them on later
// Add the loss layer in red.
var lossImageLayer = ui.Map.Layer(lossImage.updateMask(lossImage),
{palette: ['FF0000']}, 'Loss',false);
// Add the gain layer in blue.
var gainImageLayer = ui.Map.Layer(gainImage.updateMask(gainImage),
{palette: ['0000FF']}, 'Gain',false);
// Show the loss and gain image.
var lossAndGainImageLayer = ui.Map.Layer(gainAndLoss.updateMask(gainAndLoss),
{palette: 'FF00FF'}, 'Gain and Loss',false);
// Add these layers to our map. They will be added but not displayed
mainMap.add(lossImageLayer)
.add(gainImageLayer)
.add(lossAndGainImageLayer);
mapPanel.add(mainMap);
// Configure the output panel.
// Check boxes
var treeCover = ui.Label({
value:'Tree Cover', style: {fontWeight: 'bold', fontSize: '16px'}
});
// Create checkboxes that will allow the user to view the extent
// map for different years.
// Creating the checkbox will not do anything yet, functionality added in
// Behaviors section below
var lossCheck = ui.Checkbox('Tree cover loss').setValue(false);
var gainCheck = ui.Checkbox('Tree cover gain').setValue(false);
var lossAndGainCheck = ui.Checkbox('Tree cover gain and loss').setValue(false);
// Labels
var taskStatus = ui.Label('');
var statsHeader = ui.Label(
'Supplier Statistics',{fontWeight: 'bold', fontSize: '16px'});
var numMillsLabel = ui.Label('');
var totalChangeLabel = ui.Label('');
var avgChangeLabel = ui.Label('');
var maxChangeLabel = ui.Label('');
// Add components to panel
outputPanel.add(taskStatus)
.add(statsHeader)
.add(numMillsLabel)
.add(totalChangeLabel)
.add(avgChangeLabel)
.add(maxChangeLabel)
.add(treeCover)
.add(lossCheck)
.add(gainCheck)
.add(lossAndGainCheck);
/***********
* Styling *
***********/
rootPanel.style().set({
height: '100%',
width: '100%'
});
mapPanel.style().set({
height: '90%',
width: '60%',
});
outputPanel.style().set({
height: '90%',
width: '50%',
backgroundColor: 'white',
});
// Configure the layouts for how the panels flow together.
ui.root.setLayout(ui.Panel.Layout.flow('vertical'))
rootPanel.setLayout(ui.Panel.Layout.flow('horizontal'));
/***************
* Composition *
***************/
ui.root.clear();
ui.root.add(rootPanel);
rootPanel.add(mapPanel);
rootPanel.add(outputPanel);
//Get the most recent data
/**********************
* Behaviors and data *
**********************/
// Get the current date
var currentDate = Date.now();
// List the assets (all with same date format as asset id)
var assetList = ee.data.listBuckets('projects/' + project);
var assets = assetList['assets'];
// Initialize variables
var smallestDiff = 0;
var desiredAsset = new Date();
// Loop through assets and find the difference between today and the
// date of the asset
for (var asset in assets) {
// Set asset name is now the date
var id = assets[asset]['name'];
var assetName = id.split("/")[3];
// Create a date object
var assetDate = new Date(assetName);
// Find the difference between the date of the asset and the current date
var diff = currentDate - assetDate;
// Reset the smallestDiff if this is the first asset or if this asset is more
// recent than the current most recent asset
if ((asset == '0') || (diff < smallestDiff)) {
smallestDiff = diff
desiredAsset = assetDate
}
}
// Set string
var desiredAssetString = desiredAsset.toLocaleString('default',{ month: 'short'})
+ '-' + desiredAsset.getDate( ) + '-' +desiredAsset.getFullYear( );
// Set asset with desired date as data to use.
table = ee.FeatureCollection(
'projects/' + project + '/assets/' + desiredAssetString);
// Add polygons to map
mainMap.addLayer(table, {}, 'default display');
//Tree cover analysis
// Get the forest loss image.
var areaImage = lossImage.multiply(ee.Image.pixelArea());
var lossYear = hansen.select(['lossyear']);
// This function computes the feature's geometry area and forest loss
// and adds them as a properties.
var addProps = function(feature) {
var stats = areaImage.reduceRegion({
reducer: ee.Reducer.sum(),
geometry: feature.geometry(),
scale: 30,
maxPixels: 1e9
});
return feature.set({areaHa: feature.geometry().area().divide(100 * 100),
forestLoss: stats.get('loss')});
};
// Map the area and forest loss getting function over the FeatureCollection.
var propsAdded = table.map(addProps);
// Pull out interesting statistics
var totalLoss = ee.Number(propsAdded.aggregate_sum('forestLoss')).round();
var totalChange = ee.Number(propsAdded.aggregate_sum('forestLoss')).round();
var avgChange = ee.Number(propsAdded.aggregate_mean('forestLoss')).round();
var maxChange = ee.Number(propsAdded.aggregate_max('forestLoss')).round();
var changeStats = propsAdded.aggregate_stats('forestLoss');
var numMills = ee.Number(changeStats.get('total_count'));
//Add checkbox functionality for map layers and set UI panel info
// Set info in UI panel
numMills.evaluate(function callback(value) {
numMillsLabel.setValue("Number of mills: " + value + " mills");
});
avgChange.evaluate(function callback(value) {
var valueHA = value / 10000
avgChangeLabel.setValue("Average tree cover change: " + valueHA + " HA");
});
totalChange.evaluate(function callback(value) {
var valueHA = value / 10000
totalChangeLabel.setValue("Total tree cover change: " + valueHA + " HA");
});
maxChange.evaluate(function callback(value) {
var valueHA = value / 10000
maxChangeLabel.setValue("Max tree cover change: " + valueHA + " HA");
});
// Set task status
taskStatus.setValue("Data Import Status : " + desiredAssetString)
// Create a function for each checkbox so that clicking the checkbox turns
// on layers of interest.
// Loss
var doLossCheckbox = function() {
lossCheck.onChange(function(checked){
lossImageLayer.setShown(checked);
});
}
doLossCheckbox();
// Gain
var doGainCheckbox = function() {
gainCheck.onChange(function(checked){
gainImageLayer.setShown(checked);
});
}
doGainCheckbox();
// Loss and Gain
var doLossAndGainCheckbox = function() {
lossAndGainCheck.onChange(function(checked){
lossAndGainImageLayer.setShown(checked);
});
}
doLossAndGainCheckbox();
//Add export task
// Export a CSV file to Cloud Storage.
var aggregateFeature = ee.FeatureCollection(table.geometry(), changeStats);
var filename = 'changeStats' + Date()
Export.table.toCloudStorage({
collection: aggregateFeature,
description:'advancedAnalyticsToCloudStorage',
bucket: importBucket,
fileNamePrefix: filename ,
fileFormat: 'CSV'
});