FeatureCollection
içindeki özelliklerin özelliklerini azaltmak için featureCollection.reduceColumns()
'ü kullanın. Aşağıdaki oyuncak örneğini inceleyin:
Kod Düzenleyici (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'] }));
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'] ) )
Girişlerin, belirtilen weight
özelliğine göre ağırlıklandırıldığını unutmayın. Sonuç olarak:
mean: 2.333333333333333
Daha karmaşık bir örnek olarak, nüfus sayımı verilerini özellik olarak içeren ABD nüfus sayımı bloklarının FeatureCollection
'ünü düşünün. İlgilenilen değişkenler toplam nüfus ve toplam konut birimidir. reduceColumns()
için bir toplama azaltıcı bağımsız değişkeni sağlayıp sonucu yazdırarak toplamlarını elde edebilirsiniz:
Kod Düzenleyici (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);
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)
Çıkış, belirtilen azaltıcıya göre toplanmış mülkü temsil eden bir Dictionary
olur:
sum: [85579,36245]
Yukarıdaki örnekte, yalnızca koleksiyonda küçültülen seçili mülkler için null olmayan girişlere sahip özellikleri dahil etmek üzere notNull()
filtresinin kullanıldığını unutmayın.
Beklenmedik eksik verileri yakalamak ve null değerler içeren hesaplamalardan kaynaklanan hataları önlemek için null girişleri kontrol etmek iyi bir uygulamadır.
Ayrıca, her bant için azaltıcıların otomatik olarak tekrarlandığı imageCollection.reduce()
'ten farklı olarak, FeatureCollection
'teki azaltıcıların repeat()
kullanılarak açıkça tekrarlanması gerektiğini unutmayın. Daha açık belirtmek gerekirse, m girişi için azaltıcıyı m kez tekrarlayın. Azaltıcı tekrarlanmadığı için aşağıdaki hata oluşabilir: