ee.Image.arraySlice

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

用量傳回
Image.arraySlice(axis, start, end, step)圖片
引數類型詳細資料
這個:input圖片輸入陣列圖片。
axis整數,預設值為 0軸到子集。
start圖片 (預設值:null)沿著「軸」的第一個切片座標 (含)。負數用於相對於陣列結尾定位切片開頭,其中 -1 從軸上的最後一個位置開始,-2 從倒數第二個位置開始,依此類推。起始索引必須有一個帶狀區域,或每個「輸入」帶狀區域各有一個帶狀區域。如果未設定或遮蓋這個引數的某些像素,該像素的切片就會從索引 0 開始。
end圖片 (預設值:null)停止擷取切片的座標 (不含)。根據預設,這會是指定軸的長度。負數可用來指定切片結尾相對於陣列結尾的位置,其中 -1 會排除最後一個位置,-2 會排除最後兩個位置,依此類推。結尾索引必須有一個帶,或每個「輸入」帶有一個帶。如果未在某個像素設定或遮蓋這個引數,該像素的切片會在最後一個索引之後結束。
step整數,預設值為 1沿著「axis」的切片間隔;系統會從「start」(含) 到「end」(不含) 的每個「step」整數倍數位置取一個切片。必須為正數。

範例

程式碼編輯器 (JavaScript)

// A function to print arrays for a selected pixel in the following examples.
function sampArrImg(arrImg) {
  var point = ee.Geometry.Point([-121, 42]);
  return arrImg.sample(point, 500).first().get('array');
}

// Create a 1D array image with length 12.
var arrayImg1D = ee.Image([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]).toArray();
print('1D array image (pixel)', sampArrImg(arrayImg1D));
// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

// Get the first 3 elements.
print('1D array image first 3 elements (pixel)',
      sampArrImg(arrayImg1D.arraySlice(0, 0, 3)));
// [0, 1, 2]

// Get the last 3 elements.
print('1D array image last 3 elements (pixel)',
      sampArrImg(arrayImg1D.arraySlice(0, -3)));
// [9, 10, 11]

// Get elements at index positions 3 through 5 (0-based).
print('1D array image elements at index positions 3 through 5 (pixel)',
      sampArrImg(arrayImg1D.arraySlice(0, 3, 6)));
// [3, 4, 5]

// Get elements at index positions 4 through end (0-based).
print('1D array image elements at index positions 4 through end (pixel)',
      sampArrImg(arrayImg1D.arraySlice(0, 4)));
// [4, 5, 6, 7, 8, 9, 10, 11]

// Get elements using a step of 3.
print('1D array image elements at a step of 3 (pixel)',
      sampArrImg(arrayImg1D.arraySlice(0, 0, null, 3)));
// [0, 3, 6, 9]

// Create a 2D array image with 3 rows and 4 columns.
var arrayImg2D = arrayImg1D.arrayReshape(ee.Image([3, 4]).toArray(), 2);
print('2D array image (pixel)', sampArrImg(arrayImg2D));
// [[0, 1,  2,  3],
//  [4, 5,  6,  7],
//  [8, 9, 10, 11]]

// Get the second row.
print('2D array image second row (pixel)',
      sampArrImg(arrayImg2D.arraySlice(0, 1, 2)));
// [[4, 5, 6, 7]

// Get the second column.
print('2D array image second column (pixel)',
      sampArrImg(arrayImg2D.arraySlice(1, 1, 2)));
// [[1],
//  [5],
//  [9]]

// Get all columns except the last.
print('2D array image all columns except last (pixel)',
      sampArrImg(arrayImg2D.arraySlice(1, 0, -1)));
// [[0, 1,  2],
//  [4, 5,  6],
//  [8, 9, 10]]

Python 設定

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

import ee
import geemap.core as geemap

Colab (Python)

# A function to print arrays for a selected pixel in the following examples.
def samp_arr_img(arr_img):
  point = ee.Geometry.Point([-121, 42])
  return arr_img.sample(point, 500).first().get('array')

# Create a 1D array image with length 12.
array_img_1d = ee.Image([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]).toArray()
print('1D array image (pixel):', samp_arr_img(array_img_1d).getInfo())
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

# Get the first 3 elements.
print('1D array image first 3 elements (pixel):',
      samp_arr_img(array_img_1d.arraySlice(0, 0, 3)).getInfo())
# [0, 1, 2]

# Get the last 3 elements.
print('1D array image last 3 elements (pixel):',
      samp_arr_img(array_img_1d.arraySlice(0, -3)).getInfo())
# [9, 10, 11]

# Get elements at index positions 3 through 5 (0-based).
print('1D array image elements at index positions 3 through 5 (pixel):',
      samp_arr_img(array_img_1d.arraySlice(0, 3, 6)).getInfo())
# [3, 4, 5]

# Get elements at index positions 4 through end (0-based).
print('1D array image elements at index positions 4 through end (pixel)',
      samp_arr_img(array_img_1d.arraySlice(0, 4)).getInfo())
# [4, 5, 6, 7, 8, 9, 10, 11]

# Get elements using a step of 3.
print('1D array image elements at a step of 3 (pixel)',
      samp_arr_img(array_img_1d.arraySlice(0, 0, None, 3)).getInfo())
# [0, 3, 6, 9]

# Create a 2D array image with 3 rows and 4 columns.
array_img_2d = array_img_1d.arrayReshape(ee.Image([3, 4]).toArray(), 2)
print('2D array image (pixel)', samp_arr_img(array_img_2d).getInfo())
# [[0, 1,  2,  3],
#  [4, 5,  6,  7],
#  [8, 9, 10, 11]]

# Get the second row.
print('2D array image second row (pixel):',
      samp_arr_img(array_img_2d.arraySlice(0, 1, 2)).getInfo())
# [[4, 5, 6, 7]

# Get the second column.
print('2D array image second column (pixel):',
      samp_arr_img(array_img_2d.arraySlice(1, 1, 2)).getInfo())
# [[1],
#  [5],
#  [9]]

# Get all columns except the last.
print('2D array image all columns except last (pixel):',
      samp_arr_img(array_img_2d.arraySlice(1, 0, -1)).getInfo())
# [[0, 1,  2],
#  [4, 5,  6],
#  [8, 9, 10]]