การสุ่มตัวอย่างอีกครั้งและการลดความละเอียด

ตามที่ระบุไว้ในเอกสารการฉายภาพ Earth Engine จะทำการแจกแจงตัวอย่างจากเพื่อนบ้านที่อยู่ใกล้ที่สุดโดยค่าเริ่มต้นในระหว่างการฉายภาพใหม่ คุณเปลี่ยนลักษณะการทำงานนี้ได้โดยใช้เมธอด resample() หรือ reduceResolution() กล่าวโดยละเอียดคือ เมื่อใช้วิธีการใดวิธีหนึ่งเหล่านี้กับรูปภาพอินพุต ระบบจะทำการโปรเจ็กต์อินพุตอีกครั้งที่จำเป็นโดยใช้วิธีการสุ่มตัวอย่างใหม่หรือการรวมที่ระบุ

การสุ่มตัวอย่างอีกครั้ง

resample() ทําให้ระบบใช้วิธีการสุ่มตัวอย่างอีกครั้งที่ระบุ ('bilinear' หรือ 'bicubic') ในการโปรเจ็กต์ใหม่ครั้งถัดไป เนื่องจากมีการขออินพุตในโปรเจ็กชันเอาต์พุต การโปรเจ็กต์ใหม่โดยนัยอาจเกิดขึ้นก่อนการดำเนินการอื่นๆ กับอินพุต ด้วยเหตุนี้ ให้เรียกใช้ resample() โดยตรงในรูปภาพอินพุต ลองดูตัวอย่างง่ายๆ ต่อไปนี้

เครื่องมือแก้ไขโค้ด (JavaScript)

// Load a Landsat image over San Francisco, California, UAS.
var landsat = ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_044034_20160323');

// Set display and visualization parameters.
Map.setCenter(-122.37383, 37.6193, 15);
var visParams = {bands: ['B4', 'B3', 'B2'], max: 0.3};

// Display the Landsat image using the default nearest neighbor resampling.
// when reprojecting to Mercator for the Code Editor map.
Map.addLayer(landsat, visParams, 'original image');

// Force the next reprojection on this image to use bicubic resampling.
var resampled = landsat.resample('bicubic');

// Display the Landsat image using bicubic resampling.
Map.addLayer(resampled, visParams, 'resampled');

การตั้งค่า Python

ดูข้อมูลเกี่ยวกับ Python API และการใช้ geemap สําหรับการพัฒนาแบบอินเทอร์แอกทีฟได้ที่หน้า สภาพแวดล้อม Python

import ee
import geemap.core as geemap

Colab (Python)

# Load a Landsat image over San Francisco, California, UAS.
landsat = ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_044034_20160323')

# Set display and visualization parameters.
m = geemap.Map()
m.set_center(-122.37383, 37.6193, 15)
vis_params = {'bands': ['B4', 'B3', 'B2'], 'max': 0.3}

# Display the Landsat image using the default nearest neighbor resampling.
# when reprojecting to Mercator for the Code Editor map.
m.add_layer(landsat, vis_params, 'original image')

# Force the next reprojection on this image to use bicubic resampling.
resampled = landsat.resample('bicubic')

# Display the Landsat image using bicubic resampling.
m.add_layer(resampled, vis_params, 'resampled')

โปรดทราบว่า'bicubic' การสุ่มตัวอย่างใหม่ทำให้พิกเซลเอาต์พุตดูเรียบเนียนเมื่อเทียบกับรูปภาพต้นฉบับ (รูปที่ 1)

เพื่อนบ้านที่ใกล้ที่สุด
รูปที่ 1ก ภาพ Landsat ที่ทำการสุ่มตัวอย่างอีกครั้งด้วยวิธีการ Nearest Neighbor
2 คูณ 2
รูปที่ 1ข. ภาพ Landsat ที่ทำการแจกแจงใหม่ด้วยการแจกแจงแบบ 2 คูณ 2

ลําดับการดําเนินการของตัวอย่างโค้ดนี้มีแผนภาพแสดงอยู่ในรูปที่ 2 กล่าวโดยละเอียดคือ การโปรเจ็กต์ใหม่โดยนัยไปยังการโปรเจ็กต์ maps mercator จะเกิดขึ้นด้วยวิธีการสุ่มตัวอย่างอีกครั้งที่ระบุไว้ในรูปภาพอินพุต

โฟลว์ชาร์ตของการดำเนินการ

รูปที่ 2 แผนภาพขั้นตอนการทำงานเมื่อเรียกใช้ resample() ในรูปภาพอินพุตก่อนที่จะแสดงในตัวแก้ไขโค้ด เส้นโค้งแสดงการไหลของข้อมูลไปยังการฉายภาพใหม่ โดยเฉพาะการฉายภาพเอาต์พุต สเกล และวิธีการสุ่มตัวอย่างอีกครั้งที่จะใช้

