// Load Sentinel-2 TOA reflectance data. var sentinel = ee.ImageCollection('COPERNICUS/S2') .filterDate('2019-01-01', '2019-12-31') .filterBounds(table) .map(function(image) { var cloud_mask = ee.Image(0).where( image.select('QA60').bitwiseAnd(1<<10), 1).rename('cloud_mask'); var cloud_probability = image.select('QA60').bitwiseAnd(1024).rightShift(10).rename('cloud_probability'); var cloud_shadow_probability = image.select('QA60').bitwiseAnd(2048).rightShift(11).rename('cloud_shadow_probability'); var cloud_mask_combined = cloud_mask.or(cloud_probability.gt(20)).or(cloud_shadow_probability.gt(20)); return image.addBands(cloud_mask_combined); }) .map(function(image) { return image.clip(table); }); // Function to mask clouds using the Sentinel-2 cloud mask. var maskClouds = function(image) { var cloudMask = image.select('cloud_mask').not(); return image.updateMask(cloudMask); }; // Function to calculate the NDVI. var calculateNDVI = function(image) { var ndvi = image.normalizedDifference(['B8', 'B4']).rename('ndvi'); return image.addBands(ndvi); }; // Function to calculate the EVI. var calculateEVI = function(image) { var evi = image.expression( '2.5 * (nir - red) / (nir + 6 * red - 7.5 * blue + 1)', { 'nir': image.select('B8'), 'red': image.select('B4'), 'blue': image.select('B2') }).rename('evi'); return image.addBands(evi); }; // Apply the cloud mask, calculate the NDVI and EVI, and combine the bands. var sentinel_ndvi_evi = sentinel .map(maskClouds) .map(calculateNDVI) .map(calculateEVI) .select(['B2', 'B3', 'B4', 'B8', 'ndvi', 'evi']); // Function to filter images based on the quality of the NDVI and EVI. var filterQuality = function(image) { var ndvi_quality = image.select('ndvi').qualityMosaic('ndvi').gte(0.6); var evi_quality = image.select('evi').qualityMosaic('evi').gte(0.6); return image.updateMask(ndvi_quality.and(evi_quality)); }; // Filter the images based on the quality of the NDVI and EVI. var sentinel_filtered = sentinel_ndvi_evi.filter(filterQuality); // Create a median composite of the filtered images and display it. var sentinel_median = sentinel_filtered.median(); Map.addLayer(sentinel_median, {bands: ['B4', 'B3', 'B2'], min: 0, max: 0.3}, 'Sentinel-2 Median Composite');
时间: 2023-12-25 17:03:57 浏览: 143
这段代码使用 Google Earth Engine 平台加载 Sentinel-2 的 TOA 反射率数据,并对其进行云掩膜、NDVI、EVI 计算,最后使用质量筛选函数过滤图像,并生成中值合成图像。其中,变量 `sentinel` 是 Sentinel-2 图像集合,通过 `filterDate()`、`filterBounds()` 进行时间和空间筛选;`map()` 函数内的代码则是对每个图像进行云掩膜、NDVI、EVI 计算;`maskClouds()`、`calculateNDVI()`、`calculateEVI()` 分别是云掩膜、NDVI、EVI 计算的函数;`sentinel_ndvi_evi` 变量是应用了云掩膜、NDVI、EVI 计算的图像集合,并选择了所需的波段;`filterQuality()` 是质量筛选函数,根据 NDVI 和 EVI 的质量指标进行筛选;`sentinel_filtered` 是经过质量筛选后的图像集合;`sentinel_median` 是经过中值合成的结果,用于可视化。
相关问题
改进代码:var sentinel1 = ee.ImageCollection('COPERNICUS/S1_GRD') .filterBounds(aoi) .filterDate('2020-01-01','2020-12-31') .filterMetadata('instrumentMode', 'equals', 'IW'); var sentinel1_vv_vh = sentinel1.select(['VV', 'VH']).median(); // 获取图像中所有波段的名称 var ratio = sentinel1_vv_vh.select('VV').divide(sentinel1_vv_vh.select('VH'));
你的代码已经很不错了,但如果你想要更好的可读性和可维护性,可以考虑使用变量名来描述你的数据和操作,以及将代码分解为更小的部分。
以下是一个可能的改进版本:
```javascript
// 定义感兴趣区域
var areaOfInterest = ee.Geometry.Rectangle([xmin, ymin, xmax, ymax]);
// 定义 Sentinel-1 数据集
var sentinel1 = ee.ImageCollection('COPERNICUS/S1_GRD')
.filterBounds(areaOfInterest)
.filterDate('2020-01-01', '2020-12-31')
.filterMetadata('instrumentMode', 'equals', 'IW');
// 获取 VV 和 VH 波段的中位数
var vvVhMedian = sentinel1.select(['VV', 'VH']).median();
// 计算 VV/VH 比值
var vvOverVh = vvVhMedian.select('VV').divide(vvVhMedian.select('VH'));
```
这个版本使用了更具描述性的变量名,例如 `areaOfInterest` 和 `vvOverVh`,使代码更加易读和易于理解。它还将代码分解为三个步骤,每个步骤都有自己的注释,以帮助读者理解代码的目的和意图。
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
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.
阅读全文