-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathaddPCA.js
31 lines (29 loc) · 1.04 KB
/
addPCA.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
/* This is a function that applies Principal Compnent Analysis
to imageCollection and returns 1st, 2nd and 3rd PCA components
Reference:
https://developers.google.com/earth-engine/edu
*/
var addPCA = function(image, bands) {
var arrayImage = image.select(bands).toArray();
var covar = arrayImage.reduceRegion({
reducer: ee.Reducer.covariance(),
tileScale: 1,
scale: 10000,
// maxPixels: 1e9,
bestEffort: true,
geometry: image.geometry()
});
var covarArray = ee.Array(covar.get('array'));
var eigens = covarArray.eigen();
var eigenVectors = eigens.slice(1,1);
var principalComponents = ee.Image(eigenVectors)
.matrixMultiply(arrayImage.toArray(1));
var pcImage = principalComponents.arrayProject([0])
.arrayFlatten([['pc1', 'pc2', 'pc3']]);
return image.addBands(pcImage);
};
var imPCA = function(imcollection, imbands){
var bands = imbands;
return imcollection.map(addPCA);
};
exports.imPCA = imPCA;