天天看點

Google Earth Engine(GEE)——數組及其切片簡介

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 軸 ->
1 2 3 4 5
0.3037 0.2793 0.4743 0.5585 0.5082 0.1863
1 -0.2848 -0.2435 -0.5436 0.7243 0.0840 -0.1800
0軸 2 0.1509 0.1973 0.3279 0.3406 -0.7112 -0.4572
3 -0.8242 0.0849 0.4392 -0.0580 0.2012 -0.2768
4 -0.3280 0.0549 0.1075 0.1855 -0.4357 0.8085
5 0.1084 -0.9022 0.4120 0.0573 -0.0251 0.0238

表格左側的索引表示沿 0 軸的位置。0 軸上每個清單中的第 n 個元素位于 1 軸上的第 n 個位置。例如,數組坐标 [3,1] 處的條目是 0.0849。假設“綠色度”是感興趣的 TC 分量。您可以使用​

​slice()​

​以下方法獲得綠色子矩陣:

函數:

slice(axis, start, end, step)

通過以“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):

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]]