很久很久以前,小编写了一篇基于MODIS影像构建归一化植被指数的文章,不知道大家还有没有印象,有一段时间没有更新时间序列分析相关的文章了。
今天,我们来看看基于Lansat影像,我们来看看在GEE上如何构建NDVI的时间序列。今天写起来有点手痛,小编就只能水一篇代码了。
//导入研究区域
var roi = ee.FeatureCollection('users/hesuixinya511/Province')
.filterMetadata("NAME","equals","重庆");
Map.centerObject(roi,6);
Map.addLayer(roi,{"color":"red"},"重庆");
//还原缩放
function applyScaleFactors(image) {
var opticalBands = image.select('SR_B.').multiply(0.0000275).add(-0.2);
var thermalBands = image.select('ST_B.*').multiply(0.00341802).add(149.0);
return image.addBands(opticalBands, null, true)
.addBands(thermalBands, null, true);
}
//定义去云掩膜函数
function Mask(image){
var cloudMask = (1<<3);
var cloudshadowMask = (1<<4);
var QA = image.select("QA_PIXEL");
var mask = QA.bitwiseAnd(cloudMask).eq(0).and(QA.bitwiseAnd(cloudshadowMask).eq(0));
return image.updateMask(mask);
}
//对L5/L7/L8影像进行处理
var years = ee.List.sequence(2000, 2011);
var L5_COL = ee.ImageCollection(years
.map(function(y) {
var start = ee.Date.fromYMD(y, 1, 1);
var end = start.advance(12, 'month');
var ndvi = ee.ImageCollection('LANDSAT/LT05/C02/T1_L2')
.filterDate(start, end)
.map(applyScaleFactors)
.map(Mask)
.map(function(image){
var ndvi = image.normalizedDifference(['SR_B4', 'SR_B3']).rename('NDVI');
return image.addBands(ndvi);
});
return ndvi.reduce(ee.Reducer.median()).clip(roi)
.set('Year',y);
}));
print (L5_COL);
var years = ee.List.sequence(2012, 2012);
var L7_COL = ee.ImageCollection(years
.map(function(y) {
var start = ee.Date.fromYMD(y, 1, 1);
var end = start.advance(12, 'month');
var ndvi = ee.ImageCollection('LANDSAT/LE07/C02/T1_L2')
.filterDate(start, end)
.map(applyScaleFactors)
.map(Mask)
.map(function(image){
var ndvi = image.normalizedDifference(['SR_B4', 'SR_B3']).rename('NDVI');
return image.addBands(ndvi);
});
return ndvi.reduce(ee.Reducer.median()).clip(roi)
.set('Year',y);
}));
print (L7_COL);
var years = ee.List.sequence(2013, 2023);
var L8_COL = ee.ImageCollection(years
.map(function(y) {
var start = ee.Date.fromYMD(y, 1, 1);
var end = start.advance(12, 'month');
var ndvi = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
.filterDate(start, end)
.map(applyScaleFactors)
.map(Mask)
.map(function(image){
var ndvi = image.normalizedDifference(['SR_B5', 'SR_B4']).rename('NDVI');
return image.addBands(ndvi);
});
return ndvi.reduce(ee.Reducer.median()).clip(roi)
.set('Year',y);
}));
print (L8_COL);
var data = ee.ImageCollection(L5_COL.merge(L7_COL).merge(L8_COL));
print(data);
//创建时间序列图表.
var yearlychart = ui.Chart.image.series({
imageCollection: data.select('NDVI_median'),
region: roi,
reducer: ee.Reducer.mean(),
scale: 500,
xProperty:'Year'
}).setOptions({
interpolateNulls: true,
lineWidth: 2,
title: 'NDVI Daily Time Seires',
vAxis: {title: 'NDVI', viewWindow: {max: 0.8,min: 0.4,}},
hAxis: {title: 'Date'},
trendlines: { 0: {title: 'NDVI_trend',type:'linear', showR2: true, color:'red', visibleInLegend: true}}
});
print(yearlychart);
来看看结果:
今天的分享到这里就结束了,祝大家身体健康!