最近,在处理遥感数据时,我意识到还没有对不同研究区内的波段值进行系统的提取。考虑到这一步骤在很多遥感分析任务中的重要性,我特地整理了一套提取波段值的方法,并将相关内容分享给大家。本文将聚焦于如何利用遥感影像高效提取不同研究区域的波段值,为后续的分析打下基础。
这里以北京市和天津市的区县区域为例,我选择了2024年3月1日-10月1日之间的sentinel-2和Landsat8的指定波段的数据,并对数据进行均值合成,如sentinel-2选择11个波段(如B2、B3、B4等)进行分析以及Landsat8的多个波段(如SR_B1、SR_B2、SR_B3等)。
不多说了,直接上代码! 👇
// 定义研究区
var aoi = table2;
print("AOI (Area of Interest):", aoi);
// Sentinel-2云掩膜函数
function maskS2clouds(image) {
var qa = image.select('QA60');
var cloudBitMask = 1 << 10; // 云掩膜
var cirrusBitMask = 1 << 11; // 卷云掩膜
var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
.and(qa.bitwiseAnd(cirrusBitMask).eq(0));
return image.updateMask(mask).divide(10000);
}
// Landsat-8数据预处理函数
function applyScaleFactors(image) {
var opticalBands = image.select('SR_B.').multiply(0.0000275).add(-0.2);
var thermalBands = image.select('ST_B.*').multiply(0.00341802).add(149.0);
return image.addBands(opticalBands, null, true)
.addBands(thermalBands, null, true);
}
// Landsat-8云掩膜函数
function rmCloudNew(image) {
var cloudShadowBitMask = (1 << 3);
var cloudsBitMask = (1 << 5);
var qa = image.select('QA_PIXEL');
var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0)
.and(qa.bitwiseAnd(cloudsBitMask).eq(0));
return image.updateMask(mask)
.copyProperties(image, ["system:time_start"]);
}
// 加载并预处理Sentinel-2数据
var sentinel2 = ee.ImageCollection('COPERNICUS/S2_HARMONIZED')
.filterDate('2024-03-01', '2024-10-01')
.filterBounds(aoi)
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 5))
.map(maskS2clouds)
.select(["B2", "B3", "B4", "B5", "B6", "B7", "B8", "B8A", "B11", "B12"]);
print("Sentinel-2 Image Collection:", sentinel2);
// 加载并预处理Landsat-8数据
var landsat8 = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
.filterDate('2024-03-01', '2024-10-01')
.filterBounds(aoi)
.map(applyScaleFactors)
.map(rmCloudNew)
.select(["SR_B2", "SR_B3", "SR_B4", "SR_B5", "SR_B6", "SR_B7", "ST_B10", "SR_QA_AEROSOL"]);
print("Filtered Landsat 8 Collection:", landsat8);
var s2VisParams = {
min: 0.0,
max: 0.3,
bands: ['B4', 'B3', 'B2']
};
var l8VisParams = {
bands: ['SR_B4', 'SR_B3', 'SR_B2'],
min: 0.0,
max: 0.3
};
Map.addLayer(sentinel2.mean().clip(aoi), s2VisParams, 'Sentinel-2 RGB');
Map.addLayer(landsat8.mean().clip(aoi), l8VisParams, 'Landsat-8 RGB');
Map.addLayer(aoi.style({ fillColor: '00000000', color: 'blue' }), {}, "AOI Boundary");
Map.centerObject(aoi, 7.5);
// 计算多波段均值图像
var meanS2Image = sentinel2.mean().clip(aoi);
var meanL8Image = landsat8.mean().clip(aoi);
// 提取属性范围
var attributes = aoi.aggregate_array("PAC").distinct();
print("Attributes:", attributes);
// 提取每个属性范围的波段值(Sentinel-2)
var s2Results = attributes.map(function(attribute) {
var subset = aoi.filter(ee.Filter.eq("PAC", attribute));
var meanValues = meanS2Image.reduceRegion({
reducer: ee.Reducer.mean(),
geometry: subset.geometry(),
scale: 10,
maxPixels: 1e13
});
return ee.Feature(null, meanValues).set("PAC", attribute);
});
// 提取每个属性范围的波段值(Landsat-8)
var l8Results = attributes.map(function(attribute) {
var subset = aoi.filter(ee.Filter.eq("PAC", attribute));
var meanValues = meanL8Image.reduceRegion({
reducer: ee.Reducer.mean(),
geometry: subset.geometry(),
scale: 30,
maxPixels: 1e13
});
return ee.Feature(null, meanValues).set("PAC", attribute);
});
// 将结果转为FeatureCollection
var s2FeatureCollection = ee.FeatureCollection(s2Results);
var l8FeatureCollection = ee.FeatureCollection(l8Results);
print("Sentinel-2 Feature Collection:", s2FeatureCollection);
print("Landsat-8 Feature Collection:", l8FeatureCollection);
// 导出结果为CSV文件
Export.table.toDrive({
collection: s2FeatureCollection,
description: 'Sentinel2_BandValues_ByAttribute',
fileFormat: 'CSV'
});
Export.table.toDrive({
collection: l8FeatureCollection,
description: 'Landsat8_BandValues_ByAttribute',
fileFormat: 'CSV'
});
结果展示:
代码链接:
https://code.earthengine.google.com/d3590a3b3069925f7a3353d6207d866d