You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Earth Engine报错:reduceRegions is not a function 排查与解决

问题分析与修正方案

核心问题

  • gpw_roi是**ImageCollection(影像集合)**类型,而reduceRegions()是单个Image对象的专属方法,直接调用会触发“不是函数”的错误。
  • 代码逻辑混淆了影像集合与矢量集合的用法:你错误地将ImageCollection当作FeatureCollection使用map()遍历,且reduceRegions的collection参数要求传入FeatureCollection,但你传了单个geometry对象。

修正方案

根据你的需求,提供两种常见场景的修正代码:

场景1:获取某一特定年份的AOI人口总数

比如获取2020年的人口数据:

// Load GPW population count dataset
var gpw = ee.ImageCollection('CIESIN/GPWv4/population-count');

// Define the area of interest (AOI) using a polygon geometry
var aoi = ee.Geometry.Polygon([
  [-74.1, 40.6],
  [-74.1, 40.7],
  [-73.9, 40.7],
  [-73.9, 40.6],
]);

// 1. 筛选2020年的影像,转为单个Image对象
var gpw_2020 = gpw.filter(ee.Filter.eq('year', 2020)).first();

// 2. 将AOI转为FeatureCollection,作为reduceRegions的输入
var aoi_fc = ee.FeatureCollection([ee.Feature(aoi)]);

// 3. 计算AOI内的人口总和
var population = gpw_2020.reduceRegions({
  reducer: ee.Reducer.sum(),
  collection: aoi_fc,
  scale: 1000, // 分辨率,单位米,与GPW原始分辨率匹配
});

// 打印结果
print('2020年AOI人口总数:', population.first().get('sum'));

场景2:获取所有年份的AOI人口数据

如果需要遍历所有可用年份,计算各年份的人口:

// Load GPW population count dataset
var gpw = ee.ImageCollection('CIESIN/GPWv4/population-count');

// Define the area of interest (AOI) using a polygon geometry
var aoi = ee.Geometry.Polygon([
  [-74.1, 40.6],
  [-74.1, 40.7],
  [-73.9, 40.7],
  [-73.9, 40.6],
]);

// 将AOI转为FeatureCollection
var aoi_fc = ee.FeatureCollection([ee.Feature(aoi)]);

// 遍历影像集合,计算每个年份的人口
var populationByYear = gpw.map(function(image) {
  var year = image.get('year');
  // 对单张影像执行区域统计
  var totalPop = image.reduceRegions({
    reducer: ee.Reducer.sum(),
    collection: aoi_fc,
    scale: 1000,
  }).first().get('sum');
  // 返回包含年份和人口的Feature
  return ee.Feature(null, {
    year: year,
    total_population: totalPop
  });
});

// 打印所有年份的人口数据
print('各年份AOI人口数据:', populationByYear);

补充说明

  • GPWv4可用年份为2000、2005、2010、2015、2020,可通过filter.eq('year', 目标年份)精准筛选。
  • scale参数建议匹配影像原始分辨率(GPWv4为30弧秒,约1000米),过高的分辨率会导致计算冗余。
  • 如果需要人口密度而非总数,可替换数据集为CIESIN/GPWv4/population-density,统计逻辑完全一致,只需根据需求选择Reducer.sum()或Reducer.mean()。

内容的提问来源于stack exchange,提问作者Shadow Walker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 08:35:27