// 定义研究区域和时间范围
var aoi = ee.Geometry.Rectangle([35.5, 23.0, 48.0, 32.0]); // 中东沙漠区域示例
var startYear = 2010;
var endYear = 2020;
// 1. 加载并预处理NDVI数据 (MODIS月度数据)
var ndviCollection = ee.ImageCollection('MODIS/006/MOD13A2')
.filterDate(ee.Date.fromYMD(startYear, 1, 1), ee.Date.fromYMD(endYear, 12, 31))
.filterBounds(aoi)
.select('NDVI')
.map(function(image) {
// 转换为实际NDVI值并缩放
return image.multiply(0.0001).copyProperties(image, ['system:time_start']);
});
// 2. 加载降水数据 (CHIRPS月度降水)
var precipitation = ee.ImageCollection('UCSB-CHG/CHIRPS/PENTAD')
.filterDate(ee.Date.fromYMD(startYear, 1, 1), ee.Date.fromYMD(endYear, 12, 31))
.filterBounds(aoi)
.select('precipitation')
.map(function(image) {
// 将五日数据添加年月属性
var month = image.date().get('month');
var year = image.date().get('year');
return image.set('month', month).set('year', year);
})
.filter(ee.Filter.calendarRange(1, 12, 'month'))
// 修正核心错误:将groupby改为正确的groupBy(大写B)
.groupBy(['year', 'month'], ee.Reducer.sum())
// 将分组结果转换为ImageCollection
.map(function(feature) {
var year = feature.get('year');
var month = feature.get('month');
var precipImage = ee.Image(feature.get('precipitation_sum'))
.rename('precipitation')
.set('year', year)
.set('month', month)
.set('system:time_start', ee.Date.fromYMD(year, month, 1));
return precipImage;
});
// 3. 数据匹配与整合
var combined = ee.ImageCollection.fromImages(
ndviCollection.map(function(ndvi) {
var date = ndvi.date();
var month = date.get('month');
var year = date.get('year');
// 匹配同期降水数据
var prec = precipitation
.filter(ee.Filter.eq('month', month))
.filter(ee.Filter.eq('year', year))
.first();
return ndvi.addBands(prec.select('precipitation'))
.set('system:time_start', date)
.set('year', year)
.set('month', month);
})
.filter(ee.Filter.notNull(['precipitation'])) // 过滤掉没有匹配降水数据的影像
);
// 4. 建立降水-NDVI回归模型
var regression = combined.reduce(ee.Reducer.linearRegression({
numX: 1, // 一个自变量: precipitation
numY: 1 // 一个因变量: NDVI
}));
// 获取回归系数 [截距, 降水系数]
var coefficients = regression.select('coefficients');
// 5. 计算残差NDVI
var residualCollection = combined.map(function(image) {
var precip = image.select('precipitation');
var intercept = coefficients.arrayGet([0, 0]); // 截距项
var slope = coefficients.arrayGet([1, 0]); // 降水系数
// 计算预测NDVI: NDVI_pred = intercept + slope * precipitation
var predicted = precip.multiply(slope).add(intercept);
// 计算残差: NDVI_res = NDVI_obs - NDVI_pred
return image.select('NDVI')
.subtract(predicted)
.rename('NDVI_residual')
.copyProperties(image, ['system:time_start', 'year', 'month']);
});
// 6. 结果可视化与分析
var chart = ui.Chart.image.series({
imageCollection: residualCollection,
region: aoi,
reducer: ee.Reducer.mean(),
scale: 1000
}).setOptions({
title: '沙漠植被残差NDVI时间序列 (消除降水影响)',
vAxis: {title: 'NDVI_residual'},
hAxis: {title: '日期', format: 'YYYY-MM'}
});
print(chart);
// 计算残差NDVI的年际变化趋势
var trend = residualCollection
.select('NDVI_residual')
.reduce(ee.Reducer.linearFit()); // 斜率表示变化趋势
Map.addLayer(
trend.select('scale'),
{min: -0.005, max: 0.005, palette: ['red', 'white', 'green']},
'残差NDVI年际变化趋势'
);
// 添加地图显示
Map.centerObject(aoi, 6);
Map.addLayer(
residualCollection.mean(),
{min: -0.2, max: 0.2, palette: ['red', 'white', 'green']},
'平均残差NDVI'
);
// 导出结果用于进一步分析
Export.image.toDrive({
image: residualCollection.mean(),
description: 'NDVI_residual_mean',
region: aoi,
scale: 1000,
maxPixels: 1e13
});
GEE中显示Line 17:
ee. ImageCollection(...).filterDate(...).filterBounds(...).select(...).map(...). filter(...). groupby
is not a function。请做尽可能小的修改不要改变源代码的功能同时检查代码看是否有同样的错误一并进行修改后生成完整版代码。代码的目的是通过GEE平台利用已有原始ndvi获取真实降水数据计算残差NDVI,来消除沙漠植被动态变化识别受降水因素的影响
最新发布