GEE 9:Earth Engine Reducers 的基本操作

news2024/10/5 15:23:33

目录

    • 1.Image 、ImageCollection and Regions Reducers(图层和区域的相关操作)
      • 1.1 Image Reductions(处理单个图层)
      • 1.2 ImageCollection Reductions(处理图层集)
      • 1.3 Greenest pixel (maximum NDVI) composite(理解去云操作)
      • 1.4 ImageCollection reductions(注意事项)
      • 1.5 Image Region reductions
      • 1.6 Image Region reductions, cont.
      • 1.7 Image RegionS reductions
    • 2. Image Neighborhood reductions (convolution)(卷积Kernel)
    • 3.FeatureCollection column reductions(属性运算)

1.Image 、ImageCollection and Regions Reducers(图层和区域的相关操作)

Reducers are the way to aggregate data over time, space, bands, arrays and other data structures in Earth Engine.

Reducers 是在 Earth Engine 中聚合时间、空间、波段、数组和其他数据结构的数据的方式。 ee.Reducer 类指定数据的聚合方式。此类中的Reducer可以指定用于聚合的简单统计数据(最小值、最大值、平均值、中值、标准差等),或输入数据的更复杂的组合(例如直方图、线性回归、列表)。时间处理器:imageCollection.reduce();空间 处理器:image.reduceRegion()、image.reduceNeighborhood();波段处理器:image.reduce(); FeatureCollection 的属性空间 :featureCollection.reduceColumns()、以 aggregate_ 开头的 FeatureCollection 方法

顾名思义,Reducer翻译为减少器,即将多个波段图像减少到一个波段图像的方法。

Reducers take an input dataset and produce a single output.

ee.Reducer()指定数据的聚合方式。

在这里插入图片描述

1.1 Image Reductions(处理单个图层)

使用 image.reduce() 对一张图像进行简化,并且会对该图像的所有波段进行处理,输出图像只有一个波段,比如ee.Reducer.max(),但是ee.Reducer.minMax()有两个波段输出。

var max = image.reduce(ee.Reducer.max());

在这里插入图片描述

// Reducers: Image.reduce()
var image = l8sr.first()
var max = image.reduce(ee.Reducer.max());

Map.centerObject(image, 11);
Map.addLayer(max, {min: 1000, max: 50000}, 'max');

在这里插入图片描述

结果显示:
在这里插入图片描述

1.2 ImageCollection Reductions(处理图层集)

在这里插入图片描述

ImageCollection表示的图像时间序列取中值,输出中的每个像素由每个波段在该位置的集合中所有像素的中值组成。按照逐像素和逐波段进行计算(pixel-wise、band-wise)。

var median = imageCollection.reduce(ee.Reducer.median(), 2); #注意这里的2,parallelScale参数

注意:2 这里设置的是parallelScale(用来限制内存使用的比例因子),使用更大的 parallelScale(例如 2 或 4)可能会启用默认情况下耗尽内存的计算。

// Reducers: ImageCollection.reduce()
var median = l8sr.reduce(ee.Reducer.median(), 2);

var visParams = {
  bands: ['SR_B4_median', 'SR_B3_median', 'SR_B2_median'], 
  min: 7000, 
  max: 20000
}

Map.setCenter(116, 30, 8);
Map.addLayer(median, visParams, 'median');

结果显示:

在这里插入图片描述

1.3 Greenest pixel (maximum NDVI) composite(理解去云操作)

使用Landsat8图层,计算NDVI,计算方法为(B5-B4)/(B5+B4),并结合B2, B3, B4。max(4) reducer 有 4 个输入,4 个输出:保持元组具有第一个 (NDVI) 输入的最大值,将 4 个波段图像的堆叠转换为单个 4 波段图像。

