برای اعمال تابع به هر 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));
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 جدید از مقدار عددی دارایی استفاده میشود. همانطور که در بخشهای کاهش و ترکیببندی بحث شد، داشتن باند زمانی برای مدلسازی خطی تغییر و برای ساختن ترکیبها مفید است.
تابع نگاشتشده در عملیاتی که میتواند انجام دهد محدود است. بهطور مشخص، نمیتواند متغیرهای خارج از تابع را اصلاح کند؛ نمیتواند چیزی را چاپ کند؛ نمیتواند از دستورهای «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));
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))
فهرست تصاویر را در ImageCollection خروجی بررسی کنید و توجه داشته باشید که وقتی شرط ارزیابیشده توسط الگوریتم If() درست باشد، خروجی حاوی یک تصویر ثابت است. اگرچه این یک تابع شرطی سمت سرور را نشان میدهد
(درباره مشتری درمقابل سرور در Earth Engine بیشتر بدانید)،
بهطور کلی از If() اجتناب کنید و بهجای آن از فیلترها استفاده کنید.