统计掩膜后数据集像元数量统计,区域面积、数量统计,在GEE线上的Code Editor编写JavaScript代码实现相关功能:传入范围,筛选出符合条件的数据,预处理之后计算
原始影像:定义示例矢量区域geometry(山东省潍坊市峡山水库周边),时间范围(为凸显去云效果,选择多雨季节,云量较大),筛选出符合条件的Landsat8影像集dataset并借助函数maskL8sr去掉质量不合适的像元
var geometry =
ee.Geometry.Polygon(
[[[119.3140376290338, 36.559328749628065],
[119.3140376290338, 36.263933411986294],
[119.62234146204162, 36.263933411986294],
[119.62234146204162, 36.559328749628065]]], null, false);
// QA_PIXEL band (CFMask) to mask unwanted pixels.
function maskL8sr(image) {
// Bit 0 - Fill
// Bit 1 - Dilated Cloud
// Bit 2 - Cirrus
// Bit 3 - Cloud
// Bit 4 - Cloud Shadow
var qaMask = image.select('QA_PIXEL').bitwiseAnd(parseInt('11111', 2)).eq(0);
var saturationMask = image.select('QA_RADSAT').eq(0);
// Apply the scaling factors to the appropriate bands.
var opticalBands = image.select('SR_B.').multiply(0.0000275).add(-0.2);
var thermalBands = image.select('ST_B.*').multiply(0.00341802).add(149.0);
// Replace the original bands with the scaled ones and apply the masks.
return image.addBands(opticalBands, null, true)
.addBands(thermalBands, null, true)
.updateMask(qaMask)
.updateMask(saturationMask);
}
var dataset = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
.filterDate('2021-6-01', '2021-7-01')
.filterBounds(geometry)
// .filter(ee.Filter.lt('CLOUD_COVER', 20))
.map(maskL8sr)
.select(['SR_B4', 'SR_B3', 'SR_B2']);
数据集的影像数量为num,计算每个像元位置有效值的数目(未被上一步maskL8sr函数掩膜掉);进行可视化,定义调色板(范围为0-影像数量,颜色越深值越大)
//数据集有效像元数量
var num = dataset.size().getInfo()
print("数据集图像数量", num)
var valueCount = dataset.select(0).count().clip(geometry)
var visParams1 = { min: 0, max: num, palette: ['FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163'] };
Map.addLayer(valueCount, visParams1, "数据集有效像元数量");
有效像元数量图:
以单幅影像为实例,将数据集dataset中值合成,保留RGB三波段,真彩色显示;
//获取图像 中值合成
var l8_image = dataset.median().clip(geometry);
//可视化
var visualization = {
bands: ['SR_B4', 'SR_B3', 'SR_B2'],
min: 0.0,
max: 0.3,
};
Map.centerObject(geometry);
Map.addLayer(l8_image, visualization, 'True Color (432)',false);
真彩色(432) :去掉质量不佳像元影像有部分缺失
借助gee的ee.Image.pixelArea()生成一幅图像,其中每个像素的值为该像素的面积(以平方米为单位)。返回的图像有一个单独的波段,称为“area”。
reduceRegion函数对特定区域的所有像素应用ee.Reducer.sum()求和计算,分别得到掩膜前后区域内的面积,求得面积比例
//面积比例
// Map.addLayer(ee.Image(1).updateMask(l8_image.select(0).mask()),"","掩膜")
var allArea = ee.Image.pixelArea().reduceRegion({
reducer: ee.Reducer.sum(),
geometry: geometry,
scale: 30,
maxPixels: 10e15,
}).get("area");
print("allArea m2", allArea)
var realArea = ee.Image.pixelArea().updateMask(l8_image.select(0).mask()).reduceRegion({
reducer: ee.Reducer.sum(),
geometry: geometry,
scale: 30,
maxPixels: 10e15,
}).get("area");
print("realArea m2", realArea)
print("面积比例", ee.Number(realArea).divide(allArea))
reduceRegion函数对特定区域的所有像素应用ee.Reducer.count()求数量计算,分别得到掩膜前后区域内像元数量,求得数量比例
var allCount = ee.Image.pixelArea().reduceRegion({
reducer: ee.Reducer.count(),
geometry: geometry,
scale: 30,
maxPixels: 10e15,
}).get("area");
print("allCount", allCount)
var realCount = ee.Image.pixelArea().updateMask(l8_image.select(0).mask()).reduceRegion({
reducer: ee.Reducer.count(),
geometry: geometry,
scale: 30,
maxPixels: 10e15,
}).get("area");
print("realCount", realCount)
print("像元比例", ee.Number(realArea).divide(allArea))