缩减 ImageCollection

如需在 ImageCollection 中合成图片,请使用 imageCollection.reduce()。这会将集合中的所有图片合成到单张图片中,例如表示图片的最小值、最大值、平均值或标准差。 (如需详细了解 reducer,请参阅“Reducer”部分)。例如,如需根据集合创建中位数值图片,请执行以下操作:

// 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 API 以及如何使用 geemap 进行交互式开发,请参阅 Python 环境页面。

import ee
import geemap.core as geemap
# 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() 是以下调用的便捷方法:

// 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 API 以及如何使用 geemap 进行交互式开发,请参阅 Python 环境页面。

import ee
import geemap.core as geemap
# 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() 而非便捷方法,因此频段名称不同。具体而言,我们已将 reducer 的名称附加到乐队名称中。

您还可以使用 reduce() 进行更复杂的缩减。例如,如需计算集合中的长期线性趋势,请使用线性回归归约器之一。以下代码计算 MODIS 增强型植被指数 (EVI) 的线性趋势:

// 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 API 以及如何使用 geemap 进行交互式开发,请参阅 Python 环境页面。

import ee
import geemap.core as geemap
# 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),另一个带状用于表示截距 (offset)。请浏览 API 文档,查看可用于将 ImageCollection 化简为单个 Image 的求和函数的列表。

复合体没有投影

通过缩减图片集合创建的复合图片能够在任何请求的投影中生成像素,因此没有固定的输出投影。 而是采用 WGS-84 的默认投影,分辨率为 1 度像素。系统会使用请求的任何输出投影来计算采用默认投影的复合项。请求的发生方式包括在代码编辑器中显示复合项(了解代码编辑器如何设置比例投影),或在汇总(例如 ReduceRegionExport)中明确指定投影/比例。