ImageCollection の削減

ImageCollection で画像を合成するには、imageCollection.reduce() を使用します。これにより、コレクション内のすべての画像が合成され、画像の最小値、最大値、平均値、標準偏差などを表す単一の画像が作成されます。(リデューサーの詳細については、リデューサーのセクションをご覧ください)。たとえば、コレクションから中央値画像を作成するには:

コードエディタ(JavaScript)

// Load a Landsat 8 collection for a single path-row.
var collection = ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA')
    .filter(ee.Filter.eq('WRS_PATH', 44))
    .filter(ee.Filter.eq('WRS_ROW', 34))
    .filterDate('2014-01-01', '2015-01-01');

// Compute a median image and display.
var median = collection.median();
Map.setCenter(-122.3578, 37.7726, 12);
Map.addLayer(median, {bands: ['B4', 'B3', 'B2'], max: 0.3}, 'Median');

Python の設定

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

import ee
import geemap.core as geemap

Colab(Python)

# Load a Landsat 8 collection for a single path-row.
collection = (
    ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA')
    .filter(ee.Filter.eq('WRS_PATH', 44))
    .filter(ee.Filter.eq('WRS_ROW', 34))
    .filterDate('2014-01-01', '2015-01-01')
)

# Compute a median image and display.
median = collection.median()
m = geemap.Map()
m.set_center(-122.3578, 37.7726, 12)
m.add_layer(median, {'bands': ['B4', 'B3', 'B2'], 'max': 0.3}, 'Median')
m

出力画像の各位置、各バンドのピクセル値は、入力画像(コレクション内の画像)のマスクされていないすべてのピクセルの中央値です。上の例では、median() は次の呼び出しの便利なメソッドです。

コードエディタ(JavaScript)

// Reduce the collection with a median reducer.
var median = collection.reduce(ee.Reducer.median());

// Display the median image.
Map.addLayer(median,
             {bands: ['B4_median', 'B3_median', 'B2_median'], max: 0.3},
             'Also median');

Python の設定

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

import ee
import geemap.core as geemap

Colab(Python)

# Reduce the collection with a median reducer.
median = collection.reduce(ee.Reducer.median())

# Display the median image.
m.add_layer(
    median,
    {'bands': ['B4_median', 'B3_median', 'B2_median'], 'max': 0.3},
    'Also median',
)
m

コンビニエンス メソッドではなく reduce() を使用しているため、バンド名が異なります。具体的には、バンド名にレジューサーの名前が追加されています。

reduce() を使用すると、より複雑な減算も可能です。たとえば、コレクションの長期的な線形傾向を計算するには、線形回帰リデューサーのいずれかを使用します。次のコードは、MODIS 強化植生指数(EVI)の線形傾向を計算します。

コードエディタ(JavaScript)

// This function adds a band representing the image timestamp.
var addTime = function(image) {
  return image.addBands(image.metadata('system:time_start')
    // Convert milliseconds from epoch to years to aid in
    // interpretation of the following trend calculation.
    .divide(1000 * 60 * 60 * 24 * 365));
};

// Load a MODIS collection, filter to several years of 16 day mosaics,
// and map the time band function over it.
var collection = ee.ImageCollection('MODIS/006/MYD13A1')
  .filterDate('2004-01-01', '2010-10-31')
  .map(addTime);

// Select the bands to model with the independent variable first.
var trend = collection.select(['system:time_start', 'EVI'])
  // Compute the linear trend over time.
  .reduce(ee.Reducer.linearFit());

// Display the trend with increasing slopes in green, decreasing in red.
Map.setCenter(-96.943, 39.436, 5);
Map.addLayer(
    trend,
    {min: 0, max: [-100, 100, 10000], bands: ['scale', 'scale', 'offset']},
    'EVI trend');

Python の設定

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

import ee
import geemap.core as geemap

Colab(Python)

# This function adds a band representing the image timestamp.
def add_time(image):
  return image.addBands(
      image.metadata('system:time_start')
      # Convert milliseconds from epoch to years to aid in
      # interpretation of the following trend calculation.
      .divide(1000 * 60 * 60 * 24 * 365)
  )


# Load a MODIS collection, filter to several years of 16 day mosaics,
# and map the time band function over it.
collection = (
    ee.ImageCollection('MODIS/006/MYD13A1')
    .filterDate('2004-01-01', '2010-10-31')
    .map(add_time)
)

# Select the bands to model with the independent variable first.
trend = collection.select(['system:time_start', 'EVI']).reduce(
    # Compute the linear trend over time.
    ee.Reducer.linearFit()
)

# Display the trend with increasing slopes in green, decreasing in red.
m.set_center(-96.943, 39.436, 5)
m = geemap.Map()
m.add_layer(
    trend,
    {
        'min': 0,
        'max': [-100, 100, 10000],
        'bands': ['scale', 'scale', 'offset'],
    },
    'EVI trend',
)
m

この例のリデューションの出力は、線形回帰の傾斜(scale)に 1 つのバンド、切片(offset)に 1 つのバンドを持つ 2 バンド画像です。ImageCollection を単一の Image に減らすために使用できるリデューサーのリストについては、API ドキュメントをご覧ください。

複合体には投影がない

画像コレクションを縮小して作成された合成画像は、リクエストされた任意の投影でピクセルを生成できるため、固定の出力投影はありません。代わりに、合成には 1 度の解像度のピクセルで WGS-84 のデフォルトの投影が使用されます。デフォルトの投影を使用したコンポジットは、リクエストされた出力投影で計算されます。リクエストは、Code Editor で合成を表示するか(Code Editor でスケール投影を設定する方法を学習する)、ReduceRegionExport などの集計で投影/スケールを明示的に指定することで行われます。