การลด 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() เช่น หากต้องการคํานวณแนวโน้มเชิงเส้นระยะยาวในคอลเล็กชัน ให้ใช้ตัวลดขนาดการถดถอยเชิงเส้นอย่างใดอย่างหนึ่ง โค้ดต่อไปนี้จะคํานวณแนวโน้มเชิงเส้นของดัชนีการปกคลุมพืชพรรณที่ปรับปรุงแล้ว (EVI) ของ MODIS

เครื่องมือแก้ไขโค้ด (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

โปรดทราบว่าเอาต์พุตของการลดขนาดในตัวอย่างนี้คือรูปภาพ 2 แถบ โดยมีแถบ 1 แถบสำหรับความชันของการถดถอยเชิงเส้น (scale) และแถบ 1 แถบสำหรับค่าคงที่ (offset) สำรวจเอกสารประกอบของ API เพื่อดูรายการตัวลดขนาดที่ใช้ได้เพื่อลด ImageCollection ให้เป็น Image รายการเดียว

คอมโพสิตไม่มีการฉายภาพ

รูปภาพคอมโพสิตที่สร้างโดยการลดคอลเล็กชันรูปภาพจะสร้างพิกเซลในโปรเจ็กชันที่ขอได้ ดังนั้นจึงไม่มีโปรเจ็กชันเอาต์พุตแบบคงที่ แต่ภาพรวมจะมีการฉายภาพเริ่มต้นของ WGS-84 ด้วยพิกเซลความละเอียด 1 องศา ระบบจะคำนวณคอมโพสิทที่มีโปรเจ็กชันเริ่มต้นในโปรเจ็กชันเอาต์พุตที่ขอ คำขอเกิดขึ้นเมื่อแสดงคอมโพสิตในเครื่องมือแก้ไขโค้ด (ดูวิธีตั้งค่ามาตราส่วนและการฉายภาพของเครื่องมือแก้ไขโค้ด) หรือเมื่อระบุการฉายภาพ/มาตราส่วนอย่างชัดเจน เช่น ReduceRegion หรือ Export ในการรวม