כדי לצמצם את המאפיינים של תכונות ב-FeatureCollection
, משתמשים ב-featureCollection.reduceColumns()
. נבחן את הדוגמה הבאה:
Code Editor (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()
ארגומנט של רדוקטור שמחשב סכום ומדפיסים את התוצאה:
Code Editor (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 מקורות קלט. ייתכן שתופיע השגיאה הבאה כתוצאה מהעובדה שלא חוזרים על המצמצם: