Aby zredukować właściwości funkcji w funkcji FeatureCollection
, użyj instrukcji featureCollection.reduceColumns()
. Rozważ ten przykład:
Edytor kodu (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'] ) )
Pamiętaj, że wartości wejściowe są ważone zgodnie z określoną właściwością weight
. W efekcie:
mean: 2.333333333333333
Bardziej skomplikowanym przykładem jest FeatureCollection
bloków spisu powszechnego w Stanach Zjednoczonych z danymi spisu jako atrybutami. Interesujące nas zmienne to łączna liczba ludności i łączna liczba lokali mieszkalnych. Możesz uzyskać ich sumę, podając argument sumowania do funkcji reduceColumns()
i wydrukowując wynik:
Edytor kodu (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)
Dane wyjściowe to wartość Dictionary
reprezentująca zsumowaną właściwość zgodnie ze wskazanym reduktorem:
sum: [85579,36245]
W tym przykładzie użyto filtra notNull()
, aby uwzględnić tylko te cechy z niezerowymi wartościami, które występują w wybranych usługach w zbiorze.
Warto sprawdzać, czy nie ma w nich wartości null, aby wykrywać nieoczekiwane brakujące dane i unikać błędów wynikających z obliczeń zawierających wartości null.
Pamiętaj też, że w odróżnieniu od funkcji imageCollection.reduce()
, w której reduktor jest automatycznie powtarzany dla każdego pasma, reduktor w funkcji FeatureCollection
musi być powtarzany za pomocą funkcji repeat()
. W szczególności powtórz reduktor m razy dla m wejść. W efekcie niepowtarzania reduktora może wystąpić ten błąd: