我们需要使用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.ImageCollection
或chirps
上使用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.