Untuk menerapkan fungsi ke setiap Image
dalam ImageCollection
, gunakan
imageCollection.map()
. Satu-satunya argumen untuk map()
adalah
fungsi yang menggunakan satu parameter: ee.Image
. Misalnya, kode berikut menambahkan band stempel waktu ke setiap gambar dalam koleksi.
Editor Kode (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));
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))
Perhatikan bahwa dalam fungsi standar, metode getNumber()
digunakan
untuk membuat Image
baru dari nilai numerik properti. Seperti yang dibahas di
bagian Mengurangi dan
Membuat Komposisi, memiliki rentang waktu berguna untuk pemodelan linear perubahan dan untuk membuat
komposit.
Fungsi yang dipetakan dibatasi dalam operasi yang dapat dilakukannya. Secara khusus, fungsi ini tidak dapat
mengubah variabel di luar fungsi; tidak dapat mencetak apa pun; tidak dapat menggunakan pernyataan 'if' atau 'for' JavaScript dan
Python. Namun, Anda dapat menggunakan ee.Algorithms.If()
untuk
melakukan operasi kondisional dalam fungsi yang dipetakan. Contoh:
Editor Kode (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));
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))
Periksa daftar gambar dalam ImageCollection output dan perhatikan bahwa saat
kondisi yang dievaluasi oleh algoritma If()
bernilai benar, output akan berisi
gambar konstan. Meskipun ini menunjukkan fungsi bersyarat sisi server (pelajari lebih lanjut klien vs. server di Earth Engine), hindari If()
secara umum dan gunakan filter.