美文网首页
EZ | Relational, conditional and

EZ | Relational, conditional and

作者: 杜若飞er | 来源:发表于2019-07-30 19:37 被阅读0次

Relational,\ conditional\ and\ Boolean\ operations


关系运算、条件运算、布尔运算

为了在两个image之间执行逐个像素的比较,使用了关系运算符。为了提取图像中的城市化区域,实例代码中使用了关系运算对光谱索引建立了阀值,并使之与与and()结合:

// Load a Landsat 8 image.
var image = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318');

// Create NDVI and NDWI spectral indices.
var ndvi = image.normalizedDifference(['B5', 'B4']);
var ndwi = image.normalizedDifference(['B3', 'B5']);

// Create a binary layer using logical operations.
var bare = ndvi.lt(0.2).and(ndwi.lt(0));

// Mask and display the binary layer.
Map.setCenter(-122.3578, 37.7726, 12);
Map.setOptions('satellite');
Map.addLayer(bare.updateMask(bare), {}, 'bare');

关系运算和布尔运算对输出只有true(1)和false(0)两种,想要为所有的0值设置蒙版,可以使用自己的蒙版生成的二进制image,上例显示结果如下:


Fig.1 低NDVI和低NDWI做and运算

关系运算和布尔运算返回的二进制image可以和数学运算一起使用,此例中使用关系运算符和image.add()在夜间灯光图中创建城市化的区域:

// Load a 2012 nightlights image.
var nl2012 = ee.Image('NOAA/DMSP-OLS/NIGHTTIME_LIGHTS/F182012');
var lights = nl2012.select('stable_lights');

// Define arbitrary thresholds on the 6-bit stable lights band.
var zones = lights.gt(30).add(lights.gt(55)).add(lights.gt(62));

// Display the thresholded image as three distinct zones near Paris.
var palette = ['000000', '0000FF', '00FF00', 'FF0000'];
Map.setCenter(2.373, 48.8683, 8);
Map.addLayer(zones, {min: 0, max: 3, palette: palette}, 'development zones');

前例中的代码等效于使用expression()实现的三目运算:

// Create zones using an expression, display.
var zonesExp = nl2012.expression(
    "(b('stable_lights') > 62) ? 3" +
      ": (b('stable_lights') > 55) ? 2" +
        ": (b('stable_lights') > 30) ? 1" +
          ": 0"
);
Map.addLayer(zonesExp,
             {min: 0, max: 3, palette: palette},
             'development zones (ternary)');

请注意,前面的表达式中,使用了b()函数选择出感兴趣的波段,没有使用变量名称的字典。显示结果如下:

法国,巴黎,夜景,2012年

在图像上实现条件运算的另一种方式是使用image.where(),考虑到需要一些其他数据对蒙版蒙掉的东西进行替换,我们在下面的例子中使用了where()使用无云图像替换掉了阴影的像素:

// Load a cloudy Landsat 8 image.
var image = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20130603');
Map.addLayer(image,
             {bands: ['B5', 'B4', 'B3'], min: 0, max: 0.5},
             'original image');

// Load another image to replace the cloudy pixels.
var replacement = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20130416');

// Compute a cloud score band.
var cloud = ee.Algorithms.Landsat.simpleCloudScore(image).select('cloud');

// Set cloudy pixels to the other image.
var replaced = image.where(cloud.gt(10), replacement);

// Display the result.
Map.centerObject(image, 9);
Map.addLayer(replaced,
             {bands: ['B5', 'B4', 'B3'], min: 0, max: 0.5},
             'clouds replaced');

在本例中,观察simpleCloudScore()函数,它按照1-100的划分精度水平对像素进行排序,其中100表示云很多,LandSat算法页面上有关于此函数的更多信息。


\tag{此页面最后更新于2019年3月18日}

相关文章

网友评论

      本文标题:EZ | Relational, conditional and

      本文链接:https://www.haomeiwen.com/subject/vororctx.html