具体代码:
//去云函数
function maskS2clouds(image) {
var qa = image.select('QA60');
// Bits 10 and 11 are clouds and cirrus, respectively.
var cloudBitMask = 1 << 10;
var cirrusBitMask = 1 << 11;
// Both flags should be set to zero, indicating clear conditions.
var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
.and(qa.bitwiseAnd(cirrusBitMask).eq(0));
return image.updateMask(mask)
//.divide(10000);
}
//NDVI函数
function NDVI(img) {
var ndvi = img.expression(
"(NIR-R)/(NIR+R)", {
"R": img.select("B4"),
"NIR": img.select("B8"),
});
// 将NDVI小于0.3的区域掩盖
var ndviMasked = ndvi.updateMask(ndvi.gte(0.22));
return img.addBands(ndviMasked.rename("NDVI")).clip(cities);
}
//日期范围
var startDate = ee.Date('2019-11-01');
var endDate = ee.Date('2020-05-31');
//数据集
var dataset_s2 = ee.ImageCollection('COPERNICUS/S2_SR_HARMONIZED')
.filterBounds(cities)
.filter(ee.Filter.date(startDate, endDate))
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 10))
//曲云,计算NDVI并进行波段选择
var dataset_s2_qy=dataset_s2.map(maskS2clouds).map(NDVI);
var s2_ndvi=dataset_s2_qy.select('NDVI')
//时序折线图绘制
var s2_chart=ui.Chart.image.series({
imageCollection: s2_ndvi,
region: region1,//要绘制的区域
reducer: ee.Reducer.mean(),
scale: 500
}).setOptions({
interpolateNulls: true,
lineWidth: 2,//线宽
title: 's2_NDVI',//图标题
vAxis: {title: 'NDVI'},
hAxis: {title: 'Date'},
series: {0: {color: 'red'}}, // Color
trendlines: { 0: {title: 'NDVI_trend',type:'linear', showR2: true, color:'red', visibleInLegend: true}} //添加趋势线
});
print(s2_chart);
结果图如下: