Gli array in Earth Engine sono costituiti da elenchi di numeri ed elenchi di elenchi. Il
grado di nidificazione determina il numero di dimensioni. Per iniziare con un esempio semplice e motivato, considera il seguente esempio di Array
creato dai coefficienti del cappuccio con tasselli (TC) di Landsat 8 (Baig et al., 2014):
Editor di codice (JavaScript)
// Create an Array of Tasseled Cap coefficients. var coefficients = ee.Array([ [0.3029, 0.2786, 0.4733, 0.5599, 0.508, 0.1872], [-0.2941, -0.243, -0.5424, 0.7276, 0.0713, -0.1608], [0.1511, 0.1973, 0.3283, 0.3407, -0.7117, -0.4559], [-0.8239, 0.0849, 0.4396, -0.058, 0.2013, -0.2773], [-0.3294, 0.0557, 0.1056, 0.1855, -0.4349, 0.8085], [0.1079, -0.9023, 0.4119, 0.0575, -0.0259, 0.0252], ]);
import ee import geemap.core as geemap
Colab (Python)
# Create an Array of Tasseled Cap coefficients. coefficients = ee.Array([ [0.3029, 0.2786, 0.4733, 0.5599, 0.508, 0.1872], [-0.2941, -0.243, -0.5424, 0.7276, 0.0713, -0.1608], [0.1511, 0.1973, 0.3283, 0.3407, -0.7117, -0.4559], [-0.8239, 0.0849, 0.4396, -0.058, 0.2013, -0.2773], [-0.3294, 0.0557, 0.1056, 0.1855, -0.4349, 0.8085], [0.1079, -0.9023, 0.4119, 0.0575, -0.0259, 0.0252], ])
Verifica che si tratti di un array 2D di 6 x 6 utilizzando length()
, che restituirà le lunghezze di ogni asse:
Editor di codice (JavaScript)
// Print the dimensions. print(coefficients.length()); // [6,6]
import ee import geemap.core as geemap
Colab (Python)
# Print the dimensions. display(coefficients.length()) # [6,6]
La seguente tabella illustra la disposizione delle voci della matrice lungo l'asse 0 e l'asse 1:
1 asse -> | |||||||
---|---|---|---|---|---|---|---|
0 | 1 | 2 | 3 | 4 | 5 | ||
0 | 0,3029 | 0,2786 | 0,4733 | 0,5599 | 0,508 | 0,1872 | |
1 | -0,2941 | -0,243 | -0,5424 | 0,7276 | 0,0713 | -0,1608 | |
Asse 0 | 2 | 0,1511 | 0,1973 | 0,3283 | 0,3407 | -0,7117 | -0,4559 |
3 | -0,8239 | 0,0849 | 0,4396 | -0,058 | 0,2013 | -0,2773 | |
4 | -0,3294 | 0,0557 | 0,1056 | 0,1855 | -0,4349 | 0,8085 | |
5 | 0,1079 | -0,9023 | 0,4119 | 0,0575 | -0,0259 | 0,0252 |
Gli indici a sinistra della tabella indicano le posizioni lungo l'asse 0. L'elemento n-esimo
all'interno di ogni elenco sull'asse 0 si trova nella posizione n-esima lungo l'asse 1. Ad esempio, la voce alla coordinata [3,1] dell'array è 0,0849. Supponiamo che "verde" sia il
componente TC di interesse. Puoi ottenere la sottomatrice di verde utilizzando slice()
:
Editor di codice (JavaScript)
// Get the 1x6 greenness slice, display it. var greenness = coefficients.slice({axis: 0, start: 1, end: 2, step: 1}); print(greenness);
import ee import geemap.core as geemap
Colab (Python)
# Get the 1x6 greenness slice, display it. greenness = coefficients.slice(axis=0, start=1, end=2, step=1) display(greenness)
La matrice di verdezza 2D dovrebbe avere il seguente aspetto:
[[-0.2941,-0.243,-0.5424,0.7276,0.0713,-0.1608]]
Tieni presente che i parametri start
e end
di slice()
corrispondono agli indici dell'asse 0 visualizzati nella tabella (start
è incluso
e end
è escluso).
Array di immagini
Per ottenere un'immagine di verdeggiamento, moltiplica per matrice le bande di un'immagine Landsat 8 con la matrice di verdeggiamento. Per farlo, devi prima convertire l'immagine Landsat multibanda in un'"immagine array", in cui ogni pixel è un Array
di valori di banda. Ad esempio:
Editor di codice (JavaScript)
// Load a Landsat 8 image, select the bands of interest. var image = ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318') .select(['B2', 'B3', 'B4', 'B5', 'B6', 'B7']); // Make an Array Image, with a 1-D Array per pixel. var arrayImage1D = image.toArray(); // Make an Array Image with a 2-D Array per pixel, 6x1. var arrayImage2D = arrayImage1D.toArray(1);
import ee import geemap.core as geemap
Colab (Python)
# Load a Landsat 8 image, select the bands of interest. image = ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318').select( ['B2', 'B3', 'B4', 'B5', 'B6', 'B7'] ) # Make an Array Image, with a 1-D Array per pixel. array_image_1d = image.toArray() # Make an Array Image with a 2-D Array per pixel, 6x1. array_image_2d = array_image_1d.toArray(1)
In questo esempio, tieni presente che toArray()
converte image
in un'immagine array in cui ogni pixel è un vettore 1D le cui voci corrispondono ai 6 valori nelle posizioni corrispondenti delle bande di image
. Un'immagine di array di vettori 1D creata in questo modo non ha il concetto di forma 2D. Per eseguire operazioni solo 2D come la moltiplicazione di matrici, converti l'immagine in un array 2D per pixel con toArray(1)
. In ogni pixel dell'immagine dell'array 2D è presente una matrice 6x1
dei valori delle bande. Per comprendere questo concetto, considera il seguente esempio pratico:
Editor di codice (JavaScript)
var array1D = ee.Array([1, 2, 3]); // [1,2,3] var array2D = ee.Array.cat([array1D], 1); // [[1],[2],[3]]
import ee import geemap.core as geemap
Colab (Python)
array_1d = ee.Array([1, 2, 3]) # [1,2,3] array_2d = ee.Array.cat([array_1d], 1) # [[1],[2],[3]]
Nota che il vettore array1D
varia lungo l'asse 0. Anche la matrice
array2D
lo fa, ma ha una dimensione in più. Chiamare
toArray(1)
sull'immagine dell'array è come chiamare cat(bandVector, 1)
su ogni pixel. Utilizzando l'immagine dell'array 2D, moltiplica a sinistra per un'immagine in cui ogni pixel contiene una matrice 2D di coefficienti di verde:
Editor di codice (JavaScript)
// Do a matrix multiplication: 1x6 times 6x1. // Cast the greenness Array to an Image prior to multiplication. var greennessArrayImage = ee.Image(greenness).matrixMultiply(arrayImage2D);
import ee import geemap.core as geemap
Colab (Python)
# Do a matrix multiplication: 1x6 times 6x1. # Cast the greenness Array to an Image prior to multiplication. greenness_array_image = ee.Image(greenness).matrixMultiply(array_image_2d)
Il risultato è una nuova immagine array in cui ogni pixel è la matrice 1x1 risultante dalla moltiplicazione della matrice di verdezza 1x6 (a sinistra) e della matrice di banda 6x1 (a destra). Per scopi di visualizzazione, converti in un'immagine normale a una banda con arrayGet()
:
Editor di codice (JavaScript)
// Get the result from the 1x1 array in each pixel of the 2-D array image. var greennessImage = greennessArrayImage.arrayGet([0, 0]); // Display the input imagery with the greenness result. Map.setCenter(-122.3, 37.562, 10); Map.addLayer(image, {bands: ['B5', 'B4', 'B3'], min: 0, max: 0.5}, 'image'); Map.addLayer(greennessImage, {min: -0.1, max: 0.13}, 'greenness');
import ee import geemap.core as geemap
Colab (Python)
# Get the result from the 1x1 array in each pixel of the 2-D array image. greenness_image = greenness_array_image.arrayGet([0, 0]) # Display the input imagery with the greenness result. m = geemap.Map() m.set_center(-122.3, 37.562, 10) m.add_layer(image, {'bands': ['B5', 'B4', 'B3'], 'min': 0, 'max': 0.5}, 'image') m.add_layer(greenness_image, {'min': -0.1, 'max': 0.13}, 'greenness') m
Ecco un esempio completo che utilizza l'intero array di coefficienti per calcolare contemporaneamente più componenti del tappo con nappe e visualizzare il risultato:
Editor di codice (JavaScript)
// Define an Array of Tasseled Cap coefficients. var coefficients = ee.Array([ [0.3029, 0.2786, 0.4733, 0.5599, 0.508, 0.1872], [-0.2941, -0.243, -0.5424, 0.7276, 0.0713, -0.1608], [0.1511, 0.1973, 0.3283, 0.3407, -0.7117, -0.4559], [-0.8239, 0.0849, 0.4396, -0.058, 0.2013, -0.2773], [-0.3294, 0.0557, 0.1056, 0.1855, -0.4349, 0.8085], [0.1079, -0.9023, 0.4119, 0.0575, -0.0259, 0.0252], ]); // Load a Landsat 8 image, select the bands of interest. var image = ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318') .select(['B2', 'B3', 'B4', 'B5', 'B6', 'B7']); // Make an Array Image, with a 1-D Array per pixel. var arrayImage1D = image.toArray(); // Make an Array Image with a 2-D Array per pixel, 6x1. var arrayImage2D = arrayImage1D.toArray(1); // Do a matrix multiplication: 6x6 times 6x1. var componentsImage = ee.Image(coefficients) .matrixMultiply(arrayImage2D) // Get rid of the extra dimensions. .arrayProject([0]) .arrayFlatten( [['brightness', 'greenness', 'wetness', 'fourth', 'fifth', 'sixth']]); // Display the first three bands of the result and the input imagery. var vizParams = { bands: ['brightness', 'greenness', 'wetness'], min: -0.1, max: [0.5, 0.1, 0.1] }; Map.setCenter(-122.3, 37.562, 10); Map.addLayer(image, {bands: ['B5', 'B4', 'B3'], min: 0, max: 0.5}, 'image'); Map.addLayer(componentsImage, vizParams, 'components');
import ee import geemap.core as geemap
Colab (Python)
# Define an Array of Tasseled Cap coefficients. coefficients = ee.Array([ [0.3029, 0.2786, 0.4733, 0.5599, 0.508, 0.1872], [-0.2941, -0.243, -0.5424, 0.7276, 0.0713, -0.1608], [0.1511, 0.1973, 0.3283, 0.3407, -0.7117, -0.4559], [-0.8239, 0.0849, 0.4396, -0.058, 0.2013, -0.2773], [-0.3294, 0.0557, 0.1056, 0.1855, -0.4349, 0.8085], [0.1079, -0.9023, 0.4119, 0.0575, -0.0259, 0.0252], ]) # Load a Landsat 8 image, select the bands of interest. image = ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318').select( ['B2', 'B3', 'B4', 'B5', 'B6', 'B7'] ) # Make an Array Image, with a 1-D Array per pixel. array_image_1d = image.toArray() # Make an Array Image with a 2-D Array per pixel, 6x1. array_image_2d = array_image_1d.toArray(1) # Do a matrix multiplication: 6x6 times 6x1. components_image = ( ee.Image(coefficients) .matrixMultiply(array_image_2d) # Get rid of the extra dimensions. .arrayProject([0]) .arrayFlatten( [['brightness', 'greenness', 'wetness', 'fourth', 'fifth', 'sixth']] ) ) # Display the first three bands of the result and the input imagery. viz_params = { 'bands': ['brightness', 'greenness', 'wetness'], 'min': -0.1, 'max': [0.5, 0.1, 0.1], } m = geemap.Map() m.set_center(-122.3, 37.562, 10) m.add_layer(image, {'bands': ['B5', 'B4', 'B3'], 'min': 0, 'max': 0.5}, 'image') m.add_layer(components_image, viz_params, 'components') m
Tieni presente che, quando ottieni bande da un'immagine array, elimina prima le dimensioni aggiuntive
con project()
, quindi convertila di nuovo in un'immagine normale con
arrayFlatten()
. L'output dovrebbe essere simile al seguente:
