Bạn có thể lấy số liệu thống kê trong mỗi vùng của Image
hoặc FeatureCollection
bằng cách sử dụng reducer.group()
để nhóm đầu ra của một trình giảm theo giá trị của một đầu vào đã chỉ định. Ví dụ: để tính tổng dân số và số lượng nhà ở ở mỗi tiểu bang, ví dụ này nhóm kết quả của việc giảm một khối điều tra dân số FeatureCollection
như sau:
Trình soạn thảo mã (JavaScript)
// Load a collection of US census blocks. var blocks = ee.FeatureCollection('TIGER/2010/Blocks'); // Compute sums of the specified properties, grouped by state code. var sums = blocks .filter(ee.Filter.and( ee.Filter.neq('pop10', null), ee.Filter.neq('housing10', null))) .reduceColumns({ selectors: ['pop10', 'housing10', 'statefp10'], reducer: ee.Reducer.sum().repeat(2).group({ groupField: 2, groupName: 'state-code', }) }); // Print the resultant Dictionary. print(sums);
import ee import geemap.core as geemap
Colab (Python)
# Load a collection of US census blocks. blocks = ee.FeatureCollection('TIGER/2010/Blocks') # Compute sums of the specified properties, grouped by state code. sums = blocks.filter( ee.Filter.And( ee.Filter.neq('pop10', None), ee.Filter.neq('housing10', None) ) ).reduceColumns( selectors=['pop10', 'housing10', 'statefp10'], reducer=ee.Reducer.sum() .repeat(2) .group(groupField=2, groupName='state-code'), ) # Print the resultant Dictionary. display(sums)
Đối số groupField
là chỉ mục của dữ liệu đầu vào trong mảng bộ chọn chứa các mã để nhóm, đối số groupName
chỉ định tên của thuộc tính để lưu trữ giá trị của biến nhóm. Vì trình giảm không tự động lặp lại cho mỗi đầu vào, nên bạn cần có lệnh gọi repeat(2)
.
Để nhóm đầu ra của image.reduceRegions()
, bạn có thể chỉ định một dải nhóm xác định các nhóm theo giá trị pixel số nguyên. Loại tính toán này đôi khi được gọi là "thống kê theo vùng", trong đó các vùng được chỉ định là dải nhóm và thống kê được xác định bởi bộ giảm. Trong ví dụ sau, sự thay đổi về ánh sáng ban đêm ở Hoa Kỳ được nhóm theo danh mục lớp phủ đất:
Trình soạn thảo mã (JavaScript)
// Load a region representing the United States var region = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017') .filter(ee.Filter.eq('country_na', 'United States')); // Load MODIS land cover categories in 2001. var landcover = ee.Image('MODIS/051/MCD12Q1/2001_01_01') // Select the IGBP classification band. .select('Land_Cover_Type_1'); // Load nightlights image inputs. var nl2001 = ee.Image('NOAA/DMSP-OLS/NIGHTTIME_LIGHTS/F152001') .select('stable_lights'); var nl2012 = ee.Image('NOAA/DMSP-OLS/NIGHTTIME_LIGHTS/F182012') .select('stable_lights'); // Compute the nightlights decadal difference, add land cover codes. var nlDiff = nl2012.subtract(nl2001).addBands(landcover); // Grouped a mean reducer: change of nightlights by land cover category. var means = nlDiff.reduceRegion({ reducer: ee.Reducer.mean().group({ groupField: 1, groupName: 'code', }), geometry: region.geometry(), scale: 1000, maxPixels: 1e8 }); // Print the resultant Dictionary. print(means);
import ee import geemap.core as geemap
Colab (Python)
# Load a region representing the United States region = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017').filter( ee.Filter.eq('country_na', 'United States') ) # Load MODIS land cover categories in 2001. landcover = ee.Image('MODIS/051/MCD12Q1/2001_01_01').select( # Select the IGBP classification band. 'Land_Cover_Type_1' ) # Load nightlights image inputs. nl_2001 = ee.Image('NOAA/DMSP-OLS/NIGHTTIME_LIGHTS/F152001').select( 'stable_lights' ) nl_2012 = ee.Image('NOAA/DMSP-OLS/NIGHTTIME_LIGHTS/F182012').select( 'stable_lights' ) # Compute the nightlights decadal difference, add land cover codes. nl_diff = nl_2012.subtract(nl_2001).addBands(landcover) # Grouped a mean reducer: change of nightlights by land cover category. means = nl_diff.reduceRegion( reducer=ee.Reducer.mean().group(groupField=1, groupName='code'), geometry=region.geometry(), scale=1000, maxPixels=1e8, ) # Print the resultant Dictionary. display(means)
Xin lưu ý rằng trong ví dụ này, groupField
là chỉ mục của dải chứa các vùng để nhóm đầu ra. Dải tần đầu tiên là chỉ mục 0, dải tần thứ hai là chỉ mục 1, v.v.