-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
188 lines (152 loc) · 5.64 KB
/
utils.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
/*
Google Earth Engine module for working with Landsat annual
composites and surface reflectance-derived spectral indices.
Copyright: SkyTruth
License: Apache 2.0
Contact: Ry Covington ([email protected])
Date Updated: 07-09-2021
*/
function renameOli (img) {
// @param {ee.Image} img - a Landsat 8 image.
// returns {ee.Image}
return img.select(
['B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'pixel_qa'],
['B', 'G', 'R', 'NIR', 'SWIR1', 'SWIR2', 'pixel_qa']
);
};
function renameEtm (img) {
// @param {ee.Image} img - a Landsat 4, 5, or 7 image.
// @returns {ee.Image}
return img.select(
['B1', 'B2', 'B3', 'B4', 'B5', 'B7', 'pixel_qa'],
['B', 'G', 'R', 'NIR', 'SWIR1', 'SWIR2', 'pixel_qa']
);
};
function etmToOli (img) {
// @param {ee.Image} img - a Landsat 4, 5, or 7 image.
// @returns {ee.Image}
// Following the ETM+ to OLI Harmonization steps provided on by the EE Developer's Guide:
// https://developers.google.com/earth-engine/tutorials/community/landsat-etm-to-oli-harmonization
var coefficients = {
itcps: ee.Image.constant([-0.0095, -0.0016, -0.0022, -0.0021, -0.0030, 0.0029]).multiply(10000),
slopes: ee.Image.constant([0.9785, 0.9542, 0.9825, 1.0073, 1.0171, 0.9949])
};
return ee.Image(img.select(['B', 'G', 'R', 'NIR', 'SWIR1', 'SWIR2']) // Need explicit cast to ee.Image()
.multiply(coefficients.slopes)
.add(coefficients.itcps)
.round()
.toShort()
.addBands(img.select('pixel_qa'))
.copyProperties(img, img.propertyNames()) // Returns an ee.Element()
)
};
function maskClouds (img) {
// @param {ee.Image} img - a Landsat 4, 5, 7, or 8 image.
// @returns {ee.Image}
var cloudShadowBitMask = 1 << 3;
var cloudsBitMask = 1 << 5;
var qa = img.select('pixel_qa');
var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0).and(qa.bitwiseAnd(cloudsBitMask).eq(0));
return img.updateMask(mask);
};
function addMetrics(img){
// @param {ee.Image} img - a Landsat 4, 5, 7, or 8 image.
// @returns {ee.Image}
var ndvi = img.normalizedDifference(['NIR','R']).rename('NDVI');
var ndmi = img.normalizedDifference(['NIR','SWIR1']).rename('NDMI');
var nbr = img.normalizedDifference(['NIR', 'SWIR2']).rename('NBR');
var nbr2 = img.normalizedDifference(['SWIR1', 'SWIR2']).rename('NBR2');
var evi = img.expression({
expression: '(G * ((NIR - R) / (NIR + C1 * R - C2 * B + L)))',
map: {
'G': 2.5,
'NIR': img.select('NIR'),
'R': img.select('R'),
'C1':6,
'C2':7.5,
'B':img.select('B'),
'L':1
}
}).rename('EVI');
var savi = img.expression({
expression: '((C + L) * ((NIR - R) / (NIR + R + L)))',
map: {
'NIR': img.select('NIR'),
'R': img.select('R'),
'C': 1,
'L': 0.5
}
}).rename('SAVI');
var msavi = img.expression({
expression: '((C * NIR - sqrt(((C * NIR + C2)**C3) - C4 * (NIR - R))) / C5)',
map: {
'C': 2,
'NIR': img.select('NIR'),
'R': img.select('R'),
'C2': 1,
'C3': 2,
'C4': 8,
'C5': 2
}
}).rename('MSAVI');
return img.addBands([ndvi, ndmi, evi, savi, msavi, nbr, nbr2]);
};
function prepOli(img) {
// @param {ee.Image} img - a Landsat 8 image.
// @returns {ee.Image}
var orig = img;
img = renameOli(img);
img = maskClouds(img);
img = addMetrics(img);
return ee.Image(img.copyProperties(orig, orig.propertyNames()));
};
function prepEtm(img) {
// @param {ee.Image} img - a Landsat 4, 5, or 7 image.
// @returns {ee.Image}
var orig = img;
img = renameEtm(img);
img = maskClouds(img);
img = etmToOli(img);
img = addMetrics(img);
return ee.Image(img.copyProperties(orig, orig.propertyNames()));
};
exports.makeAnnual = function(feature, year){
// @param {ee.Feature} feature - in this case, a single basin (huc6).
// @param {int} - the year to make the composite.
// @returns {ee.Image}
var landsat4 = ee.ImageCollection("LANDSAT/LT04/C01/T1_SR")
.filter(ee.Filter.calendarRange(year, year, 'year'))
.filter(ee.Filter.calendarRange(5, 9, 'month'))
.filterBounds(feature.geometry())
.map(prepEtm);
var landsat5 = ee.ImageCollection("LANDSAT/LT05/C01/T1_SR")
.filter(ee.Filter.calendarRange(year, year, 'year'))
.filter(ee.Filter.calendarRange(5, 9, 'month'))
.filterBounds(feature.geometry())
.map(prepEtm);
var landsat7 = ee.ImageCollection("LANDSAT/LE07/C01/T1_SR")
.filter(ee.Filter.calendarRange(year, year, 'year'))
.filter(ee.Filter.calendarRange(5, 9, 'month'))
.filterBounds(feature.geometry())
.map(prepEtm);
var landsat8 = ee.ImageCollection("LANDSAT/LC08/C01/T1_SR")
.filter(ee.Filter.calendarRange(year, year, 'year'))
.filter(ee.Filter.calendarRange(5, 9, 'month'))
.filterBounds(feature.geometry())
.map(prepOli);
// All images.
var collects = ee.ImageCollection((landsat4).merge(landsat5).merge(landsat7).merge(landsat8));
return collects.median().set('Year', year);
};
exports.makeStats = function(image, collection){
// @param {ee.Image} image - an annual composite.
// @param {ee.FeatureCollection} collection - a feature collection of subwatersheds (huc12s).
// @returns {ee.FeatureCollection} data - a feature collection with summary stats for each band.
var data = image.reduceRegions({
collection: collection,
reducer: ee.Reducer.mean(),
scale: 30,
tileScale: 4
});
return data;
};