目录
错误提示1
错误提示2
原始的教程链接:
错误代码
修正后的代码
结果
错误提示1
这个是因为原始GEE教程中给的让我们填入需要进行计算的波段名称,而且是以list的形式传入。
错误提示2
这里我们虽然传入了正确的波段名称,但是发现要求的list要多一个中括号,所以我们再次修改代码。
原始的教程链接:
https://developers.google.com/earth-engine/guides/arrays_eigen_analysis#code-editor-javascript
错误代码
var roi = /* color: #d63000 *//* displayProperties: [{"type": "rectangle"}] */ee.Geometry.Polygon([[[-80.39442194432449, 25.89616042988596],[-80.39442194432449, 25.698332029213574],[-80.21864069432449, 25.698332029213574],[-80.21864069432449, 25.89616042988596]]], null, false);var getPrincipalComponents = function(centered, scale, region) {// Collapse the bands of the image into a 1D array per pixel.var arrays = centered.toArray();// Compute the covariance of the bands within the region.var covar = arrays.reduceRegion({reducer: ee.Reducer.centeredCovariance(),geometry: region,scale: scale,maxPixels: 1e9});// Get the 'array' covariance result and cast to an array.// This represents the band-to-band covariance within the region.var covarArray = ee.Array(covar.get('array'));// Perform an eigen analysis and slice apart the values and vectors.var eigens = covarArray.eigen();// This is a P-length vector of Eigenvalues.var eigenValues = eigens.slice(1, 0, 1);// This is a PxP matrix with eigenvectors in rows.var eigenVectors = eigens.slice(1, 1);// Convert the array image to 2D arrays for matrix computations.var arrayImage = arrays.toArray(1);// Left multiply the image array by the matrix of eigenvectors.var principalComponents = ee.Image(eigenVectors).matrixMultiply(arrayImage);// Turn the square roots of the Eigenvalues into a P-band image.var sdImage = ee.Image(eigenValues.sqrt()).arrayProject([0]).arrayFlatten([getNewBandNames('sd')]);// Turn the PCs into a P-band image, normalized by SD.return principalComponents// Throw out an an unneeded dimension, [[]] -> []..arrayProject([0])// Make the one band array image a multi-band image, [] -> image..arrayFlatten([getNewBandNames('pc')])// Normalize the PCs by their SDs..divide(sdImage);
};var image = ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA').filterBounds(roi).filterDate('2020-01-01','2020-03-01').mosaic().select(['B2', 'B3', 'B4', 'B5', 'B6', 'B7'])var centered = image.subtract(image.reduceRegion({'reducer': ee.Reducer.mean(), 'geometry': roi, 'scale': 30, 'maxPixels': 1e13}))var principal_components = getPrincipalComponents(image, 30, roi)print('principal_components',principal_components)
修正后的代码
var roi = /* color: #d63000 *//* displayProperties: [{"type": "rectangle"}] */ee.Geometry.Polygon([[[-80.39442194432449, 25.89616042988596],[-80.39442194432449, 25.698332029213574],[-80.21864069432449, 25.698332029213574],[-80.21864069432449, 25.89616042988596]]], null, false);var getPrincipalComponents = function(centered, scale, region) {// Collapse the bands of the image into a 1D array per pixel.var arrays = centered.toArray();// Compute the covariance of the bands within the region.var covar = arrays.reduceRegion({reducer: ee.Reducer.centeredCovariance(),geometry: region,scale: scale,maxPixels: 1e9});// Get the 'array' covariance result and cast to an array.// This represents the band-to-band covariance within the region.var covarArray = ee.Array(covar.get('array'));// Perform an eigen analysis and slice apart the values and vectors.var eigens = covarArray.eigen();// This is a P-length vector of Eigenvalues.var eigenValues = eigens.slice(1, 0, 1);// This is a PxP matrix with eigenvectors in rows.var eigenVectors = eigens.slice(1, 1);// Convert the array image to 2D arrays for matrix computations.var arrayImage = arrays.toArray(1);// Left multiply the image array by the matrix of eigenvectors.var principalComponents = ee.Image(eigenVectors).matrixMultiply(arrayImage);// Turn the square roots of the Eigenvalues into a P-band image.var sdImage = ee.Image(eigenValues.sqrt()).arrayProject([0]).arrayFlatten([['B2_sd', 'B3_sd', 'B4_sd', 'B5_sd', 'B6_sd', 'B7_sd']]);// Turn the PCs into a P-band image, normalized by SD.return principalComponents// Throw out an an unneeded dimension, [[]] -> []..arrayProject([0])// Make the one band array image a multi-band image, [] -> image..arrayFlatten([['B2_pc', 'B3_pc', 'B4_pc', 'B5_pc', 'B6_pc', 'B7_pc']])// Normalize the PCs by their SDs..divide(sdImage);
};var image = ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA').filterBounds(roi).filterDate('2020-01-01','2020-03-01').mosaic().select(['B2', 'B3', 'B4', 'B5', 'B6', 'B7'])var centered = image.subtract(image.reduceRegion({'reducer': ee.Reducer.mean(), 'geometry': roi, 'scale': 30, 'maxPixels': 1e13}))var principal_components = getPrincipalComponents(image, 30, roi)print('principal_components',principal_components)