zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Google Earth Engine(GEE)——数组及其切片简介

Google数组 简介 及其 Engine Earth GEE 切片
2023-09-11 14:15:12 时间

Earth Engine 表示 1-D 向量、2-D 矩阵、3-D 立方体和具有该ee.Array类型的更高维超立方体。数组是一种灵活的数据结构,但为了换取它们提供的强大功能,它们的伸缩性不如地球引擎中的其他数据结构。如果问题可以在不使用数组的情况下解决,那么结果的计算速度会更快、效率更高。但是,如果问题需要更高维度的模型、灵活的线性代数或任何其他数组特别适合的东西,则可以使用Array该类。

这里官方给出了一个简单的教学方案:

https://youtu.be/-qo8L5GmKO0

数组维度、形状和大小

数组的维数是指底层数据沿其变化的轴数。例如,0-D 数组是标量数,1-D 数组是向量,2-D 数组是矩阵,3-D 数组是立方体,>3-D 数组是超立方体。对于一个 N 维数组,从 0 到 N-1 有 N 个轴。阵列的形状由轴的长度决定。轴的长度是沿它的位置数。数组大小或数组中的总元素数等于轴长度的乘积。每个轴上每个位置的每个值都必须有一个有效数字,因为当前不支持稀疏或参差不齐的数组。数组的元素类型表示每个元素是什么类型的数字;数组的所有元素都将具有相同的类型。

Earth Engine 中的数组由数字列表和列表列表构成。嵌套的程度决定了维数。要从一个简单的、有动机的示例开始,请考虑以下Array从 Landsat 5 tasseled cap (TC) 系数(Crist 和 Cicone 1984)创建的案例:

函数:

length()

返回一个一维 EEArray,其中包含给定 EEArray 的每个维度的长度。无论你输入的是几个维度的数据,最终的结果都会显示为一个列表集合的一维数组

Returns a 1-D EEArray containing the length of each dimension of the given EEArray.

Arguments:

this:array (Array):

The array from which to extract the axis lengths.

Returns: Array

//创建一个缨帽系数数组
var coefficients = ee.Array([
  [0.3037, 0.2793, 0.4743, 0.5585, 0.5082, 0.1863],
  [-0.2848, -0.2435, -0.5436, 0.7243, 0.0840, -0.1800],
  [0.1509, 0.1973, 0.3279, 0.3406, -0.7112, -0.4572],
  [-0.8242, 0.0849, 0.4392, -0.0580, 0.2012, -0.2768],
  [-0.3280, 0.0549, 0.1075, 0.1855, -0.4357, 0.8085],
  [0.1084, -0.9022, 0.4120, 0.0573, -0.0251, 0.0238]
]);

//使用 确认这是一个 6x6 二维数组length(),它将返回每个轴的长度:
// 答应出来是一个维度,也就是6*6的格式,因为这里选取的是这个数组的长宽
print(coefficients.length()); //    [6,6]

下表说明了矩阵条目沿 0 轴和 1 轴的排列:0为竖轴,1为横轴。

1 轴 ->
012345
00.30370.27930.47430.55850.50820.1863
1-0.2848-0.2435-0.54360.72430.0840-0.1800
0轴20.15090.19730.32790.3406-0.7112-0.4572
3-0.82420.08490.4392-0.05800.2012-0.2768
4-0.32800.05490.10750.1855-0.43570.8085
50.1084-0.90220.41200.0573-0.02510.0238

表格左侧的索引表示沿 0 轴的位置。0 轴上每个列表中的第 n 个元素位于 1 轴上的第 n 个位置。例如,数组坐标 [3,1] 处的条目是 0.0849。假设“绿色度”是感兴趣的 TC 分量。您可以使用slice()以下方法获得绿色子矩阵:

函数:

slice(axisstartendstep)

通过以“step”为增量沿给定轴从“开始”(包括)到“结束”(不包括)切出每个位置来创建子数组。结果将具有与输入一样多的维度,并且在除切片轴之外的所有方向上都具有相同的长度,其中长度将是从“开始”到“结束”的“步长”范围内的位置数输入数组沿“轴”的长度。这意味着如果 start=end,或者如果开始或结束值完全超出范围,结果可以是沿给定轴的长度 0。

Creates a subarray by slicing out each position along the given axis from the 'start' (inclusive) to 'end' (exclusive) by increments of 'step'. The result will have as many dimensions as the input, and the same length in all directions except the slicing axis, where the length will be the number of positions from 'start' to 'end' by 'step' that are in range of the input array's length along 'axis'. This means the result can be length 0 along the given axis if start=end, or if the start or end values are entirely out of range.

Arguments:

轴(整数,默认值:0):
要切片的轴。

开始(整数,默认值:0):
第一个切片(包括)沿“轴”的坐标。负数用于相对于数组的末尾定位切片的开始,其中 -1 从轴上的最后一个位置开始,-2 从最后一个位置的下一个位置开始,依此类推。

结束(整数,默认值:空):
停止切片的坐标(独占)。默认情况下,这将是给定轴的长度。负数用于相对于数组的末尾定位切片的末尾,其中 -1 将排除最后一个位置,-2 将排除最后两个位置等。

步长(整数,默认值:1):
切片之间沿“轴”的间隔;将在从“开始”(包括)到“结束”(不包括)的“步”的每个整数倍处取一个切片。

this:array (Array):

Array to slice.

axis (Integer, default: 0):

The axis to slice on.

start (Integer, default: 0):

The coordinate of the first slice (inclusive) along 'axis'. Negative numbers are used to position the start of slicing relative to the end of the array, where -1 starts at the last position on the axis, -2 starts at the next to last position, etc.

end (Integer, default: null):

The coordinate (exclusive) at which to stop taking slices. By default this will be the length of the given axis. Negative numbers are used to position the end of slicing relative to the end of the array, where -1 will exclude the last position, -2 will exclude the last two positions, etc.

step (Integer, default: 1):

The separation between slices along 'axis'; a slice will be taken at each whole multiple of 'step' from 'start' (inclusive) to 'end' (exclusive). Must be positive.

Returns: Array

//获取 1x6 greenness切片,显示它。设置轴,设置切片的起始值,重点值和步长
var greenness = coefficients.slice({axis: 0, start: 1, end: 2, step: 1});
print(greenness);
//最后输出的结果:就是第2行数据
//[[-0.2848,-0.2435,-0.5436,0.7243,0.0840,-0.1800]]

注:观察startend参数slice() 对应于表中显示的 0 轴索引(start包含和end不包含)。我在函数当中标红了