您可以使用 reducer.group()
按指定输入的值对缩减器的输出进行分组,从而获取 Image
或 FeatureCollection
的每个区域中的统计信息。例如,如需计算每个州的总人口和住宅单元数量,此示例会按如下方式对人口普查区 FeatureCollection
的缩减输出进行分组:
Code Editor (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
参数指定用于存储分组变量值的属性的名称。由于不会针对每个输入自动重复 reducer,因此需要进行 repeat(2)
调用。
如需对 image.reduceRegions()
的输出进行分组,您可以指定一个分组带,以按整数像素值定义组。这种类型的计算有时称为“区域统计信息”,其中区域被指定为分组带,统计信息由缩减器确定。在以下示例中,美国夜间灯光的变化按土地覆盖类别分组:
Code Editor (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,以此类推。