我正在try 使用从我的驱动器导入的Shapefile文件,基于按地区划分的chirps数据来检索2004年至2021年的月平均降雨量.到目前为止,我在Google Colab中使用了以下代码:

path = "/content/drive/.../x.shp"
districts = gpd.read_file(path) 

startDate = ee.Date('2004-01-01')
endDate = ee.Date('2021-12-31')

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

# Reduce the rainfall data to the district polygons
def reduce_image(img):
    img_reduced = img.reduceRegions(
        collection=districts,
        reducer=ee.Reducer.mean(),
        scale=5500
    )
    return img_reduced

rainfall_reduced = chirps.map(reduce_image).flatten()

...但我收到一条错误消息说

EEException: Unrecognized argument type to convert to a FeatureCollection

另外,当我try 添加

.featureBounds(districts) 

对于chirps导入,我收到一条错误消息

EEException: Invalid GeoJSON geometry.

我试着更改代码几个小时了,但似乎无法使其工作.

谁能告诉我如何计算每个地区的月平均降雨量,并最终以.csv文件的形式下载它们?

非常提前感谢您!

推荐答案

我们需要使用to_json()和其他一些东西从shapefile中获取'features',才能将其作为ee.FeatureCollection:

file_name = '/content/drive/My Drive/Colab Notebooks/DATA_FOLDERS/SHP/gadm41_PRY_2.shx'
districts = gpd.read_file(file_name)

fc = []
for i in range(districts.shape[0]):
    g = districts.iloc[i:i + 1, :] 
    json_dict = eval(g.to_json()) 
    geo_json_dict = json_dict['features'][0] 
    fc.append(ee.Feature(geo_json_dict))

districts = ee.FeatureCollection(fc)

我们还需要在ee.ImageCollectionchirps上使用mosaic():

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

对于reduceRegions(),我们需要使用ee.Image()getInfo():

def reduce_image(img):
    img_reduced = ee.Image(img).reduceRegions(
        reducer=ee.Reducer.mean(),
        collection=districts,
        scale=5500,
    ).getInfo()
    return img_reduced

总而言之,我们有:

file_name = '/content/drive/My Drive/Colab Notebooks/DATA_FOLDERS/SHP/gadm41_PRY_2.shx'
districts = gpd.read_file(file_name)

fc = []
for i in range(districts.shape[0]):
    g = districts.iloc[i:i + 1, :] 
    json_dict = eval(g.to_json()) 
    geo_json_dict = json_dict['features'][0] 
    fc.append(ee.Feature(geo_json_dict))

districts = ee.FeatureCollection(fc)

#startDate = ee.Date('2004-01-01')
startDate = ee.Date('2020-01-01')
endDate = ee.Date('2021-12-31')
chirps = ee.ImageCollection('UCSB-CHG/CHIRPS/DAILY').filterDate(startDate, endDate).select("precipitation").mosaic()

def reduce_image(img):
    img_reduced = ee.Image(img).reduceRegions(
        reducer=ee.Reducer.mean(),
        collection=districts,
        scale=5500,
    ).getInfo()
    return img_reduced

rainfall_reduced = reduce_image(chirps)

print(rainfall_reduced)项输出(I only included a subset since it's a million lines项):

...
      [-56.286949156999924, -24.767101287999935],
      [-56.28482055699993, -24.76206016599997],
      [-56.28269958499993, -24.757202148999852],
      [-56.28142547599998, -24.754322050999917],
      [-56.28020477399997, -24.751232146999882],
      [-56.28010559099994, -24.751117705999945]]]},
   'id': '217',
   'properties': {'CC_2': 'NA',
    'COUNTRY': 'Paraguay',
    'ENGTYPE_2': 'District',
    'GID_0': 'PRY',
    'GID_1': 'PRY.18_1',
    'GID_2': 'PRY.18.15_1',
    'HASC_2': 'PY.SP.YN',
    'NAME_1': 'San Pedro',
    'NAME_2': 'Yataity del Norte',
    'NL_NAME_1': 'NA',
    'NL_NAME_2': 'NA',
    'TYPE_2': 'Distrito',
    'VARNAME_2': 'NA',
    'mean': 0}}]}

Note:我不得不限制日期范围(即startDate),因为它是...大量数据后,我收到以下错误/警告消息:

IOPub data rate exceeded.
The notebook server will temporarily stop sending output
to the client in order to avoid crashing it.

Python相关问答推荐

剧作家Python没有得到回应

Python daskValue错误:无法识别的区块管理器dask -必须是以下之一:[]

try 在树叶 map 上应用覆盖磁贴

如何避免Chained when/then分配中的Mypy不兼容类型警告?

有症状地 destruct 了Python中的regex?

大小为M的第N位_计数(或人口计数)的公式

如何更改分组条形图中条形图的 colored颜色 ?

解决调用嵌入式函数的XSLT中表达式的语法移位/归约冲突

在不同的帧B中判断帧A中的子字符串,每个帧的大小不同

python sklearn ValueError:使用序列设置数组元素

计算空值

以异步方式填充Pandas 数据帧

Python 3试图访问在线程调用中实例化的类的对象

Python类型提示:对于一个可以迭代的变量,我应该使用什么?

使用Python TCP套接字发送整数并使用C#接收—接收正确数据时出错

需要帮助使用Python中的Google的People API更新联系人的多个字段'

如何在SQLAlchemy + Alembic中定义一个"Index()",在基表中的列上

如何在Python中从html页面中提取html链接?

将像素信息写入文件并读取该文件

根据过滤后的牛郎星图表中的数据计算新系列