直接附上代码,在学习过程中又不需要了,但怕忘记便记录下来。
该为一个函数,所需要分析的影像数据 添加至
如下所示
//var LAI_mean=ee.ImageCollection(LAI_list).median();
//var NDVI_mean=ee.ImageCollection(NDVI_list).median();
var NDVI_LAI=LAI_mean.select('Lai').addBands(NDVI_mean.select('NDVI')).rename(['Lai','NDVI']);
get_value(NDVI_LAI)//调用函数
function get_value(combo){
// Create 5000 random points in the region.在研究区中获得随机点
var randomPoints = ee.FeatureCollection.randomPoints(region,5000);
var imgSamp = combo.sampleRegions({
collection: randomPoints,
scale: 500,
tileScale:16
})
// Add a constant property to each feature to be used as an independent variable.
.map(function(feature) {
return feature.set('constant', 1);设定一个常数
});
Map.addLayer(randomPoints, {}, 'random points');
//
//return imgSamp
//Map.addLayer(randomPoints,{},'point')
var linearRegression = ee.Dictionary(imgSamp.reduceColumns({
reducer: ee.Reducer.linearRegression({
numX: 2,
numY: 1
}),
selectors: ['constant', 'NDVI', 'Lai'] ndvi为x lai为y
}));
// Convert the coefficients array to a list.
var coefList = ee.Array(linearRegression.get('coefficients')).toList();
//print(coefList,'coe')
var res = ee.Array(linearRegression.get('residuals')).toList();
//print(res,'res')
// Extract the y-intercept and slope.
var yInt = ee.List(coefList.get(0)).get(0); // y-intercept
var slope = ee.List(coefList.get(1)).get(0); // slope
print(slope,'slope')
print(yInt,'yInt')
// Gather the values from the point sample into a list of lists.
var props = ee.List(['NDVI', 'Lai']);
var regressionVarsList = ee.List(imgSamp.reduceColumns({
reducer: ee.Reducer.toList().repeat(props.size()),
selectors: props
}).get('list'));
// Convert regression x and y variable lists to an array - used later as input
// to ui.Chart.array.values for generating a scatter plot.
var x = ee.Array(ee.List(regressionVarsList.get(0)));
var y1 = ee.Array(ee.List(regressionVarsList.get(1)));
// Apply the line function defined by the slope and y-intercept of the
// regression to the x variable list to create an array that will represent
// the regression line in the scatter plot.
var y2 = ee.Array(ee.List(regressionVarsList.get(0)).map(function f(x) {
var y = ee.Number(x).multiply(slope).add(y1);
return y;
}));
// Concatenate the y variables ( predicted y) into an array
// for input to ui.Chart.array.values for plotting a scatter plot.
var yArr = ee.Array.cat([y1,y2], 1);
// Make a scatter plot of the two bands for the point sample and include
// the least squares line of best fit through the data.
print(ui.Chart.array.values({
array: yArr,
axis: 0,
xLabels: x})
.setChartType('ScatterChart')
.setOptions({
legend: {position: 'none'},
hAxis: {'title': 'NDVI'},
vAxis: {'title': 'Lai'},
series: {
0: {
pointSize: 0.5,
dataOpacity: 0.5,
},
1: {
pointSize: 0,
lineWidth: 2,
},
}
})
);
}
计算过程中会有超时、及波段超过80mib,等问题尚待解决,目前对小区域、少随机数量点值可用