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

在Python/Google Colab中按区县计算CHIRPS月均降雨量报错求助

解决CHIRPS月均降水量统计与CSV导出问题

核心问题分析

  • 第一个报错是因为你传入reduceRegions的districts是本地GeoPandas GeoDataFrame,Earth Engine API仅识别自身的ee.FeatureCollection类型,无法直接解析本地地理数据对象。
  • 第二个报错是因为.featureBounds()方法要求输入EE原生几何对象,而非本地GeoDataFrame,类型不匹配导致解析失败。

完整解决方案代码

以下代码直接实现「月度降水均值计算+区域统计+CSV导出」,适配你的需求:

import geopandas as gpd
import ee

# 初始化Earth Engine(Colab环境必须执行)
ee.Authenticate()
ee.Initialize()

# 1. 加载本地shapefile并转换为EE FeatureCollection
path = "/content/drive/.../x.shp"
districts_gdf = gpd.read_file(path)

# 确保坐标系与CHIRPS一致(CHIRPS采用EPSG:4326),若shapefile坐标系不符则转换
if districts_gdf.crs != "EPSG:4326":
    districts_gdf = districts_gdf.to_crs("EPSG:4326")

# 将本地GeoDataFrame转为GeoJSON格式,再导入为EE FeatureCollection
districts_ee = ee.FeatureCollection(districts_gdf.__geo_interface__)

# 2. 定义时间范围与加载CHIRPS数据集
startDate = ee.Date('2004-01-01')
endDate = ee.Date('2021-12-31')

chirps = ee.ImageCollection('UCSB-CHG/CHIRPS/DAILY') \
    .filterDate(startDate, endDate) \
    .select("precipitation")

# 3. 定义月度聚合函数:计算每个月的降水均值
def monthly_aggregate(year):
    year = ee.Number(year)
    # 遍历1-12月生成月度均值影像
    def monthly_mean(month):
        month = ee.Number(month)
        start = ee.Date.fromYMD(year, month, 1)
        end = start.advance(1, 'month')
        # 筛选当月影像并计算均值,同时标记年月属性
        monthly_img = chirps.filterDate(start, end).mean() \
            .set('year', year) \
            .set('month', month)
        return monthly_img
    return ee.List.sequence(1, 12).map(monthly_mean)

# 生成2004-2021年的月度均值影像集合
years = ee.List.sequence(2004, 2021)
monthly_chirps = ee.ImageCollection.fromImages(years.map(monthly_aggregate).flatten())

# 4. 定义区域统计函数:对每个月度影像计算区县均值
def reduce_monthly(img):
    year = img.get('year')
    month = img.get('month')
    # 执行区域均值统计
    stats = img.reduceRegions(
        collection=districts_ee,
        reducer=ee.Reducer.mean(),
        scale=5500  # 匹配CHIRPS原生~5.5km分辨率
    )
    # 给每个统计结果添加年月属性,便于后续识别
    return stats.map(lambda feat: feat.set({'year': year, 'month': month}))

# 执行统计并扁平化结果集合
rainfall_stats = monthly_chirps.map(reduce_monthly).flatten()

# 5. 导出结果到云端硬盘CSV文件
task = ee.batch.Export.table.toDrive(
    collection=rainfall_stats,
    description='district_monthly_rainfall_2004-2021',
    folder='your_drive_folder_name',  # 替换为你的云端硬盘目标文件夹名
    fileFormat='CSV',
    selectors=['year', 'month', 'precipitation', '区县名称字段']  # 替换为shapefile中实际的区县名称字段
)
task.start()

# 查看任务状态(可选)
print("导出任务已启动,可在Earth Engine网页控制台的Tasks页面查看进度")

关键注意事项

  • 替换代码中的your_drive_folder_name为你云端硬盘的目标文件夹名称,确保Colab已挂载云端硬盘。
  • 替换selectors中的区县名称字段为shapefile中实际存储区县名称的字段(如NAME、district等),保证导出的CSV包含区县标识。
  • 若shapefile含中文字段名,可在gpd.read_file中添加encoding='utf-8'避免乱码。
  • 采用月度聚合的方式比逐日统计后再聚合更高效,大幅减少计算量与内存占用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 16:35:25