例如,有时候在计算NDVI后,需要统计某个区域的NDVI最大值、平均值、最小值等,这里以统计NDVI最大值为例。
目录
1、筛选影像
var roi = table;
//去云
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);
}
var dataset = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
.filterDate('2016-01-01', '2016-12-31')
.map(maskL8sr)
.median()
.clip(roi);
var visParams = {
bands: ['B4', 'B3', 'B2'],
min: 0,
max: 3000,
gamma: 1.4,
};
Map.centerObject(roi,7)
Map.addLayer(dataset, visParams);
2、计算NDVI
var l8_ndvi = dataset.normalizedDifference(['B5', 'B4']).rename("L8_NDVI");
3、统计区域内的NDVI最大值
var ndvi_max = l8_ndvi.reduceRegion({
reducer: ee.Reducer.max(),
scale: 30,
geometry: roi,
maxPixels: 1e13,
tileScale:16
})
print("ndvi_max",ndvi_max)
将代码 ee.Reducer.max()进行更改,可统计其他特征值。
ee.Reducer.min():统计区域内的最小值
ee.Reducer.mean():统计区域内的平均值
4、完整代码
var roi = table;
//去云
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);
}
var dataset = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
.filterDate('2016-01-01', '2016-12-31')
.map(maskL8sr)
.median()
.clip(roi);
var visParams = {
bands: ['B4', 'B3', 'B2'],
min: 0,
max: 3000,
gamma: 1.4,
};
Map.centerObject(roi,7)
Map.addLayer(dataset, visParams);
var l8_ndvi = dataset.normalizedDifference(['B5', 'B4']).rename("L8_NDVI");
var ndvi_max = l8_ndvi.reduceRegion({
reducer: ee.Reducer.max(),
scale: 30,
geometry: roi,
maxPixels: 1e13,
tileScale:16
})
print("ndvi_max",ndvi_max)
学习更多GEE教程可关注GZH:"GEEer成长日记"。