GEE代码学习 day5

Thresholding, Masking, and Remapping Images

1 Implementing a Threshold-help us partition the variability of images into categories

filterBounds:通过与几何体的相交来过滤一个集合的快捷方式。集合中的项目如果不能与给定的几何体相交,就会被排除。

gt 方法来自布尔运算符系列,即 gt 是一个函数,它在每个像素中执行测试,如果测试的计算结果为 true,则返回值 1,否则返回0,此布尔系列中的其他运算符包括小于 (lt)、小于或等于 (lte)、等于 (eq)、不等于 (neq) 和大于或等于 (gte) 等。

// Create an NDVI image using Sentinel 2. 
var seaPoint = ee.Geometry.Point(-122.2040, 47.6221); 
var seaImage = ee.ImageCollection('COPERNICUS/S2')  
               .filterBounds(seaPoint) 
               .filterDate('2020-08-15', '2020-10-01') 
               .first();  
var seaNDVI = seaImage.normalizedDifference(['B8', 'B4']);  
// And map it. 
Map.centerObject(seaPoint, 10); 
var vegPalette = ['red', 'white', 'green'];  
Map.addLayer(seaNDVI, 
            {  min: -1, max: 1, 
              palette: vegPalette }, 
              'NDVI Seattle');
// Implement a threshold. 
var seaVeg = seaNDVI.gt(0.5);  
// Map the threshold. 
Map.addLayer(seaVeg, {  
                    min: 0, 
                    max: 1, 
                    palette: ['white', 'green'] }, 
                    'Non-forest vs. Forest');

where

我们可以使用 seaNDVI.gte(−0.1).and(seaNDVI.lt(0.5)) 来识别具有中间 NDVI 级别的像素。

// Implement .where. 
// Create a starting image with all values = 1. v
var seaWhere = ee.Image(1)  
              // Use clip to constrain the size of the new image. 
              .clip(seaNDVI.geometry());  
// Make all NDVI values less than -0.1 equal 0. 
seaWhere = seaWhere.where(seaNDVI.lte(-0.1), 0);  
// Make all NDVI values greater than 0.5 equal 2. 
seaWhere = seaWhere.where(seaNDVI.gte(0.5), 2);  
// Map our layer that has been divided into three classes. 
Map.addLayer(seaWhere, 
             {  min: 0, max: 2, 
               palette: ['blue', 'white', 'green'] }, 
                'Water, Non-forest, Forest');

2 Masking Specific Values in an Image

removes specific areas of an image—those covered by the mask—from being displayed or analyzed.

// Implement masking. 
// View the seaVeg layer's current mask. 
Map.centerObject(seaPoint, 9); 
Map.addLayer(seaVeg.mask(), {}, 'seaVeg Mask');
// Create a binary mask of non-forest. 
var vegMask = seaVeg.eq(1);
// Update the seaVeg mask with the non-forest mask. 
var maskedVeg = seaVeg.updateMask(vegMask);  
// Map the updated Veg layer 
Map.addLayer(maskedVeg, 
             {  min: 0, 
                max: 1, 
                palette: ['green'] }, 
                'Masked Forest Layer');
// Map the updated mask 
Map.addLayer(maskedVeg.mask(), {}, 'maskedVeg Mask');

Remapping Values in an Image

采用图像中的特定值,并为其分配不同的值。这对于分类数据集特别有用

// Implement remapping. 
// Remap the values from the seaWhere layer. 
var seaRemap = seaWhere.remap([0, 1, 2], // Existing values. 
                          [9, 11, 10]); // Remapped values.  
Map.addLayer(seaRemap, 
            {  min: 9, 
               max: 11, 
               palette: ['blue', 'green', 'white'] }, 
              'Remapped Values');

以下是在 Google Earth Engine 中导入 Landsat 5 数据集的代码示例: ```javascript // 定义时间范围 var startDate = ee.Date('1984-01-01'); var endDate = ee.Date('2012-05-05'); // 导入Landsat 5表面反射率数据集 var collection = ee.ImageCollection('LANDSAT/LT05/C01/T1_SR') .filterDate(startDate, endDate) .filterBounds(geometry); // 选择需要的波段 var bands = ['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7']; // 选择云掩膜(可选) var maskClouds = function(image) { var qa = image.select('pixel_qa'); var cloud = qa.bitwiseAnd(1 << 5).neq(0); return image.updateMask(cloud.not()); }; // 应用云掩膜 var maskedCollection = collection.map(maskClouds); // 打印导入的图像集合信息 print(maskedCollection); // 选择特定图像进行进一步分析 var image = maskedCollection.first(); print(image); ``` 在上述代码中,我们首先定义了一个时间范围,然后使用 `ee.ImageCollection()` 函数导入 Landsat 5 表面反射率数据集。通过 `filterDate()` 方法可以筛选出指定时间范围内的图像。我们还可以使用 `filterBounds()` 方法来限定导入数据的地理范围,其中 `geometry` 是表示地理范围的几何图形。 接下来,我们选择需要的波段,可以根据自己的需求修改 `bands` 数组。如果需要进行云掩膜处理,可以定义 `maskClouds` 函数并应用于图像集合,通过 `map()` 方法应用云掩膜。 最后,我们可以打印导入的图像集合信息,并选择特定的图像进行进一步的分析。在示例中,我们选择了第一张图像进行打印。 请注意,在使用上述代码之前,需要在代码中指定感兴趣区域的几何图形,可以使用 `ee.Geometry()` 函数来定义几何图形。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值