그룹화된 감소 및 구역 통계

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);

Python 설정

Python API 및 대화형 개발을 위한 geemap 사용에 관한 자세한 내용은 Python 환경 페이지를 참고하세요.

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()의 출력을 그룹화하려면 정수 픽셀 값으로 그룹을 정의하는 그룹화 밴드를 지정하면 됩니다. 이러한 유형의 계산을 '지역 통계'라고도 하는데, 이때 지역은 그룹화 밴드로 지정되고 통계는 감소기에 의해 결정됩니다. 다음 예에서는 미국의 야간 조명 변화가 토지 피복 카테고리별로 그룹화되어 있습니다.

코드 편집기 (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);

Python 설정

Python API 및 대화형 개발을 위한 geemap 사용에 관한 자세한 내용은 Python 환경 페이지를 참고하세요.

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 등으로 지정됩니다.