Sentinel-2:中文名哨兵2号卫星。
NDVI的计算公式:NDVI = (近红外波段 - 红波段) / (近红外波段 + 红波段)
针对每种卫星的波段,选用的波段都有所不同,公式如下:
代码如下(以鹿邑县为例)
//以鹿邑县为例子
var geometry = ee.FeatureCollection('users/www1573979951/luyixian')
Map.centerObject(geometry,7)
//颜色设置
var colorizedVis = {
min: -0.8,
max: 0.8,
palette: ['blue', 'white', 'green'],
};
//使用QA波段去云
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).set(image.toDictionary(image.propertyNames()));
}
//NDVI的计算公式
function createNDVI(image){
var ndvi = image.normalizedDifference(["B8","B4"]).rename('NDVI');
return image.addBands(ndvi);
}
//特别注意的是,在数学变换之后,保持原始影像的属性,所以这里.set(image.toDictionary(image.propertyNames()));
var S2_COL = ee.ImageCollection("COPERNICUS/S2")
.filterDate("2020-01-01", "2020-12-31")
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',20))
.filterBounds(geometry)
.map(maskS2clouds)
.map(createNDVI)
.select('NDVI');
print(S2_COL)
Map.addLayer(S2_COL.mean().clip(geometry), colorizedVis, 'col');
//趋势线代码
var S2_chart = ui.Chart.image.series({
imageCollection: S2_COL.select('NDVI'),
region: geometry,
reducer: ee.Reducer.mean(),
scale: 500
}).setOptions({
interpolateNulls: true,
lineWidth: 2,
title: 'NDVI Time Seires',
vAxis: {title: 'NDVI'},
hAxis: {title: 'Date'},
trendlines: { 0: {title: 'NDVI_trend',type:'linear', showR2: true, color:'red', visibleInLegend: true}}
});
print(S2_chart);
遥感影像截图:
数据集截图(51个数据集):
表格数据以及变化趋势截图:
CSV数据:
代码如下(以鹿邑县为例):
//以鹿邑县为例子
var geometry = ee.FeatureCollection('users/www1573979951/luyixian')
Map.centerObject(geometry,7)
//颜色设置
var colorizedVis = {
min: -0.8,
max: 0.8,
palette: ['blue', 'white', 'green'],
};
//使用QA波段去云
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).set(image.toDictionary(image.propertyNames()));
}
//NDVI的计算公式
function createNDVI(image){
var ndvi = image.normalizedDifference(["B8","B4"]).rename('NDVI');
return image.addBands(ndvi);
}
//特别注意的是,在数学变换之后,保持原始影像的属性,所以这里.set(image.toDictionary(image.propertyNames()));
var S2_COL = ee.ImageCollection("COPERNICUS/S2")
.filterDate("2020-01-01", "2020-12-31")
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',20))
.filterBounds(geometry)
.map(maskS2clouds)
.map(createNDVI)
.select('NDVI');
print(S2_COL)
Map.addLayer(S2_COL.median().clip(geometry), colorizedVis, 'col');
var years = ee.List.sequence(2020, 2020);
var months = ee.List.sequence(1, 12);
var S2_monthlymeanNDVI = ee.ImageCollection.fromImages(
years.map(function (y) {
return months.map(function(m) {
return S2_COL.filter(ee.Filter.calendarRange(y,y, 'year')).filter(ee.Filter.calendarRange(m, m, 'month')).mean().set('year', y).set('month', m).set('system:time_start', ee.Date.fromYMD(y, m, 1));
});
}).flatten()
);
// Create a monthly time series chart.
var plotNDVI = ui.Chart.image.seriesByRegion(S2_monthlymeanNDVI, geometry,ee.Reducer.mean(),
'NDVI',500,'system:time_start')
.setChartType('LineChart').setOptions({
interpolateNulls: true,
title: 'NDVI Monthly time series',
hAxis: {title: 'Date'},
vAxis: {title: 'NDVI',viewWindowMode: 'explicit', viewWindow: {max: 0.7,min: 0.3,},gridlines: {count: 10,}},
trendlines: { 0: {title: 'NDVI_trend',type:'linear', showR2: true, color:'red', visibleInLegend: true}}});
// Display.
print(plotNDVI);
影像截图:
表格变化趋势图:
CSV数据截图: