FeatureCollection 열의 통계

FeatureCollection에서 지형지물의 속성을 줄이려면 featureCollection.reduceColumns()를 사용하세요. 다음과 같은 간단한 예시를 생각해 보세요.

코드 편집기 (JavaScript)

// Make a toy FeatureCollection.
var aFeatureCollection = ee.FeatureCollection([
  ee.Feature(null, {foo: 1, weight: 1}),
  ee.Feature(null, {foo: 2, weight: 2}),
  ee.Feature(null, {foo: 3, weight: 3}),
]);

// Compute a weighted mean and display it.
print(aFeatureCollection.reduceColumns({
  reducer: ee.Reducer.mean(),
  selectors: ['foo'],
  weightSelectors: ['weight']
}));

Python 설정

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

import ee
import geemap.core as geemap

Colab (Python)

# Make a toy FeatureCollection.
a_feature_collection = ee.FeatureCollection([
    ee.Feature(None, {'foo': 1, 'weight': 1}),
    ee.Feature(None, {'foo': 2, 'weight': 2}),
    ee.Feature(None, {'foo': 3, 'weight': 3}),
])

# Compute a weighted mean and display it.
display(
    a_feature_collection.reduceColumns(
        reducer=ee.Reducer.mean(), selectors=['foo'], weightSelectors=['weight']
    )
)

입력은 지정된 weight 속성에 따라 가중치가 적용됩니다. 따라서 결과는 다음과 같습니다.

mean: 2.333333333333333
    

좀 더 복잡한 예로 인구 조사 데이터가 속성으로 포함된 미국 인구 조사 구역의 FeatureCollection를 생각해 보세요. 관심 변수는 총 인구와 총 주택 수입니다. reduceColumns()에 합산 리듀서 인수를 제공하고 결과를 출력하여 합계를 얻을 수 있습니다.

코드 편집기 (JavaScript)

// Load US census data as a FeatureCollection.
var census = ee.FeatureCollection('TIGER/2010/Blocks');

// Filter the collection to include only Benton County, OR.
var benton = census.filter(
  ee.Filter.and(
    ee.Filter.eq('statefp10', '41'),
    ee.Filter.eq('countyfp10', '003')
  )
);

// Display Benton County census blocks.
Map.setCenter(-123.27, 44.57, 13);
Map.addLayer(benton);

// Compute sums of the specified properties.
var properties = ['pop10', 'housing10'];
var sums = benton
    .filter(ee.Filter.notNull(properties))
    .reduceColumns({
      reducer: ee.Reducer.sum().repeat(2),
      selectors: properties
    });

// Print the resultant Dictionary.
print(sums);

Python 설정

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

import ee
import geemap.core as geemap

Colab (Python)

# Load US census data as a FeatureCollection.
census = ee.FeatureCollection('TIGER/2010/Blocks')

# Filter the collection to include only Benton County, OR.
benton = census.filter(
    ee.Filter.And(
        ee.Filter.eq('statefp10', '41'), ee.Filter.eq('countyfp10', '003')
    )
)

# Display Benton County census blocks.
m = geemap.Map()
m.set_center(-123.27, 44.57, 13)
m.add_layer(benton)
display(m)

# Compute sums of the specified properties.
properties = ['pop10', 'housing10']
sums = benton.filter(ee.Filter.notNull(properties)).reduceColumns(
    reducer=ee.Reducer.sum().repeat(2), selectors=properties
)

# Print the resultant Dictionary.
display(sums)

출력은 지정된 리듀서의 집계된 속성을 나타내는 Dictionary입니다.

sum: [85579,36245]
    

위 예에서는 notNull() 필터를 사용하여 축소되는 컬렉션에서 선택한 속성에 대한 null이 아닌 항목이 있는 기능만 포함합니다. null 항목을 확인하여 예상치 못한 누락된 데이터를 포착하고 null 값이 포함된 계산으로 인한 오류를 방지하는 것이 좋습니다.

또한 리듀서가 각 밴드에 대해 자동으로 반복되는 imageCollection.reduce()와 달리 FeatureCollection의 리듀서는 repeat()를 사용하여 명시적으로 반복해야 합니다. 구체적으로 m 입력에 대해 리듀서를 m번 반복합니다. 리듀서를 반복하지 않으면 다음과 같은 오류가 발생할 수 있습니다.