如投影文件所述,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');
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)。
圖 2 為此程式碼範例的作業順序。具體來說,系統會使用輸入圖像上指定的重新取樣方法,對地圖 Mercator 投影方式進行隱含重新投影。
圖 2. 在輸入圖片上呼叫 resample()
之前,在程式碼編輯器中顯示的操作流程圖。曲線表示資訊流向重投影:具體來說,就是要使用的輸出投影、比例和重新取樣方法。
降低解析度
假設您的目標是在不同投影中,將像素匯總為較大的像素,而非在重新投影期間重新取樣。這項功能可用於比較不同比例的圖像資料集,例如將以 Landsat 為基礎的產品中的 30 公尺像素,與以 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');
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. 在 reproject()
之前,對輸入圖片呼叫 reduceResolution()
時的作業流程圖。曲線表示重投影資訊的流程,具體來說,就是要使用的輸出投影、比例和像素匯集方法。
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');
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