Để giảm các thuộc tính của các tính năng trong FeatureCollection
, hãy sử dụng featureCollection.reduceColumns()
. Hãy xem ví dụ sau đây về đồ chơi:
Trình soạn thảo mã (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'] ) )
Xin lưu ý rằng các giá trị đầu vào được tính trọng số theo thuộc tính weight
được chỉ định. Do đó, kết quả là:
mean: 2.333333333333333
Ví dụ phức tạp hơn: hãy xem xét FeatureCollection
của các khu vực điều tra dân số ở Hoa Kỳ với dữ liệu điều tra dân số làm thuộc tính. Các biến quan tâm là tổng dân số và tổng số nhà ở. Bạn có thể lấy(các) tổng bằng cách cung cấp đối số bộ giảm tổng cho reduceColumns()
và in kết quả:
Trình soạn thảo mã (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ết quả là một Dictionary
đại diện cho thuộc tính tổng hợp theo trình giảm được chỉ định:
sum: [85579,36245]
Xin lưu ý rằng ví dụ trên sử dụng bộ lọc notNull()
để chỉ bao gồm các tính năng có mục nhập khác rỗng cho các thuộc tính đã chọn trong bộ sưu tập đang được giảm.
Bạn nên kiểm tra các mục rỗng để phát hiện dữ liệu bị thiếu ngoài dự kiến và tránh các lỗi phát sinh từ các phép tính có chứa giá trị rỗng.
Ngoài ra, hãy lưu ý rằng không giống như imageCollection.reduce()
, trong đó các trình giảm được tự động lặp lại cho mỗi dải, các trình giảm trên FeatureCollection
phải được lặp lại một cách rõ ràng bằng cách sử dụng repeat()
. Cụ thể, lặp lại bộ giảm m lần cho m đầu vào. Lỗi sau đây có thể xảy ra do không lặp lại
hàm giảm: