function maskL8sr(image) { // The third bit is cloud and the fourth bit is cloudshadow。 var cloudShadowBitMask = 1 << 4; var cloudsBitMask = 1 << 3; // Get QA_PIXEL band. var qa = image.select('QA_PIXEL'); // Set the cloud mask to zero var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0) .and(qa.bitwiseAnd(cloudsBitMask).eq(0)); // Update the cloudmask return image.updateMask(mask) .select("SR_B.*") .copyProperties(image, ["system:time_start"]); } // Map the function over 3 months of data and take the median. // Load Landsat-8 surface reflectance data. var landsat8 = ee.ImageCollection("LANDSAT/LC08/C02/T1_L2") .filterBounds(table) .filterDate('2019-1-01','2020-1-1') // Pre-filter to get less cloudy granules. .filter(ee.Filter.lte('CLOUD_COVER',5)) .map(maskL8sr) .median() .clip(table); landsat8 = landsat8.uint16(); print(landsat8);// visualize the datasets var rgbVis = { min: 8400, max: 15000, gamma:1.5, bands: ['SR_B4', 'SR_B3', 'SR_B2'], }; Map.addLayer(landsat8,rgbVis,'landsat8'); var imgselect = landsat8.select('SR_B2','SR_B3','SR_B4','SR_B5'); //Export image to google drive and then download Export.image.toDrive({ image:imgselect, description:'2019yaogan', scale:30, region:table, folder:"result", crs: "EPSG:4326", maxPixels: 1e13 });将这一段利用Landsat的代码改写成使用哨兵2
时间: 2024-03-08 16:45:42 浏览: 147
Sure here's the modified code to use Sentinel-2 instead of Landsat:
```
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 the masked image, scaled to reflectance, without the QA bands.
return image.updateMask(mask)
.select('B.*')
.multiply(0.0001)
.copyProperties(image, ['system:time_start']);
}
// Load Sentinel-2 surface reflectance data.
var sentinel2 = ee.ImageCollection('COPERNICUS/S2_SR')
.filterBounds(table)
.filterDate('2019-01-01', '2020-01-01')
.map(maskS2clouds)
.median()
.clip(table);
sentinel2 = sentinel2.uint16();
print(sentinel2);
// Visualize the datasets
var rgbVis = {
min: 0.0,
max: 3000.0,
gamma: 1.5,
bands: ['B4', 'B3', 'B2'],
};
Map.addLayer(sentinel2, rgbVis, 'Sentinel-2');
var imgselect = sentinel2.select('B2', 'B3', 'B4', 'B8');
// Export image to Google Drive
Export.image.toDrive({
image: imgselect,
description: '2019yaogan',
scale: 10,
region: table,
folder: 'result',
crs: 'EPSG:4326',
maxPixels: 1e13
});
```
Note that Sentinel-2's surface reflectance data has a different band naming convention and scale factor compared to Landsat, so the code has been modified accordingly.
阅读全文