ImageCollection をマッピングする

ImageCollection 内のすべての Image に関数を適用するには、imageCollection.map() を使用します。map() の唯一の引数は、1 つのパラメータ(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 API とインタラクティブな開発で geemap を使用する方法については、 Python 環境のページをご覧ください。

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 API とインタラクティブな開発で geemap を使用する方法については、 Python 環境のページをご覧ください。

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() アルゴリズムによって評価された条件が true の場合、出力には定数画像が含まれています。これはサーバーサイドの条件関数を示していますが(Earth Engine のクライアントとサーバーについて)、通常は If() を使用しないで、代わりにフィルタを使用します。