หากต้องการลดพร็อพเพอร์ตี้ขององค์ประกอบใน 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
นอกจากนี้ โปรดทราบว่า FeatureCollection
ต่างจาก imageCollection.reduce()
ตรงที่ตัวลดจำนวนใน FeatureCollection
ต้องซ้ำกันอย่างชัดเจนโดยใช้ repeat()
ต่างจาก imageCollection.reduce()
ที่ตัวลดจำนวนจะซ้ำกันโดยอัตโนมัติสำหรับแต่ละย่านความถี่ กล่าวโดยละเอียดคือ ให้ใช้ตัวลด m ครั้งสําหรับอินพุต m ระบบอาจแสดงข้อผิดพลาดต่อไปนี้เนื่องจากไม่ได้ใช้ตัวลดซ้ำ