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'] }));
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);
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 回レジューサーを繰り返します。リデューサーが繰り返されなかった結果、次のエラーがスローされることがあります。