التعيين على ImageCollection

لتطبيق دالة على كل Image في ImageCollection، استخدِم imageCollection.map(). الوسيطة الوحيدة للدالة map() هي دالة تأخذ مَعلمة واحدة: ee.Image. على سبيل المثال، يضيف الرمز التالي شريط طابع زمني إلى كل صورة في المجموعة.

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

// Load a Landsat 8 collection for a single path-row, 2021 images only.
var collection = ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA')
  .filterDate('2021', '2022')
  .filter(ee.Filter.eq('WRS_PATH', 44))
  .filter(ee.Filter.eq('WRS_ROW', 34));

// This function adds a band representing the image timestamp.
var addTime = function(image) {
  return image.addBands(image.getNumber('system:time_start'));
};

// Map the function over the collection and display the result.
print(collection.map(addTime));

إعداد لغة Python

اطّلِع على صفحة بيئة Python للحصول على معلومات عن واجهة برمجة التطبيقات Python API واستخدام IDE geemap لتطوير التطبيقات التفاعلي.

import ee
import geemap.core as geemap

Colab (Python)

# Load a Landsat 8 collection for a single path-row, 2021 images only.
collection = (
    ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA')
    .filterDate('2021', '2022')
    .filter(ee.Filter.eq('WRS_PATH', 44))
    .filter(ee.Filter.eq('WRS_ROW', 34))
)


# This function adds a band representing the image timestamp.
def add_time(image):
  return image.addBands(image.getNumber('system:time_start'))


# Map the function over the collection and display the result.
display(collection.map(add_time))

يُرجى العلم أنّه في الدالة المحدّدة مسبقًا، يتم استخدام طريقة getNumber() لإنشاء Image جديد من القيمة الرقمية لسمة. كما هو موضّح في القسمَين التقليل و التركيب ، يكون استخدام النطاق الزمني مفيدًا لوضع نماذج خطية للتغيير ولعمل رسومات مركبة.

تكون الوظيفة المُحدَّدة محدودة في العمليات التي يمكنها تنفيذها. على وجه التحديد، لا يمكنه تعديل المتغيّرات خارج الدالة، ولا يمكنه طباعة أي شيء، ولا يمكنه استخدام JavaScript وعبارة Python "if" أو "for". ومع ذلك، يمكنك استخدام ee.Algorithms.If() للقيام بعمليات شرطية في دالة منسَّقة. على سبيل المثال:

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

// Load a Landsat 8 collection for a single path-row, 2021 images only.
var collection = ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA')
  .filterDate('2021', '2022')
  .filter(ee.Filter.eq('WRS_PATH', 44))
  .filter(ee.Filter.eq('WRS_ROW', 34));

// This function uses a conditional statement to return the image if
// the solar elevation > 40 degrees. Otherwise it returns a "zero image".
var conditional = function(image) {
  return ee.Algorithms.If(ee.Number(image.get('SUN_ELEVATION')).gt(40),
                          image,
                          ee.Image(0));
};

// Map the function over the collection and print the result. Expand the
// collection and note that 7 of the 22 images are now "zero images'.
print('Expand this to see the result', collection.map(conditional));

إعداد لغة Python

اطّلِع على صفحة بيئة Python للحصول على معلومات عن واجهة برمجة التطبيقات Python API واستخدام IDE geemap لتطوير التطبيقات التفاعلي.

import ee
import geemap.core as geemap

Colab (Python)

# Load a Landsat 8 collection for a single path-row, 2021 images only.
collection = (
    ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA')
    .filterDate('2021', '2022')
    .filter(ee.Filter.eq('WRS_PATH', 44))
    .filter(ee.Filter.eq('WRS_ROW', 34))
)


# This function uses a conditional statement to return the image if
# the solar elevation > 40 degrees. Otherwise it returns a "zero image".
def conditional(image):
  return ee.Algorithms.If(
      ee.Number(image.get('SUN_ELEVATION')).gt(40), image, ee.Image(0)
  )


# Map the function over the collection and print the result. Expand the
# collection and note that 7 of the 22 images are now "zero images'.
display('Expand this to see the result', collection.map(conditional))

تحقَّق من قائمة الصور في مجموعة الصور الناتجة، وتجدُر الإشارة إلى أنّه عندما يكون الشرط الذي تقيّمه خوارزمية If() صحيحًا، يحتوي الناتج على صورة ثابتة. على الرغم من أنّ هذا يوضّح دالة شرطية من جهة الخادم (مزيد من المعلومات عن استخدام وظائف العميل مقابل الخادم في Earth Engine)، تجنَّب استخدام If() بشكل عام واستخدِم الفلاتر بدلاً من ذلك.