ee.Array.slice

從「start」(含) 到「end」(不含) 沿著指定軸,以「step」的增量切出每個位置,建立子陣列。結果的維度數量會與輸入內容相同,且除了切片軸之外,所有方向的長度都會相同。在切片軸上,長度會是從「start」到「end」的「step」位置數,這些位置位於輸入陣列沿「axis」的長度範圍內。也就是說,如果 start=end,或 start 或 end 值完全超出範圍,則結果沿指定軸的長度可能為 0。

用量傳回
Array.slice(axis, start, end, step)陣列
引數類型詳細資料
這個:array陣列要切片的陣列。
axis整數,預設值為 0要切片的軸。
start整數,預設值為 0沿著「軸」的第一個切片座標 (含)。負數是用來相對於陣列結尾定位切片開頭,其中 -1 從軸上的最後一個位置開始,-2 從倒數第二個位置開始,依此類推。
end整數,預設值為 null停止擷取切片的座標 (不含)。根據預設,這會是指定軸的長度。負數用於相對於陣列結尾來定位切片結尾,其中 -1 會排除最後一個位置,-2 會排除最後兩個位置,依此類推。
step整數,預設值為 1沿著「axis」的切片間隔;系統會從「start」(含) 到「end」(不含) 的每個「step」整數倍數位置取一個切片。必須為正數。

範例

程式碼編輯器 (JavaScript)

var array1x6 = ee.Array([1, 2, 3, 4, 5, 6]);
print(array1x6.slice());  // [1,2,3,4,5,6]
print(array1x6.slice(0));  // [1,2,3,4,5,6]
print(array1x6.slice(0, 0, 6, 1));  // [1,2,3,4,5,6]
print(array1x6.slice(0, 0, 10, 1));  // [1,2,3,4,5,6]

print(array1x6.slice(0, 2));  // [3,4,5,6]
print(array1x6.slice(0, 5));  // [6]
print(array1x6.slice(0, 6));  // []
print(array1x6.slice(0, 0, 2));  // [1,2]
print(array1x6.slice(0, 0, 0));  // []

// Negative start and end.
print(array1x6.slice(0, 0, -3));  // [1,2,3]
print(array1x6.slice(0, -2, 6));  // [5,6]

print(array1x6.slice(0, 0, 6, 2));  // [1,3,5]
print(array1x6.slice(0, 0, 6, 3));  // [1,4]
print(array1x6.slice(0, 0, 6, 4));  // [1,5]
print(array1x6.slice(0, 0, 6, 6));  // [1]

print(array1x6.slice(0, 2, 6, 2));  // [3,5]

var array3x2 = ee.Array([[1, 2], [3, 4], [5, 6]]);
print(array3x2.slice());  // [[1,2],[3,4],[5,6]]
print(array3x2.slice(0));  // [[1,2],[3,4],[5,6]]
print(array3x2.slice(0, 0));  // [[1,2],[3,4],[5,6]]
print(array3x2.slice(0, 0, 1));  // [[1,2]]
print(array3x2.slice(0, 0, 2));  // [[1,2],[3,4]]
print(array3x2.slice(0, 0, 3, 1));  // [[1,2],[3,4],[5,6]]
print(array3x2.slice(0, 0, 3, 2));  // [[1,2],[5,6]]
print(array3x2.slice(0, 1, 3, 2));  // [[3,4]]
print(array3x2.slice(0, 0, 3, 3));  // [[1,2]]

print(array3x2.slice(1));  // [[1,2],[3,4],[5,6]]
print(array3x2.slice(1, 1));  // [[2],[4],[6]]
print(array3x2.slice(1, 0, 1));  // [[1],[3],[5]]

var empty = ee.Array([], ee.PixelType.int8());
print(empty.slice());  // []
print(empty.slice(0));  // []
print(empty.slice(0, 0, 0, 1));  // []

Python 設定

請參閱 Python 環境頁面,瞭解 Python API 和如何使用 geemap 進行互動式開發。

import ee
import geemap.core as geemap

Colab (Python)

array1x6 = ee.Array([1, 2, 3, 4, 5, 6])
display(array1x6.slice())  # [1, 2, 3, 4, 5, 6]
display(array1x6.slice(0))  # [1, 2, 3, 4, 5, 6]
display(array1x6.slice(0, 0, 6, 1))  # [1, 2, 3, 4, 5, 6]
display(array1x6.slice(0, 0, 10, 1))  # [1, 2, 3, 4, 5, 6]

display(array1x6.slice(0, 2))  # [3, 4, 5, 6]
display(array1x6.slice(0, 5))  # [6]
display(array1x6.slice(0, 6))  # []
display(array1x6.slice(0, 0, 2))  # [1, 2]
display(array1x6.slice(0, 0, 0))  # []

# Negative start and end.
display(array1x6.slice(0, 0, -3))  # [1, 2, 3]
display(array1x6.slice(0, -2, 6))  # [5, 6]

display(array1x6.slice(0, 0, 6, 2))  # [1, 3, 5]
display(array1x6.slice(0, 0, 6, 3))  # [1, 4]
display(array1x6.slice(0, 0, 6, 4))  # [1, 5]
display(array1x6.slice(0, 0, 6, 6))  # [1]

display(array1x6.slice(0, 2, 6, 2))  # [3, 5]

array3x2 = ee.Array([[1, 2], [3, 4], [5, 6]])
display(array3x2.slice())  # [[1, 2], [3, 4], [5, 6]]
display(array3x2.slice(0))  # [[1, 2], [3, 4], [5, 6]]
display(array3x2.slice(0, 0))  # [[1, 2],[3, 4],[5, 6]]
display(array3x2.slice(0, 0, 1))  # [[1, 2]]
display(array3x2.slice(0, 0, 2))  # [[1, 2], [3, 4]]
display(array3x2.slice(0, 0, 3, 1))  # [[1, 2], [3, 4], [5, 6]]
display(array3x2.slice(0, 0, 3, 2))  # [[1, 2], [5, 6]]
display(array3x2.slice(0, 1, 3, 2))  # [[3, 4]]
display(array3x2.slice(0, 0, 3, 3))  # [[1, 2]]

display(array3x2.slice(1))  # [[1, 2], [3, 4], [5, 6]]
display(array3x2.slice(1, 1))  # [[2], [4], [6]]
display(array3x2.slice(1, 0, 1))  # [[1], [3], [5]]

empty = ee.Array([], ee.PixelType.int8())
display(empty.slice())  # []
display(empty.slice(0))  # []
display(empty.slice(0, 0, 0, 1))  # []