您可以使用 reducer.group()
依指定輸入內容的值,將 Image
或 FeatureCollection
的每個區域分組,藉此取得統計資料。舉例來說,為了計算各州的總人口數和住房單位數,這個範例會將人口普查區塊 FeatureCollection
的縮減結果分組,如下所示:
程式碼編輯器 (JavaScript)
// Load a collection of US census blocks. var blocks = ee.FeatureCollection('TIGER/2010/Blocks'); // Compute sums of the specified properties, grouped by state code. var sums = blocks .filter(ee.Filter.and( ee.Filter.neq('pop10', null), ee.Filter.neq('housing10', null))) .reduceColumns({ selectors: ['pop10', 'housing10', 'statefp10'], reducer: ee.Reducer.sum().repeat(2).group({ groupField: 2, groupName: 'state-code', }) }); // Print the resultant Dictionary. print(sums);
import ee import geemap.core as geemap
Colab (Python)
# Load a collection of US census blocks. blocks = ee.FeatureCollection('TIGER/2010/Blocks') # Compute sums of the specified properties, grouped by state code. sums = blocks.filter( ee.Filter.And( ee.Filter.neq('pop10', None), ee.Filter.neq('housing10', None) ) ).reduceColumns( selectors=['pop10', 'housing10', 'statefp10'], reducer=ee.Reducer.sum() .repeat(2) .group(groupField=2, groupName='state-code'), ) # Print the resultant Dictionary. display(sums)
groupField
引數是選取器陣列中輸入項目的索引,其中包含用於分組的代碼;groupName
引數則指定儲存分組變數值的屬性名稱。由於系統不會自動針對每個輸入內容重複執行縮減器,因此需要 repeat(2)
呼叫。
如要將 image.reduceRegions()
的輸出內容分組,您可以指定分組頻帶,以整數像素值定義群組。這類運算有時稱為「區域統計資料」,其中區域會指定為分組頻帶,而統計資料則由縮減器決定。在以下範例中,美國的夜間燈光變化會依土地覆蓋率類別分組:
程式碼編輯器 (JavaScript)
// Load a region representing the United States var region = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017') .filter(ee.Filter.eq('country_na', 'United States')); // Load MODIS land cover categories in 2001. var landcover = ee.Image('MODIS/051/MCD12Q1/2001_01_01') // Select the IGBP classification band. .select('Land_Cover_Type_1'); // Load nightlights image inputs. var nl2001 = ee.Image('NOAA/DMSP-OLS/NIGHTTIME_LIGHTS/F152001') .select('stable_lights'); var nl2012 = ee.Image('NOAA/DMSP-OLS/NIGHTTIME_LIGHTS/F182012') .select('stable_lights'); // Compute the nightlights decadal difference, add land cover codes. var nlDiff = nl2012.subtract(nl2001).addBands(landcover); // Grouped a mean reducer: change of nightlights by land cover category. var means = nlDiff.reduceRegion({ reducer: ee.Reducer.mean().group({ groupField: 1, groupName: 'code', }), geometry: region.geometry(), scale: 1000, maxPixels: 1e8 }); // Print the resultant Dictionary. print(means);
import ee import geemap.core as geemap
Colab (Python)
# Load a region representing the United States region = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017').filter( ee.Filter.eq('country_na', 'United States') ) # Load MODIS land cover categories in 2001. landcover = ee.Image('MODIS/051/MCD12Q1/2001_01_01').select( # Select the IGBP classification band. 'Land_Cover_Type_1' ) # Load nightlights image inputs. nl_2001 = ee.Image('NOAA/DMSP-OLS/NIGHTTIME_LIGHTS/F152001').select( 'stable_lights' ) nl_2012 = ee.Image('NOAA/DMSP-OLS/NIGHTTIME_LIGHTS/F182012').select( 'stable_lights' ) # Compute the nightlights decadal difference, add land cover codes. nl_diff = nl_2012.subtract(nl_2001).addBands(landcover) # Grouped a mean reducer: change of nightlights by land cover category. means = nl_diff.reduceRegion( reducer=ee.Reducer.mean().group(groupField=1, groupName='code'), geometry=region.geometry(), scale=1000, maxPixels=1e8, ) # Print the resultant Dictionary. display(means)
請注意,在本範例中,groupField
是包含區域的頻帶索引,可用於將輸出內容分組。第一個頻帶的索引為 0,第二個為 1,以此類推。