-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNDVIgif.js
230 lines (192 loc) · 8.41 KB
/
NDVIgif.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
var fires = ee.FeatureCollection("users/dgodwin/Reproject_Fire_Polygons"),
geometry = /* color: #d63000 */ee.Geometry.Point([-123.091123046875, 40.89708050390358]),
mapboundary =
/* color: #98ff00 */
/* displayProperties: [
{
"type": "rectangle"
}
] */
ee.Geometry.Polygon(
[[[-123.59642413761442, 41.40802878427711],
[-123.59642413761442, 40.46189407449804],
[-122.61864093448942, 40.46189407449804],
[-122.61864093448942, 41.40802878427711]]], null, false);
//Use this to create the main collection.
//Each element of the collection represents the surface reflectance bands, NDVI, and NDWI between May 28 and August 28 of the years 1997-2017
//There is no data for 2012, as it represents a gap in coverage between Landsat 5 and 8, during which Landsat 7 produced poor quality data
// Declare years and dates of interest, this can be changed at will without changing any other code
var L5startYear = 2000;
var L5endYear = 2011; //Always skip 2012, as there is no data
var L8startYear = 2013; //Always skip 2012, as there is no data
var L8endYear = 2022;
var startMonth = 6;
var startDay = 1;
var endMonth = 7;
var endDay = 31;
//Set the region for data collection as overlapping the study area shapefiles
var region = ee.FeatureCollection(fires).geometry()
//First, we create the cloud masking function
// This example demonstrates the use of the Landsat 4, 5, 7 Collection 2,
// Level 2 QA_PIXEL band (CFMask) to mask unwanted pixels.
function maskL457sr(image) {
// Bit 0 - Fill
// Bit 1 - Dilated Cloud
// Bit 2 - Unused
// Bit 3 - Cloud
// Bit 4 - Cloud Shadow
var qaMask = image.select('QA_PIXEL').bitwiseAnd(parseInt('11111', 2)).eq(0);
var saturationMask = image.select('QA_RADSAT').eq(0);
// Apply the scaling factors to the appropriate bands.
var opticalBands = image.select('SR_B.').multiply(0.0000275).add(-0.2);
var thermalBand = image.select('ST_B6').multiply(0.00341802).add(149.0);
// Replace the original bands with the scaled ones and apply the masks.
return image.addBands(opticalBands, null, true)
.addBands(thermalBand, null, true)
.updateMask(qaMask)
.updateMask(saturationMask);
}
// Get all data in the desired range and apply the mask
var L5collection = ee.ImageCollection('LANDSAT/LT05/C02/T1_L2')
.filter(ee.Filter.calendarRange(L5startYear, L5endYear, "year"))
.filter(ee.Filter.calendarRange(startMonth, endMonth, "month"))
.filterBounds(region)
.map(maskL457sr);
//Define the function to add NDVI and NDWI
var L5addNDVIandNDWIandNBR = function(image) {
var ndvi = image.normalizedDifference(['SR_B5', 'SR_B4']).rename('NDVI');
var ndwi = image.normalizedDifference(['SR_B2', 'SR_B4']).rename('NDWI');
var nbr = image.normalizedDifference(['SR_B4', 'SR_B7']).rename('NBR');
return image.addBands(ndvi).addBands(ndwi).addBands(nbr);
};
//Add the NDVI and NDWI to the image
var L5collection = L5collection.map(L5addNDVIandNDWIandNBR)
var L5years = ee.List.sequence(L5startYear, L5endYear);
// Map a function to select data within the year and apply median reducer
var L5summermedian = ee.ImageCollection.fromImages(
L5years.map(function(year) {
var startDate = ee.Date.fromYMD(year, startMonth, startDay);
var endDate = ee.Date.fromYMD(year, endMonth, endDay);
var annual = L5collection
.filterDate(startDate, endDate)
.median();
return annual
.set('year', year)
.set('system:time_start', ee.Date.fromYMD(year, 1, 1).format("YYYY_MM_dd"))
})
);
function maskL8sr(image) {
// Bit 0 - Fill
// Bit 1 - Dilated Cloud
// Bit 2 - Cirrus
// Bit 3 - Cloud
// Bit 4 - Cloud Shadow
var qaMask = image.select('QA_PIXEL').bitwiseAnd(parseInt('11111', 2)).eq(0);
var saturationMask = image.select('QA_RADSAT').eq(0);
// Apply the scaling factors to the appropriate bands.
var opticalBands = image.select('SR_B.').multiply(0.0000275).add(-0.2);
var thermalBands = image.select('ST_B.*').multiply(0.00341802).add(149.0);
// Replace the original bands with the scaled ones and apply the masks.
return image.addBands(opticalBands, null, true)
.addBands(thermalBands, null, true)
.updateMask(qaMask)
.updateMask(saturationMask);
}
// Map the function over one year of data.
var L8collection = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
.filter(ee.Filter.calendarRange(L8startYear, L8endYear, "year"))
.filter(ee.Filter.calendarRange(startMonth, endMonth, "month"))
.filterBounds(region)
.map(maskL8sr);
//Define the function to add NDVI and NDWI
var L8addNDVIandNDWIandNBR = function(image) {
var ndvi = image.normalizedDifference(['SR_B6', 'SR_B5']).rename('NDVI');
var ndwi = image.normalizedDifference(['SR_B3', 'SR_B5']).rename('NDWI');
var nbr = image.normalizedDifference(['SR_B5', 'SR_B7']).rename('NBR');
return image.addBands(ndvi).addBands(ndwi).addBands(nbr);
};
//Add the NDVI and NDWI to the image
var L8collection = L8collection.map(L8addNDVIandNDWIandNBR)
var L8years = ee.List.sequence(L8startYear, L8endYear);
// Map a function to select data within the year and apply median reducer
var L8summermedian = ee.ImageCollection.fromImages(
L8years.map(function(year) {
var startDate = ee.Date.fromYMD(year, startMonth, startDay);
var endDate = ee.Date.fromYMD(year, endMonth, endDay);
var annual = L8collection
.filterDate(startDate, endDate)
.median();
return annual
.set('year', year)
.set('system:time_start', ee.Date.fromYMD(year, 1, 1).format("YYYY_MM_dd"))
})
);
var maincollection = L5summermedian.merge(L8summermedian);
var comp = maincollection.select("NDVI")
print (comp, "comp")
var mask = fires.geometry()
var region = fires.geometry()
Map.addLayer(fires)
// Define GIF visualization parameters.
var args = {
'region': mapboundary,
'dimensions': 600,
'crs': 'EPSG:3857',
'framesPerSecond': 4
};
var text = require('users/gena/packages:text'); // Import gena's package which allows text overlay on image
var style = require('users/gena/packages:style');
var utils = require('users/gena/packages:utils');
var annotations = [
{position: 'right', offset: '1%', margin: '1%', property: 'label', scale: 500} //large scale because image if of the whole world. Use smaller scale otherwise
]
function addText(image){
var timeStamp = ee.String('Date: ').cat(ee.String(image.get('year')).slice(0,4)); //convert time stamp to string
var image = image.visualize({ //convert each frame to RGB image explicitly since it is a 1 band image
forceRgbOutput: true,
min: -1,
max: 1,
palette: ['640000', 'ff0000', 'ffff00', '00c800', '006400']
}).set({'label':timeStamp}); // set a property called label for each image
var annotated = text.annotateImage(image, {}, geometry, annotations); // create a new image with the label overlayed using gena's package
return annotated
}
var collection = comp.map(addText) //add time stamp to all images
var firecollection = ee.FeatureCollection(mask);
// Define an empty image to paint features to.
var empty = ee.Image().byte();
// Paint country feature edges to the empty image.
var fireOutline = empty
.paint({featureCollection: firecollection, color: 1, width: 1})
// Convert to an RGB visualization image; set line color to black.
.visualize({palette: '000000'});
var visParams = {
min: -1,
max: 1,
palette: ['640000', 'ff0000', 'ffff00', '00c800', '006400']
};
var geometryGradientBar = ee.Geometry.Polygon(
[[[-123.04621730340197,40.53782342145082],
[-122.66444240105822,40.53782342145082],
[-122.66444240105822,40.56671132260011],
[-123.04621730340197,40.56671132260011]]], null, false);
var min = -1;
var max = 1;
var textProperties = {
fontSize: 32,
textColor: 'ffffff',
outlineColor: '000000',
outlineWidth: 0,
outlineOpacity: 0.6
};
var labels = ee.List.sequence(min, max);
var gradientBar = style.GradientBar.draw(geometryGradientBar, {
min: min, max: max, palette: visParams.palette, labels: labels,
format: '%.0f', text: textProperties
});
// Map a blend operation over the temperature collection to overlay the country
// border outline image on all collection images.
var collection = collection.map(function(img) {
return img.blend(fireOutline).blend(gradientBar);
});
print(ui.Thumbnail(collection,args));