ee.FeatureCollection.reduceColumns

Áp dụng một hàm giảm cho từng phần tử của một bộ sưu tập, sử dụng các bộ chọn đã cho để xác định dữ liệu đầu vào.

Trả về một từ điển kết quả, được khoá bằng tên đầu ra.

Cách sử dụngGiá trị trả về
FeatureCollection.reduceColumns(reducer, selectors, weightSelectors)Từ điển
Đối sốLoạiThông tin chi tiết
this: collectionFeatureCollectionBộ sưu tập để tổng hợp.
reducerHàm giảmHàm giảm cần áp dụng.
selectorsDanh sáchBộ chọn cho từng dữ liệu đầu vào của hàm giảm.
weightSelectorsDanh sách, mặc định: nullBộ chọn cho từng dữ liệu đầu vào có trọng số của hàm giảm.

Ví dụ

Trình soạn thảo mã (JavaScript)

// FeatureCollection of power plants in Belgium.
var fc = ee.FeatureCollection('WRI/GPPD/power_plants')
            .filter('country_lg == "Belgium"');

// Calculate mean of a single FeatureCollection property.
var propMean = fc.reduceColumns({
  reducer: ee.Reducer.mean(),
  selectors: ['gwh_estimt']
});
print('Mean of a single property', propMean);

// Calculate mean of multiple FeatureCollection properties.
var propsMean = fc.reduceColumns({
  reducer: ee.Reducer.mean().repeat(2),
  selectors: ['gwh_estimt', 'capacitymw']
});
print('Mean of multiple properties', propsMean);

// Calculate weighted mean of a single FeatureCollection property. Add a fuel
// source weight property to the FeatureCollection.
var fuelWeights = ee.Dictionary({
  Wind: 0.9,
  Gas: 0.2,
  Oil: 0.2,
  Coal: 0.1,
  Hydro: 0.7,
  Biomass: 0.5,
  Nuclear: 0.3
});
fc = fc.map(function(feature) {
  return feature.set('weight', fuelWeights.getNumber(feature.get('fuel1')));
});

var weightedMean = fc.reduceColumns({
  reducer: ee.Reducer.mean(),
  selectors: ['gwh_estimt'],
  weightSelectors: ['weight']
});
print('Weighted mean of a single property', weightedMean);

Thiết lập Python

Hãy xem trang Môi trường Python để biết thông tin về Python API và cách sử dụng geemap cho quá trình phát triển có tính tương tác.

import ee
import geemap.core as geemap

Colab (Python)

# FeatureCollection of power plants in Belgium.
fc = ee.FeatureCollection('WRI/GPPD/power_plants').filter(
    'country_lg == "Belgium"')

# Calculate mean of a single FeatureCollection property.
prop_mean = fc.reduceColumns(**{
    'reducer': ee.Reducer.mean(),
    'selectors': ['gwh_estimt']
    })
display('Mean of a single property:', prop_mean)

# Calculate mean of multiple FeatureCollection properties.
props_mean = fc.reduceColumns(**{
    'reducer': ee.Reducer.mean().repeat(2),
    'selectors': ['gwh_estimt', 'capacitymw']
    })
display('Mean of multiple properties:', props_mean)


# Calculate weighted mean of a single FeatureCollection property. Add a fuel
# source weight property to the FeatureCollection.
def get_fuel(feature):
  return feature.set('weight', fuel_weights.getNumber(feature.get('fuel1')))

fuel_weights = ee.Dictionary({
    'Wind': 0.9,
    'Gas': 0.2,
    'Oil': 0.2,
    'Coal': 0.1,
    'Hydro': 0.7,
    'Biomass': 0.5,
    'Nuclear': 0.3
    })

fc = fc.map(get_fuel)

weighted_mean = fc.reduceColumns(**{
    'reducer': ee.Reducer.mean(),
    'selectors': ['gwh_estimt'],
    'weightSelectors': ['weight']
    })
display('Weighted mean of a single property:', weighted_mean)