کاهش‌های گروه‌بندی‌شده و آمار منطقه‌ای

می‌توانید بااستفاده از reducer.group() برای گروه‌بندی خروجی کاهنده براساس مقدار ورودی مشخص‌شده، آمار هر منطقه از Image یا FeatureCollection را دریافت کنید. برای مثال، برای محاسبه کل جمعیت و تعداد واحدهای مسکونی در هر ایالت، این نمونه خروجی کاهش بلوک سرشماری FeatureCollection را به‌صورت زیر گروه‌بندی می‌کند:

ویرایشگر کد (جاوا اسکریپت)

// 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);

راه‌اندازی Python

برای اطلاعات درباره Python API و استفاده از geemap برای توسعه تعاملی، به صفحه محیط Python مراجعه کنید.

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)

آرگومان groupField نمایه ورودی در آرایه انتخاب‌کننده‌ها است که حاوی کدهایی است که براساس آن‌ها گروه‌بندی انجام می‌شود، آرگومان groupName نام ویژگی را برای ذخیره مقدار متغیر گروه‌بندی مشخص می‌کند. چون کاهنده به‌طور خودکار برای هر ورودی تکرار نمی‌شود، فراخوانی repeat(2) لازم است.

برای گروه‌بندی خروجی image.reduceRegions() می‌توانید باند گروه‌بندی‌ای را مشخص کنید که گروه‌ها را براساس مقادیر پیکسل عدد صحیح تعریف می‌کند. این نوع محاسبه گاهی اوقات «آمار منطقه‌ای» نامیده می‌شود که در آن مناطق به‌عنوان باند گروه‌بندی مشخص می‌شوند و آمار توسط کاهنده تعیین می‌شود. در مثال زیر، تغییر در چراغ‌های شب در ایالات متحده براساس دسته پوشش زمین گروه‌بندی شده است:

ویرایشگر کد (جاوا اسکریپت)

// 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/061/MCD12Q1/2001_01_01')
  // Select the IGBP classification band.
  .select('LC_Type1');

// 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);

راه‌اندازی Python

برای اطلاعات درباره Python API و استفاده از geemap برای توسعه تعاملی، به صفحه محیط Python مراجعه کنید.

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/061/MCD12Q1/2001_01_01').select(
    # Select the IGBP classification band.
    'LC_Type1'
)

# 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)

توجه داشته باشید که در این مثال، groupField نمایه باندی است که حاوی مناطقی است که خروجی باید براساس آن‌ها گروه‌بندی شود. باند اول نمایه ۰، باند دوم نمایه ۱، و غیره است.