מערכי תמונות ומערכי תמונות

מערכים ב-Earth Engine מורכבים מרשימות של מספרים ורשימות של רשימות. מידת ההטמעה קובעת את מספר המאפיינים. כדי להתחיל עם דוגמה פשוטה וממוקדת, נבחן את הדוגמה הבאה של Array שנוצרה מגורמי כובע מעוטר (TC) של Landsat 8‏ (Baig et al., 2014):

Code Editor‏ (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],
]);

הגדרת Python

בדף סביבת Python מפורט מידע על Python API ועל השימוש ב-geemap לפיתוח אינטראקטיבי.

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

מוודאים שמדובר במערך דו-מימדי בגודל 6x6 באמצעות length(), שתחזיר את האורך של כל ציר:

Code Editor‏ (JavaScript)

// Print the dimensions.
print(coefficients.length()); //    [6,6]

הגדרת Python

בדף סביבת Python מפורט מידע על Python API ועל השימוש ב-geemap לפיתוח אינטראקטיבי.

import ee
import geemap.core as geemap

Colab (Python)

# Print the dimensions.
display(coefficients.length())  #    [6,6]

בטבלה הבאה מוצגת ההסדרה של הרשומות במטריצה לאורך ציר 0 ולציר 1:

ציר אחד ->
012345
00.30290.27860.47330.55990.5080.1872
1‎-0.2941‎-0.243‎-0.54240.72760.0713‎-0.1608
ציר 020.15110.19730.32830.3407‎-0.7117‎-0.4559
3‎-0.82390.08490.4396‎-0.0580.2013‎-0.2773
4‎-0.32940.05570.10560.1855‎-0.43490.8085
50.1079-0.90230.41190.0575‎-0.02590.0252

המדדים שמשמאל לטבלה מציינים את המיקומים לאורך ציר האפס. הרכיב ה-n בכל רשימה בציר 0 נמצא במיקום ה-n בציר 1. לדוגמה, הערך ב-[3,1] של המערך הוא 0.0849. נניח ש'ירוק' הוא רכיב ה-TC הרצוי. אפשר לקבל את מטריצת המשנה של הצבע הירוק באמצעות slice():

Code Editor‏ (JavaScript)

// Get the 1x6 greenness slice, display it.
var greenness = coefficients.slice({axis: 0, start: 1, end: 2, step: 1});
print(greenness);

הגדרת Python

בדף סביבת Python מפורט מידע על Python API ועל השימוש ב-geemap לפיתוח אינטראקטיבי.

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)

מטריצת הגוון הירוק הדו-מימדית אמורה להיראות בערך כך:

[[-0.2941,-0.243,-0.5424,0.7276,0.0713,-0.1608]]
    

שימו לב שהפרמטרים start ו-end של slice() תואמים למדדים של ציר 0 שמוצגים בטבלה (start כולל ו-end לא כולל).

תמונות של מערך

כדי לקבל תמונה של רמת הירוקת, מכפילים את הרצועות של תמונה מ-Landsat 8 במטריצה של רמת הירוקת. כדי לעשות זאת, קודם צריך להמיר את התמונה של Landsat עם מספר הפסים ל'תמונה של מערך', שבה כל פיקסל הוא Array של ערכי הפס. לדוגמה:

Code Editor‏ (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);

הגדרת Python

בדף סביבת Python מפורט מידע על Python API ועל השימוש ב-geemap לפיתוח אינטראקטיבי.

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)

בדוגמה הזו, שימו לב ש-toArray() ממירה את image לתמונה של מערך שבה כל פיקסל הוא וקטור דו-מימדי, שהרשומות שלו תואמות ל-6 הערכים במיקומים התואמים בפסגות של image. לתמונה של מערך וקטורים דו-ממדיים שנוצרת באופן הזה אין מושג של צורה דו-ממדית. כדי לבצע פעולות דו-מימדיות בלבד, כמו כפל מטריצות, צריך להמיר את התמונה למערך דו-מימדי לכל פיקסל באמצעות toArray(1). בכל פיקסל של התמונה במערך דו-מימדי יש מטריקס של 6x1 של ערכי הלהקות. כדי להבין את זה, נבחן את הדוגמה הבאה:

Code Editor‏ (JavaScript)

var array1D = ee.Array([1, 2, 3]);              // [1,2,3]
var array2D = ee.Array.cat([array1D], 1);     // [[1],[2],[3]]

הגדרת Python

בדף סביבת Python מפורט מידע על Python API ועל השימוש ב-geemap לפיתוח אינטראקטיבי.

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

שימו לב שהוקטור array1D משתנה לאורך ציר 0. גם המטריצה array2D עומדת בקריטריונים האלה, אבל יש לה מאפיין נוסף. קריאה ל-toArray(1) בתמונה של המערך היא כמו קריאה ל-cat(bandVector, 1) בכל פיקסל. באמצעות התמונה של מערך דו-מימדי, מכפילים בתמונה שבה כל פיקסל מכיל מטריצת 2-D של מקדם ירוק:

Code Editor‏ (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);

הגדרת Python

בדף סביבת Python מפורט מידע על Python API ועל השימוש ב-geemap לפיתוח אינטראקטיבי.

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)

התוצאה היא תמונה חדשה של מערך, שבה כל פיקסל הוא המטריצה 1x1 שמתקבלת מהכפלת המטריצה של הצבע הירוק בגודל 1x6 (שמשמאל) במטריצה של התדר בגודל 6x1 (שמשמאל). למטרות תצוגה, ממירים לתמונה רגילה עם פס אחד באמצעות arrayGet():

Code Editor‏ (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');

הגדרת Python

בדף סביבת Python מפורט מידע על Python API ועל השימוש ב-geemap לפיתוח אינטראקטיבי.

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

זו דוגמה מלאה שבה נעשה שימוש במערך הקואורדינטות כולו כדי לחשב כמה רכיבים של כובע עם סרטים בבת אחת ולהציג את התוצאה:

Code Editor‏ (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');

הגדרת Python

בדף סביבת Python מפורט מידע על Python API ועל השימוש ב-geemap לפיתוח אינטראקטיבי.

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

שימו לב שכדי לקבל פסות מתמונה של מערך, קודם צריך להסיר את המאפיינים הנוספים באמצעות project(), ואז להמיר אותה חזרה לתמונה רגילה באמצעות arrayFlatten(). הפלט אמור להיראות כך:

תמונה של כובע עם פס קטן
איור 1. רכיבי כובע עם סרטים: 'בהירות' (אדום), 'ירוק' (ירוק) ו'רטיבות' (כחול).