الصفائف وصور الصفائف

يتم إنشاء المصفوفات في Earth Engine من قوائم الأرقام وقوائم القوائم. تحدّد درجة التداخل عدد السمات. للبدء بمثال بسيط ومفيد، ننصحك بالاطّلاع على المثال التالي لArray تم إنشاؤه من معاملات غطاء التظليل (TC) في Landsat 8 (Baig et al., 2014):

محرِّر الرموز البرمجية (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 واستخدام IDE 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],
])

تأكَّد من أنّ هذا صفيف ثنائي الأبعاد أبعاده 6×6 باستخدام length()، ما سيؤدي إلى عرض أطوال كل محور:

محرِّر الرموز البرمجية (JavaScript)

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

إعداد لغة Python

اطّلِع على صفحة بيئة Python للحصول على معلومات عن واجهة برمجة التطبيقات Python API واستخدام IDE 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

تشير المؤشرات على يمين الجدول إلى مواضع على طول محور 0. العنصر n ضمن كل قائمة على محور 0 هو في الموضع n على طول محور 1. على سبيل المثال، القيمة في الإحداثي [3,1] من الصفيف هي 0.0849. لنفترض أنّ "الخضرة" هي مكوّن TC المعنيّ. يمكنك الحصول على مصفوفة المساحات الخضراء الفرعية باستخدام slice():

محرِّر الرموز البرمجية (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 واستخدام IDE 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 لقيم النطاقات. على سبيل المثال:

محرِّر الرموز البرمجية (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 واستخدام IDE 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). في كل بكسل من صورة الصفيف ثنائي الأبعاد، تتوفّر مصفوفة 6×1 لقيم النطاقات. للتوضيح، إليك مثال على لعبة:

محرِّر الرموز البرمجية (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 واستخدام IDE 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) في كل بكسل. باستخدام صورة المصفوفة ثنائية الأبعاد، اضرب على يمينها صورة يحتوي كل بكسل فيها على مصفوفة ثنائية الأبعاد لمعاملات الخضرة:

محرِّر الرموز البرمجية (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 واستخدام IDE 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)

والنتيجة هي صورة صفيف جديدة يكون فيها كل بكسل مصفوفة 1×1 تنتج عن ضرب المصفوفة في مصفوفة الخضرة 1×6 (على يمين الشاشة) ومصفوفة النطاق 6×1 (على يسار الشاشة). لأغراض العرض، يمكنك تحويلها إلى صورة عادية أحادية النطاق باستخدام arrayGet():

محرِّر الرموز البرمجية (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 واستخدام IDE 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

في ما يلي مثال كامل يستخدم صفيف المعاملات بالكامل لاحتساب عدة مكوّنات للقبعة ذات الشراريب في آنٍ واحد وعرض النتيجة:

محرِّر الرموز البرمجية (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 واستخدام IDE 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. مكوّنات القبّعة المزيّنة بشرابات: "السطوع" (أحمر)، و"الخضرة" (أخضر)، و"الرطوبة" (أزرق).