Statistik Kolom FeatureCollection

Untuk mengurangi properti fitur dalam FeatureCollection, gunakan featureCollection.reduceColumns(). Perhatikan contoh mainan berikut:

Editor Kode (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']
}));

Penyiapan Python

Lihat halaman Lingkungan Python untuk mengetahui informasi tentang Python API dan penggunaan geemap untuk pengembangan interaktif.

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']
    )
)

Perhatikan bahwa input diberi bobot sesuai dengan properti weight yang ditentukan. Oleh karena itu, hasilnya adalah:

mean: 2.333333333333333
    

Sebagai contoh yang lebih kompleks, pertimbangkan FeatureCollection blok sensus Amerika Serikat dengan data sensus sebagai atribut. Variabel yang diminati adalah total populasi dan total unit perumahan. Anda bisa mendapatkan jumlah totalnya dengan memberikan argumen pengurangan jumlah ke reduceColumns() dan mencetak hasilnya:

Editor Kode (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);

Penyiapan Python

Lihat halaman Lingkungan Python untuk mengetahui informasi tentang Python API dan penggunaan geemap untuk pengembangan interaktif.

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)

Outputnya adalah Dictionary yang mewakili properti gabungan sesuai dengan pengurangan yang ditentukan:

sum: [85579,36245]
    

Perhatikan bahwa contoh di atas menggunakan filter notNull() untuk hanya menyertakan fitur dengan entri non-null untuk properti yang dipilih dalam koleksi yang dikurangi. Sebaiknya periksa entri null untuk menangkap data yang tidak terduga dan menghindari error yang dihasilkan dari penghitungan yang menyertakan nilai null.

Perhatikan juga bahwa tidak seperti imageCollection.reduce(), yang pengurangannya diulang secara otomatis untuk setiap band, pengurangan pada FeatureCollection harus diulang secara eksplisit menggunakan repeat(). Secara khusus, ulangi pengurangan sebanyak m kali untuk input m. Error berikut dapat ditampilkan karena tidak mengulangi pengurangan: