نقشه برداری روی ImageCollection

برای اعمال یک تابع به هر Image در ImageCollection از imageCollection.map() استفاده کنید. تنها آرگومان برای map() تابعی است که یک پارامتر دارد: ee.Image . به عنوان مثال، کد زیر به هر تصویر در مجموعه یک نوار زمان اضافه می کند.

ویرایشگر کد (جاوا اسکریپت)

// 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));

راه اندازی پایتون

برای اطلاعات در مورد API پایتون و استفاده از geemap برای توسعه تعاملی به صفحه محیط پایتون مراجعه کنید.

import ee
import geemap.core as geemap

کولب (پایتون)

# 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 جدید از مقدار عددی یک ویژگی استفاده می شود. همانطور که در بخش‌های Reducing و Compositing بحث شد، داشتن باند زمانی برای مدل‌سازی خطی تغییرات و ساخت کامپوزیت‌ها مفید است.

عملکرد نگاشت شده در عملیاتی که می تواند انجام دهد محدود است. به طور خاص، نمی تواند متغیرهای خارج از تابع را تغییر دهد. نمی تواند چیزی را چاپ کند. نمی تواند از جملات «if» یا «for» جاوا اسکریپت و پایتون استفاده کند. با این حال، می توانید از ee.Algorithms.If() برای انجام عملیات شرطی در یک تابع نگاشت شده استفاده کنید. به عنوان مثال:

ویرایشگر کد (جاوا اسکریپت)

// 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));

راه اندازی پایتون

برای اطلاعات در مورد API پایتون و استفاده از geemap برای توسعه تعاملی به صفحه محیط پایتون مراجعه کنید.

import ee
import geemap.core as geemap

کولب (پایتون)

# 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))

لیست تصاویر موجود در ImageCollection خروجی را بررسی کنید و توجه داشته باشید که وقتی شرط ارزیابی شده توسط الگوریتم If() درست باشد، خروجی حاوی یک تصویر ثابت است. اگرچه این یک تابع شرطی سمت سرور را نشان می دهد ( در مورد مشتری در مقابل سرور در Earth Engine بیشتر بیاموزید )، به طور کلی از If() اجتناب کنید و به جای آن از فیلترها استفاده کنید.