计算流程:

  • 加载Landsat8图像,并使用filterDate()选择时间,时间为2020年
  • 使用 maskL8sr() 函数对图层进行处理:去云、去除饱和像素;适当缩放波段;计算并插入一个NDVI波段
  • 选择 ImageCollection 的四个波段(NDVI、B2、B3、B4),并使用 ee.Reducer.max(4) 将它们缩小为单个图像。
// Computes a greenest-pixel composite of Landsat 8 SR images.
function maskL8sr(image) {
  var qaMask = image.select('QA_PIXEL').bitwiseAnd(parseInt('11111', 2)).eq(0);
  var saturationMask = image.select('QA_RADSAT').eq(0);

  // Apply the scaling factors to the appropriate bands.
  var opticalBands = image.select('SR_B.').multiply(0.0000275).add(-0.2);
  var thermalBands = image.select('ST_B.*').multiply(0.00341802).add(149.0);

  // Replace the original bands with the scaled ones and apply the masks.
  return image.addBands(opticalBands, null, true)
      .addBands(thermalBands, null, true)
      .addBands(image.normalizedDifference(['SR_B5', 'SR_B4']))
      .updateMask(qaMask)
      .updateMask(saturationMask);
}

// The bands we'll have in our composite, with the NDVI band first.
var bands = ['nd', 'SR_B2', 'SR_B3', 'SR_B4'];

var imgs = l8sr.filterDate('2020-01-01', '2020-12-31');

// Use setOutputs() to keep the band names (otherwise they're
// placed with "max", "max2", "max3", etc.).
var greenestPixel =
      imgs.map(maskL8sr)
          .select(bands)
          .reduce(ee.Reducer.max(4).setOutputs(bands));

var visParams = {
  bands: ['SR_B4', 'SR_B3', 'SR_B2'],
  min: 0,
  max: 0.5,
  gamma: 1.4,
};

Map.addLayer(greenestPixel, visParams);

// Optionallly display a simple median composite for comparison.
Map.addLayer(imgs.map(maskL8sr).median(), visParams, 'median');

语法注释:

  • parseInt(‘11111’, 2):强制转换,将’11111’这个2进制数字转化成了十进制数字31。
  • bitwiseAnd() 即“按位与”:对应的两个二进位都为1时,结果位才为1,如十进制位的6(0110)和10(1010),结果为0010。
  • .eq(0) 是将影像中等于0的像素变为1,其它像素变为0。

去云操作:

  • 第一步,选取影像的 ‘QA_PIXEL’ 波段;
  • 第二步,对其中的每一个像素和 ‘0000 0000 0001 1111’ 进行按位与运算(不到16位就往左补0);
  • 第三步,将值等于0的像素变为1,其它像素变为0。我们不难看出,第二步中,只有’XXXX XXXX XXX0 0000’(X代表0或1)这样的像素,在进行位运算后的结果为0,最终的结果为1。结合第一步中展示的表格,我们最终得到的影像中,值为0的像素就是函数中注释提到的需要筛去的像素。

结果显示:
median:

在这里插入图片描述

maxNDVI
在这里插入图片描述

1.4 ImageCollection reductions(注意事项)

这些 reducer 在 ImageCollection.reduce() 中不起作用,因为它们没有数字输出:
ee.Reducer.frequencyHistogram()
ee.Reducer.histogram()
ee.Reducer.toList()

如果不首先将每个输入转换为一维数组,这些缩减器将无法在 ImageCollection.reduce() 中工作:
ee.Reducer.covariance()
ee.Reducer.centeredCovariance()

1.5 Image Region reductions

var mean = image.reduceRegion({
  reducer: ee.Reducer.mean(), 
  geometry: santaCruz.geometry(),
  scale: 30,
  maxPixels: 1e9,
});

解释:
reducer: 需要使用的 reducer
geometry: 数据范围
scale: 每个像素的分辨率
crs: 投影,如果没有则设置为图像第一个波段的投影
maxPixels: 图像最大像素数量。默认为10,000,000个. 这是防止意外运行大量计算的保护机制。

在这里插入图片描述

// Reducers: Image.reduceRegion()
var santaCruz = counties.filter(ee.Filter.eq('NAME', 'Santa Cruz')).first();

var image = l8sr.filterBounds(santaCruz.geometry()).first()

var mean = image.reduceRegion({
  reducer: ee.Reducer.mean(), 
  geometry: santaCruz.geometry(),
  scale: 30,
  maxPixels: 1e9,
});

print(mean)

在这里插入图片描述
The result is a set of <band, band_mean> tuples.

1.6 Image Region reductions, cont.

var meanDictionary = composite.reduceRegion({
  reducer: ee.Reducer.mean(),
  geometry: region.geometry(),
  scale: 30,
  maxPixels: 1e10,
  tileScale: 4
});

此示例使用更大的 maxPixels 值以及使用 tileScale 参数。tileScale 是一个介于 0.1 和 16(默认为 1)之间的比例因子,用于调整聚合切片大小。设置较大的 tileScale(例如 2 或 4)会使用较小的图块,并且可能会启用默认情况下耗尽内存的计算。(类似于并行计算,将需要计算的图层分为2^n个)

提示:

  • 计算被分成每个像素的子计算,每个子计算都可以由不同的服务器完成。
  • 增加 tileScale 会使每台服务器的计算量更小,从而减少每台服务器内存不足的可能性。
  • 注意: 将计算拆分为更多像素的设置时间可能会使整体计算花费更长的时间。
// See:
// https://code.earthengine.google.com/?scriptPath=Examples%3ACloud%20Masking%2FLandsat8%20Surface%20Reflectance
function maskL8sr(image) {
  // Bit 0 - Fill
  // Bit 1 - Dilated Cloud
  // Bit 2 - Cirrus
  // Bit 3 - Cloud
  // Bit 4 - Cloud Shadow
  var qaMask = image.select('QA_PIXEL').bitwiseAnd(parseInt('11111', 2)).eq(0);
  var saturationMask = image.select('QA_RADSAT').eq(0);

  // Apply the scaling factors to the appropriate bands.
  var opticalBands = image.select('SR_B.').multiply(0.0000275).add(-0.2);
  var thermalBands = image.select('ST_B.*').multiply(0.00341802).add(149.0);

  // Replace the original bands with the scaled ones and apply the masks.
  return image.addBands(opticalBands, null, true)
      .addBands(thermalBands, null, true)
      .updateMask(qaMask)
      .updateMask(saturationMask);
}

var collection = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
                     .filterDate('2020-01-01', '2021-01-01')
                     .map(maskL8sr);

var composite = collection.median();


// Load an input region: Sierra Nevada.
var region = ee.Feature(ee.FeatureCollection('EPA/Ecoregions/2013/L3')
  .filter(ee.Filter.eq('us_l3name', 'Sierra Nevada'))
  .first());

// Reduce the region. The region parameter is the Feature geometry.
var meanDictionary = composite.reduceRegion({
  reducer: ee.Reducer.mean(),
  geometry: region.geometry(),
  scale: 30,
  maxPixels: 1e10,
  tileScale: 4
});

// The result is a Dictionary.  Print it.
print(meanDictionary);

在这里插入图片描述

1.7 Image RegionS reductions

var featuresWithMeans = image.reduceRegions({
  collection: maineCounties,
  reducer: ee.Reducer.mean(),
  scale: 30,
  tileScale: 4
});
// Load a FeatureCollection of counties in Maine.
var maineCounties = ee.FeatureCollection('TIGER/2016/Counties')
  .filter(ee.Filter.eq('STATEFP', '23'));

var image = l8sr
    .filterBounds(maineCounties)
    .filterDate('2020-01-01', '2020-12-31')
    .median()

// Add reducer output to the Features in the collection.
var maineMeansFeatures = image.reduceRegions({
  collection: maineCounties,
  reducer: ee.Reducer.mean(),
  scale: 30,
  tileScale: 2
});

// Print the first feature, to illustrate the result.
print(ee.Feature(maineMeansFeatures.first()).select(image.bandNames()));

使用 reduceRegions() 执行图像区域缩减。返回输入特征,每个特征都增加了相应的 reducer 输出。
在这里插入图片描述

2. Image Neighborhood reductions (convolution)(卷积Kernel)

在这里插入图片描述
在这里插入图片描述

// Reducers: Image.reduceNeighborhood()

// Region in the redwood forest
var redwoods = ee.Geometry.Rectangle(-124.0665, 41.0739, -123.934, 41.2029);

// Input NAIP imagery
//National Agriculture Imagery Program (NAIP) ImageCollection
var naipCollection = ee.ImageCollection('USDA/NAIP/DOQQ')
  .filterBounds(redwoods)
  .filterDate('2012-01-01', '2012-12-31');
var naip = naipCollection.mosaic();

// Compute NDVI from the NAIP
var naipNDVI = naip.normalizedDifference(['N', 'R']);

// Standard Deviation (SD) as texture of NDVI
// a circle kernel of radius 7 pixels
var texture = naipNDVI.reduceNeighborhood({
  reducer: ee.Reducer.stdDev(), 
  kernel: ee.Kernel.circle(7), 
});

// Display
Map.centerObject(redwoods, 12);
Map.addLayer(naip, {}, 'NAIP input imagery');
Map.addLayer(naipNDVI, {min: -1, max: 1, palette:['FF0000', '00FF00']}, 'NDVI');
Map.addLayer(texture, {min:0, max: 0.3}, 'SD of NDVI');

NAIP:
NAIP
NDVI:
在这里插入图片描述
SD:表示NDVI的纹理
在这里插入图片描述

3.FeatureCollection column reductions(属性运算)

在这里插入图片描述
脚本流程:
1.加载美国人口普查数据,并在Benton County选择需要的属性
2.选择人口和房屋单元两个属性
3.实现reduceColumn操作:selectors设置为人口和房屋单元属性;reducer设置为ee.Reducer.sum().repeat(2)

Notes: Unlike imageCollection.reduce(), in which reducers are automatically repeated for each band, reducers on a FeatureCollection must be explicitly repeated using repeat(). Specifically, repeat the reducer m times for m inputs.

在这里插入图片描述

// Consider a FeatureCollection of US census blocks with census data as attributes.
// The variables of interest are total population and total housing units.
// You can get their sum(s) by supplying a summing reducer argument to 
// reduceColumns() and printing the result.

// Load US cenus data as a FeatureCollection.
var census = ee.FeatureCollection('TIGER/2010/Blocks');

// Filter the collection to include only Benton County, OR.
var benton = census.filter(
  ee.Filter.and(
    ee.Filter.eq('statefp10', '41'),
    ee.Filter.eq('countyfp10', '003')
  )
);

// Display Benton County cenus blocks.
Map.setCenter(-123.27, 44.57, 13);
Map.addLayer(benton);

// Compute sums of the specified properties.
var properties = ['pop10', 'housing10'];
var sums = benton
    .filter(ee.Filter.notNull(properties))
    .reduceColumns({
      reducer: ee.Reducer.sum().repeat(2),
      selectors: properties
    });

// Print the resultant Dictionary.
print(sums);

结果显示:
在这里插入图片描述
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/169053.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

01背包——二维动态规划【c++】代码实现

今天学了01背包&#xff0c;就想来讲一讲&#xff0c;正好回顾一下&#xff08;BZOJ上的题目&#xff09;。 01背包 所谓01背包&#xff0c;也就是背包的一种&#xff0c;01背包和完全背包的区别就在于&#xff0c;01背包一件物品只能选择一次&#xff0c;而完全背包可以重复…

架构运维篇(七):Centos7/Linux中安装Zookeeper

版本说明 JDK &#xff1a;1.8&#xff08;已安装&#xff09;ZK : 3.8.0 安装部署Zookeeper 第一步&#xff1a;下载最新版本 官网地址&#xff1a;Apache DownloadsHome page of The Apache Software Foundationhttps://www.apache.org/dyn/closer.lua/zookeeper/zookeep…

libvirt零知识学习1 —— libvirt简介

本文内容部分取自《KVM实战 —— 原理、进阶与性能调优》&#xff0c;非常好的一本书。 1. 概述 提到KVM的管理工具&#xff0c;首先必须要介绍的无疑是大名鼎鼎的libvirt。libvirt是目前使用最为广泛的对KVM虚拟机进行管理的工具和应用程序接口。并且&#xff0c;一些常用的虚…

Oracle VM VirtualBox 安装 CentOS Linux

Virtual Box VirtualBox是一个强大的、面向个人用户或者企业用户的虚拟机产品&#xff0c;其支持x86以及AMD64/Intel64的计算架构&#xff0c;功能特性丰富、性能强劲&#xff0c;支持GPL开源协议&#xff0c;其官方网址是www.virtualbox.org&#xff0c;由Oracle开源&#xf…

Spring自动装配

自动装配的三种方式一、xml方式自动装配二、 按类型自动装配三、使用注解一、xml方式自动装配 byName: 会自动在容器上下文查找&#xff0c;和自己对象set方法后面的值对应的bean id&#xff08;通过id匹配&#xff09; //实体类 Data public class Pepole {private String n…

GameFrameWork框架(Unity3D)使用笔记(八) 实现场景加载进度条

前言&#xff1a; 游戏在转换场景的时候&#xff0c;需要花费时间来加载相关的资源。而这个过程往往因为游戏场景的规模和复杂度以及玩家电脑配置的原因花费一小段时间&#xff08;虽然这个项目里用不到&#xff09;。 所以&#xff0c;如果这一小段时间&#xff0c;画面就卡在…

计算机网络 —— TCP篇 三次握手与四次挥手

计算机网络系列文章目录 TCP篇 三次握手四次挥手 socket 文章目录计算机网络系列文章目录前言4.1 tcp三次握手与四次挥手面试题4.1.1 TCP的基本认识TCP头部为什么需要tcp&#xff0c;tcp工作在哪一层什么是tcp什么是tcp连接如何唯一确定一个tcp连接(tcp&udp比较) UDP 和 T…

机器学习【西瓜书/南瓜书】--- 第1章绪论+第二章模型选择和评估(学习笔记+公式推导)

【西瓜书南瓜书】task01: 第1、2章&#xff08;2天&#xff09; 第一章 绪论 主要符号表 下述这些符号在本书中将频繁的使用&#xff0c;务必牢记于心各个特殊符号所具有的的含义 &#x1f31f;对上述部分定义做一下解释&#xff1a; 向量有维度&#xff0c;其中的元素是有…

路由器NAT典型配置

1.组网需求内部网络中IP地址为10.110.10.0/24的用户可以访问Internet&#xff0c;其它网段的用户则不能访问Internet。外部的PC可以访问内部的服务器。公司具有202.38.160.100/24至202.38.160.105/24六个合法的IP地址。选用202.38.160.100作为公司对外的IP地址&#xff0c;WWW服…

stm32平衡小车(2)-----编码器电机驱动

前言&#xff1a;之前做arduino小车的时候使用的是L298N电机&#xff0c;没有用过编码器&#xff0c;这次第一次用编码器&#xff0c;还是比较懵圈&#xff0c;记录一下学的整个过程。 1.编码器的简介 霍尔编码器是一种通过磁电转换将输出轴上的机械几何位移量转换成脉冲或数字…

下载整个网站