ลดความละเอียด

สมมติว่าเป้าหมายของคุณคือการรวมพิกเซลให้เป็นพิกเซลขนาดใหญ่ขึ้นในโปรเจ็กชันอื่นแทนการสุ่มตัวอย่างใหม่ระหว่างการโปรเจ็กต์ใหม่ ซึ่งมีประโยชน์เมื่อเปรียบเทียบชุดข้อมูลรูปภาพในระดับต่างๆ เช่น พิกเซล 30 เมตรจากผลิตภัณฑ์ที่อิงตาม Landsat กับพิกเซลหยาบ (ระดับที่สูงขึ้น) จากผลิตภัณฑ์ที่อิงตาม MODIS คุณควบคุมกระบวนการรวบรวมข้อมูลนี้ได้ด้วยreduceResolution()วิธีนี้ เช่นเดียวกับ resample() ให้เรียกใช้ reduceResolution() ในอินพุตเพื่อที่จะส่งผลต่อการฉายภาพใหม่ครั้งถัดไป ตัวอย่างต่อไปนี้ใช้ reduceResolution() เพื่อเปรียบเทียบข้อมูลการปกคลุมป่าที่ความละเอียด 30 เมตรกับดัชนีพืชพันธุ์ที่ความละเอียด 500 เมตร

เครื่องมือแก้ไขโค้ด (JavaScript)

// Load a MODIS EVI image.
var modis = ee.Image(ee.ImageCollection('MODIS/061/MOD13A1').first())
    .select('EVI');

// Display the EVI image near La Honda, California.
Map.setCenter(-122.3616, 37.5331, 12);
Map.addLayer(modis, {min: 2000, max: 5000}, 'MODIS EVI');

// Get information about the MODIS projection.
var modisProjection = modis.projection();
print('MODIS projection:', modisProjection);

// Load and display forest cover data at 30 meters resolution.
var forest = ee.Image('UMD/hansen/global_forest_change_2023_v1_11')
    .select('treecover2000');
Map.addLayer(forest, {max: 80}, 'forest cover 30 m');

// Get the forest cover data at MODIS scale and projection.
var forestMean = forest
    // Force the next reprojection to aggregate instead of resampling.
    .reduceResolution({
      reducer: ee.Reducer.mean(),
      maxPixels: 1024
    })
    // Request the data at the scale and projection of the MODIS image.
    .reproject({
      crs: modisProjection
    });

// Display the aggregated, reprojected forest cover data.
Map.addLayer(forestMean, {max: 80}, 'forest cover at MODIS scale');

การตั้งค่า Python

ดูข้อมูลเกี่ยวกับ Python API และการใช้ geemap สําหรับการพัฒนาแบบอินเทอร์แอกทีฟได้ที่หน้า สภาพแวดล้อม Python

import ee
import geemap.core as geemap

Colab (Python)

# Load a MODIS EVI image.
modis = ee.Image(ee.ImageCollection('MODIS/006/MOD13A1').first()).select('EVI')

# Display the EVI image near La Honda, California.
m.set_center(-122.3616, 37.5331, 12)
m.add_layer(modis, {'min': 2000, 'max': 5000}, 'MODIS EVI')

# Get information about the MODIS projection.
modis_projection = modis.projection()
display('MODIS projection:', modis_projection)

# Load and display forest cover data at 30 meters resolution.
forest = ee.Image('UMD/hansen/global_forest_change_2015').select(
    'treecover2000'
)
m.add_layer(forest, {'max': 80}, 'forest cover 30 m')

# Get the forest cover data at MODIS scale and projection.
forest_mean = (
    forest
    # Force the next reprojection to aggregate instead of resampling.
    .reduceResolution(reducer=ee.Reducer.mean(), maxPixels=1024)
    # Request the data at the scale and projection of the MODIS image.
    .reproject(crs=modis_projection)
)

# Display the aggregated, reprojected forest cover data.
m.add_layer(forest_mean, {'max': 80}, 'forest cover at MODIS scale')

