代码如下:
function createNDVI(image) {
var ndvi = image.normalizedDifference(['B5', 'B4']).rename('NDVI');
return image.addBands(ndvi);
}
代码如下(以南京市为例):
var geometry = ee.FeatureCollection('users/www1573979951/nanjingshi')
Map.centerObject(geometry,6)
var colorizedVis = {
min: -0.8,
max: 0.8,
palette: ['blue', 'white', 'green'],
};
//cloud mask 去云的方法
function maskL8sr(image) {
// Bits 3 and 5 are cloud shadow and cloud, respectively.
var cloudShadowBitMask = (1 << 3);
var cloudsBitMask = (1 << 5);
// Get the pixel QA band.
var qa = image.select('pixel_qa');
// Both flags should be set to zero, indicating clear conditions.
var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0).and(qa.bitwiseAnd(cloudsBitMask).eq(0));
return image.updateMask(mask);
}
//NDVI的计算
function createNDVI(image) {
var ndvi = image.normalizedDifference(['B5', 'B4']).rename('NDVI');
return image.addBands(ndvi);
}
var col = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
.map(maskL8sr)
.filterDate('2020-01-01','2020-12-31')
.filterBounds(geometry)
.map(createNDVI)
.select('NDVI');
Map.addLayer(col.mean().clip(geometry), colorizedVis, 'col');
print(col);
print(ui.Chart.image.series(col, geometry, ee.Reducer.mean(), 500));
影像截图:
影像集截图(44个数据):
表格截图:
CSV数据截图:
仅仅看时间序列,发现有很多异常值。所以需要观察数据的变化趋势。此代码加入了趋势线的相关系数R2进行分析.
代码如下(以南京市为例):
var geometry = ee.FeatureCollection('users/www1573979951/nanjingshi')
//cloud mask 去云的方法
function maskL8sr(image) {
// Bits 3 and 5 are cloud shadow and cloud, respectively.
var cloudShadowBitMask = (1 << 3);
var cloudsBitMask = (1 << 5);
// Get the pixel QA band.
var qa = image.select('pixel_qa');
// Both flags should be set to zero, indicating clear conditions.
var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0).and(qa.bitwiseAnd(cloudsBitMask).eq(0));
return image.updateMask(mask);
}
//NDVI的计算
function createNDVI(image) {
var ndvi = image.normalizedDifference(['B5', 'B4']).rename('NDVI');
return image.addBands(ndvi);
}
var col = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
.map(maskL8sr)
.filterDate('2020-01-01','2020-12-31')
.filterBounds(geometry)
.map(createNDVI)
.select('NDVI');
var landsat8trendline = Chart.image.series(col, geometry, ee.Reducer.mean(), 500);
landsat8trendline = landsat8trendline
.setOptions({
title: 'Landsat 8 SR NDVI',
hAxis: {title: 'Date', gridlines: {count: 10}},
vAxis: {title: 'NDVI',viewWindowMode: 'explicit', viewWindow: {max: 1,min: -0.25,},gridlines: {count: 5,}},
interpolateNulls: true,
lineWidth: 1,
pointSize: 1,
trendlines: { 0: {title: 'NDVI_trend',type:'linear', showR2: true, color:'red', visibleInLegend: true}}
});
print(landsat8trendline);
接着进行进行月平均分析。
代码如下(以南京市为例):
var geometry = ee.FeatureCollection('users/www1573979951/nanjingshi')
//cloud mask 去云的方法
function maskL8sr(image) {
// Bits 3 and 5 are cloud shadow and cloud, respectively.
var cloudShadowBitMask = (1 << 3);
var cloudsBitMask = (1 << 5);
// Get the pixel QA band.
var qa = image.select('pixel_qa');
// Both flags should be set to zero, indicating clear conditions.
var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0).and(qa.bitwiseAnd(cloudsBitMask).eq(0));
return image.updateMask(mask);
}
//NDVI的计算
function createNDVI(image) {
var ndvi = image.normalizedDifference(['B5', 'B4']).rename('NDVI');
return image.addBands(ndvi);
}
var col = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
.map(maskL8sr)
.filterDate('2020-01-01','2020-12-31')
.filterBounds(geometry)
.map(createNDVI)
.select('NDVI');
var years = ee.List.sequence(2020, 2020);
var months = ee.List.sequence(1, 12);
var landsat8monthlymeanNDVI = ee.ImageCollection.fromImages(
years.map(function (y) {
return months.map(function(m) {
return 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()
);
var monthlymeantrendline = Chart.image.series(landsat8monthlymeanNDVI, geometry, ee.Reducer.mean(), 500);
monthlymeantrendline = monthlymeantrendline
.setOptions({
title: 'Landsat 8 SR NDVI',
hAxis: {title: 'Date', gridlines: {count: 10}},
vAxis: {title: 'NDVI',viewWindowMode: 'explicit', viewWindow: {max: 1,min: -0.25,},gridlines: {count: 5,}},
interpolateNulls: true,
lineWidth: 1,
pointSize: 1,
trendlines: { 0: {title: 'NDVI_trend',type:'linear', showR2: true, color:'red', visibleInLegend: true}}
});
print(monthlymeantrendline)