Bir ImageCollection üzerinde eşleme

Bir ImageCollection içindeki her Image öğesine işlev uygulamak için imageCollection.map()'yi kullanın. map() işlevinin tek bağımsız değişkeni, bir parametre alan bir işlevdir: ee.Image. Örneğin, aşağıdaki kod, koleksiyondaki her resme bir zaman damgası bandı ekler.

Kod Düzenleyici (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 kurulumu

Python API'si ve etkileşimli geliştirme için geemap kullanımı hakkında bilgi edinmek üzere Python Ortamı sayfasına bakın.

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

Önceden tanımlanmış işlevde, bir mülkün sayısal değerinden yeni bir Image oluşturmak için getNumber() yönteminin kullanıldığını unutmayın. Azaltma ve Birleştirme bölümlerinde belirtildiği gibi, zaman aralığına sahip olmak, değişimin doğrusal modellenmesi ve birleştirmeler oluşturmak için yararlıdır.

Eşlenen işlev, gerçekleştirebileceği işlemler açısından sınırlıdır. Daha açık belirtmek gerekirse, işlevin dışındaki değişkenleri değiştiremez, hiçbir şey yazdıramaz, JavaScript ve Python "if" veya "for" ifadelerini kullanamaz. Ancak, eşlenmiş bir işlevde koşullu işlemler yapmak için ee.Algorithms.If() kullanabilirsiniz. Örneğin:

Kod Düzenleyici (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 kurulumu

Python API'si ve etkileşimli geliştirme için geemap kullanımı hakkında bilgi edinmek üzere Python Ortamı sayfasına bakın.

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

Çıkıştaki ImageCollection'daki resim listesini inceleyin ve If() algoritması tarafından değerlendirilen koşul doğru olduğunda çıkışın sabit bir resim içerdiğini unutmayın. Bu örnekte sunucu tarafı koşullu işlev gösterilmektedir (Earth Engine'da istemci ve sunucu hakkında daha fazla bilgi edinin). Genel olarak If() işlevini kullanmaktan kaçının ve bunun yerine filtreleri kullanın.