如要在 ImageCollection
中合成圖片,請使用 imageCollection.reduce()
。這會將集合中的所有圖片合成單一圖片,用來代表圖片的極小值、極大值、平均值或標準差。(如要進一步瞭解 Reducer,請參閱「Reducer 一節」)。例如,如要從集合建立中位值圖片:
程式碼編輯器 (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');
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');
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');
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
),另一個帶狀圖像代表截距 (offset
)。請參閱 API 說明文件,查看可用縮減器的清單,以便將 ImageCollection
縮減為單一 Image
。
複合物沒有投影
透過縮減圖片集合所建立的複合圖片,可產生任何要求的專案中的像素,因此沒有固定的輸出投影方式。相反地,合成圖層具有 1 度解析度像素的 預設投影方式 WGS-84。使用預設投影的組合會在要求的任何輸出投影中計算。您可以在程式碼編輯器中顯示複合圖 (瞭解程式碼編輯器如何設定縮放和投影),或是在匯總 (例如 ReduceRegion
或 Export
) 中明確指定投影/縮放。