ee.Image.arraySlice

Crée un sous-tableau en découpant chaque position le long de l'axe donné, de "start" (inclusif) à "end" (exclusif) par incréments de "step".

Le résultat aura autant de dimensions que l'entrée, et la même longueur dans toutes les directions, à l'exception de l'axe de découpage, où la longueur correspondra au nombre de positions de "start" à "end" par "step" qui se trouvent dans la plage de la longueur du tableau d'entrée le long de "axis". Cela signifie que le résultat peut avoir une longueur de 0 le long de l'axe donné si start=end, ou si les valeurs de début ou de fin sont entièrement hors plage.

UtilisationRenvoie
Image.arraySlice(axis, start, end, step)Image
ArgumentTypeDétails
this: inputImageImage du tableau d'entrée.
axisEntier, par défaut : 0Axe à sous-ensemble.
startImage, par défaut : nullCoordonnée de la première tranche (inclusive) le long de "axis". Les nombres négatifs sont utilisés pour positionner le début du découpage par rapport à la fin du tableau, où -1 commence à la dernière position de l'axe, -2 commence à l'avant-dernière position, etc. Il doit y avoir une bande pour les indices de début, ou une bande par bande "input". Si cet argument n'est pas défini ou masqué au niveau d'un pixel, la tranche de ce pixel commence à l'index 0.
endImage, par défaut : nullCoordonnée (exclusive) à laquelle arrêter de prendre des tranches. Par défaut, il s'agit de la longueur de l'axe donné. Les nombres négatifs sont utilisés pour positionner la fin du découpage par rapport à la fin du tableau, où -1 exclut la dernière position, -2 exclut les deux dernières positions, etc. Il doit y avoir une bande pour les indices de fin, ou une bande par bande "input". Si cet argument n'est pas défini ou masqué au niveau d'un pixel, la tranche de ce pixel se termine juste après le dernier index.
stepEntier, par défaut : 1Séparation entre les tranches le long de "axis" ; une tranche est prise à chaque multiple entier de "step" de "start" (inclusif) à "end" (exclusif). Doit être positive.

Exemples

Éditeur de code (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]]

Configuration de Python

Pour en savoir plus sur l'API Python et sur l'utilisation de geemap pour le développement interactif, consultez la page Environnement Python.

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()
display('1D array image (pixel):', samp_arr_img(array_img_1d))
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

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

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

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

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

# Get elements using a step of 3.
display('1D array image elements at a step of 3 (pixel)',
        samp_arr_img(array_img_1d.arraySlice(0, 0, None, 3)))
# [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)
display('2D array image (pixel)', samp_arr_img(array_img_2d))
# [[0, 1,  2,  3],
#  [4, 5,  6,  7],
#  [8, 9, 10, 11]]

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

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

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