ในตัวอย่างนี้ โปรดทราบว่ามีการตั้งค่าการโปรเจ็กต์เอาต์พุตอย่างชัดเจนด้วย reproject() ในระหว่างการโปรเจ็กต์ใหม่เป็นโปรเจ็กต์ไซน์ไซด์ของ MODIS ระบบจะรวมพิกเซลขนาดเล็กเข้าด้วยกันด้วยตัวลดที่ระบุ (ee.Reducer.mean() ในตัวอย่าง) แทนที่จะทำการสุ่มตัวอย่างใหม่ ลําดับการดําเนินการนี้แสดงอยู่ในรูปที่ 3 แม้ว่าตัวอย่างนี้จะใช้ reproject() เพื่อช่วยแสดงผลของ reduceResolution() แต่สคริปต์ส่วนใหญ่ไม่จำเป็นต้องเปลี่ยนการโปรเจ็กต์อีกครั้งอย่างชัดเจน โปรดดูคำเตือนที่นี่

โฟลว์ชาร์ตของการดำเนินการ

รูปที่ 3 โฟลว์ชาร์ตของการดำเนินการเมื่อมีการเรียกใช้ reduceResolution() ในรูปภาพอินพุตก่อน reproject() เส้นโค้งแสดงการไหลของข้อมูลไปยังการฉายภาพใหม่ โดยเฉพาะการฉายภาพเอาต์พุต สเกล และวิธีการรวมพิกเซลที่จะใช้

อย่างชัดเจน

น้ำหนักพิกเซลสำหรับ ReduceResolution

น้ำหนักของพิกเซลที่ใช้ในระหว่างreduceResolution()กระบวนการรวบรวมข้อมูลจะขึ้นอยู่กับการซ้อนทับระหว่างพิกเซลขนาดเล็กที่รวบรวมและพิกเซลขนาดใหญ่ที่ระบุโดยโปรเจ็กชันเอาต์พุต ดังที่แสดงในรูปที่ 4

พิกเซลอินพุตและเอาต์พุต

รูปที่ 4 พิกเซลอินพุต (สีดํา) และพิกเซลเอาต์พุต (สีน้ำเงิน) สําหรับ reduceResolution()

ลักษณะการทํางานเริ่มต้นคือระบบจะคํานวณน้ำหนักพิกเซลอินพุตเป็นเศษส่วนของพื้นที่พิกเซลเอาต์พุตที่พิกเซลอินพุตครอบคลุม ในแผนภาพ พิกเซลเอาต์พุตมีพื้นที่ a น้ำหนักของพิกเซลอินพุตที่มีพื้นที่ตัดกัน b จะคํานวณเป็น b/a และน้ำหนักของพิกเซลอินพุตที่มีพื้นที่ตัดกัน c จะคํานวณเป็น c/a ลักษณะการทำงานนี้อาจส่งผลให้เกิดผลลัพธ์ที่ไม่คาดคิดเมื่อใช้ตัวลดข้อมูลอื่นที่ไม่ใช่ตัวลดค่าเฉลี่ย เช่น หากต้องการคํานวณพื้นที่ป่าต่อพิกเซล ให้ใช้ตัวลดค่าเฉลี่ยเพื่อคํานวณเศษส่วนของพิกเซลที่ปกคลุม แล้วคูณด้วยพื้นที่ (แทนการคํานวณพื้นที่ในพิกเซลที่เล็กกว่า แล้วบวกเข้าด้วยกันด้วยตัวลดค่าผลรวม)

เครื่องมือแก้ไขโค้ด (JavaScript)

// Compute forest area per MODIS pixel.
var forestArea = forest.gt(0)
    // Force the next reprojection to aggregate instead of resampling.
    .reduceResolution({
      reducer: ee.Reducer.mean(),
      maxPixels: 1024
    })
    // The reduce resolution returns the fraction of the MODIS pixel
    // that's covered by 30 meter forest pixels.  Convert to area
    // after the reduceResolution() call.
    .multiply(ee.Image.pixelArea())
    // Request the data at the scale and projection of the MODIS image.
    .reproject({
      crs: modisProjection
    });
Map.addLayer(forestArea, {max: 500 * 500}, 'forested area at MODIS scale');

การตั้งค่า Python

ดูข้อมูลเกี่ยวกับ Python API และการใช้ geemap สําหรับการพัฒนาแบบอินเทอร์แอกทีฟได้ที่หน้า สภาพแวดล้อม Python

import ee
import geemap.core as geemap

Colab (Python)

# Compute forest area per MODIS pixel.
forest_area = (
    forest.gt(0)
    # Force the next reprojection to aggregate instead of resampling.
    .reduceResolution(reducer=ee.Reducer.mean(), maxPixels=1024)
    # The reduce resolution returns the fraction of the MODIS pixel
    # that's covered by 30 meter forest pixels.  Convert to area
    # after the reduceResolution() call.
    .multiply(ee.Image.pixelArea())
    # Request the data at the scale and projection of the MODIS image.
    .reproject(crs=modis_projection)
)
m.add_layer(forest_area, {'max': 500 * 500}, 'forested area at MODIS scale')
m