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

如何在Google Earth Engine中跨多年获取单个几何区域的降水数据

解决方法

你的核心问题是误用了reduceRegion——它只会计算整个输入几何范围的统计值,而要对要素集合里的每个几何单独计算,应该用reduceRegions。另外,直接给yearlyRainfall加几何参数会因为GEE的map函数只能传递单个参数而报错,调整思路如下:

  1. 调整年份处理函数,让它对单个年份计算总降水后,用reduceRegions遍历circles_collection的每个要素,输出带年份、降水值的要素
  2. 把每年生成的要素集合合并成一个完整集合,再转成DataFrame

修改后的代码:

def yearlyRainfall(year):
    startDate = ee.Date.fromYMD(year, 1, 1)
    endDate = startDate.advance(1, 'year')
    # 筛选当年降水数据并计算年总量
    filtered = chirps.filter(ee.Filter.date(startDate, endDate))
    total = filtered.reduce(ee.Reducer.sum())
    # 对每个要素单独计算均值,返回带统计值的要素集合
    stats_col = total.reduceRegions(
        collection = circles_collection,
        reducer = ee.Reducer.mean(),
        scale = 5000,
    )
    # 给每个要素添加年份属性
    return stats_col.map(lambda feat: feat.set('year', year))

years = ee.List.sequence(1981, 2019)
# 映射年份得到每年的要素集合,再 flatten 合并成单个集合
rainfallYears = ee.FeatureCollection(years.map(yearlyRainfall)).flatten()
# 转成DataFrame
rainfalldf = geemap.ee_to_pandas(rainfallYears)
# 查看结果(假设你的circles_collection有id属性,可按id分组查看单个图形的逐年数据)
print(rainfalldf.head())
# 示例:按id分组计算每个图形的平均年降水
print(rainfalldf.groupby('id')['precipitation_sum'].mean())

关键说明:

  • reduceRegions会自动遍历circles_collection的每个要素,为每个要素生成包含降水统计值的新要素
  • 用map给每个要素添加年份属性,确保后续能区分数据对应的年份
  • flatten()用来把多个年份的要素集合合并成一个,避免嵌套结构导致转DataFrame失败
  • 如果你的circles_collection没有唯一标识属性(比如id),建议先给每个要素添加:
    circles_collection = circles_collection.map(lambda feat: feat.set('id', feat.id()))
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 11:15:58