使用Cyotek WebCopy下载整个网站一、下载网站复制所需软件&#xff1a;二、打开Cyotek WebCopy&#xff0c;然后下载网站系统环境&#xff1a;win10 所需工具&#xff1a;Cyotek WebCopy 一、下载网站复制所需软件&#xff1a; https://www.cyotek.com/cyotek-webcopy/downloa…

3-3存储系统-高速缓冲存储器Cache

文章目录一.概述二.Cache与主存的映射方式1.直接映射2.全相联映射3.组相联映射三.Cache中主存块的替换算法1.随机算法RAND2.先进先出算法FIFO3.最近最少使用算法LRU4.最不经常使用算法LFU四.Cache写策略1.写命中2.写不命中一.概述 高速缓冲存储器&#xff08;Cache&#xff09…

labelme使用labelme2voc.py忽略部分标签生成VOC/coco类型数据集(Ignore a label in json file)

一&#xff1a;背景和问题描述这是一个比较小众&#xff0c;且查找全网未有答案的问题。在使用labelme标注数据之后&#xff0c;生成了json文件。在利用json文件生成可用于训练分割模型的voc或者coco数据集时&#xff0c;可以使用labelme中自带的示例文件labelme2voc.py&#x…

第二章.线性回归以及非线性回归—弹性网

第二章.线性回归以及非线性回归 2.14 弹性网&#xff08;Elastic Net&#xff09; Elastic Net是一种使用L1和L2作为正则化矩阵的线性回归模型&#xff0c;这种组合用于只有很少的权重非零的稀疏模型。 当多个特征和另一个特征相关时&#xff0c;弹性网络非常有用&#xff0c;L…

C++ STL简介

文章目录前言一、STL概述二、STL历史三、STL六大组件功能与运用前言 对C/C学习感兴趣的可以看看这篇文章哦&#xff1a;C/C教程 一、STL概述 在软件行业&#xff0c;一直都希望建立一种可以重复运用的东西和一种可以制造出“可重复使用的东西”的方法&#xff0c;来让程序员…

什么是基于模型设计(MBD)?

MBD(Model based Design)是什么&#xff1f; 随着嵌入式行业的快速发展&#xff0c;MBD作为解决专家紧缺、国内解决方案匮乏的新软件开发方式备受关注。 MBD是一种通过建模自动生成代码的&#xff08;Auto-generation Code&#xff09;开发方式。 可以说它与如何用传统的C/C等计…

CANoe-仿真总线上的红蓝线、“CANoe DEMO“ license下的软件限制

1、仿真总线上的红蓝线 最近有人问我一个问题,就是Simulation Setup界面下的仿真网络里,有两条线:红线、蓝线。它们表示什么意思呢? 有人可能首先想到:会不会是代表CAN总线的两条物理连线? 那LIN网络呢?LIN网络属于单条线,它在仿真网络里应该显示一条线咯? LIN总线也…

ThreadLocal 详解

ThreadLocal 详解 1. ThreadLocal简介 1.1 官方介绍 /*** This class provides thread-local variables. These variables differ from* their normal counterparts in that each thread that accesses one (via its* {code get} or {code set} method) has its own, indep…

【go语言http2.0代码实现】

go语言之http2.0Serverclient之前主要说的是基于http1.1的。然后因为http1.1存在的局限性&#xff0c;现在http2.0越来越流行&#xff0c;这里来根据go的源码来分析一下http2.0的实现。首先需要注意的是http2.0是在https的基础之上&#xff0c;因此推荐有https的基础或者看前面…

如何在Microsoft Teams中发布作业

之前我们介绍了在Microsoft Teams中收发批改作业的基础使用方法。今天我们会在“发布作业”这个方面上进行详细介绍。 首先我们进入Teams的“作业”点击“创建”。 选择“作业”。 可以通过“附加”的形式发布已创建的作业文件。 可以通过选择OneDrive的形式来选取里面的文件发…