本文最后更新于 2025-11-28T10:27:30+08:00
人口数据来自GlobPOP: A 33-year (1990-2022) global gridded population dataset generated by cluster analysis and statistical learning链接 GDP数据来自Data for: Downscaled gridded global dataset for Gross Domestic Product (GDP) per capita at purchasing power parity (PPP) over 1990-2022链接
尽管zonodo上已经提供数据的下载链接,但是由于数据量过大,国内连接不稳定,不能按研究区域下载等原因,使用GEE下载会更加方便快捷
这里采用吴秋生老师提供的python的geemap库获取全国范围的以上数据
1、配置python环境: 安装geemap库,调用api进行下载操作
有关如何配置环境,吴秋生老师提供的的官方网站 已经介绍的很详细了,也可以根据youtube上的教程来
另外,这里虽然可以直接在自己的环境中安装geemap库,但是官方建议直接创建新环境安装,geemap会自动安装合适版本的所有依赖,避免环境中依赖库版本不适配带来的麻烦
2、使用GEE获取数据: 首先使用GEE要先注册GEE账号,具体操作可参考网上的教程,这里不再说明
使用geemap之前需要运行:
1 2 3 4 5 import eeimport geemap ee.Authenticate() ee.Initialize()
进行账号认证和初始化
gee下载数据到本地需要先上传到谷歌云盘再手动下载到本地,geemap提供了直接下载到本地的方法download_ee_image以及ee_export_image,(这里笔者在下载人口数据时使用download_ee_image由于网络接连问题并不能正常下载,最终仍是创建task上传到云盘下载)
这里以下载人口数据为例介绍:
1、获取数据集和边界数据 1 2 3 4 5 6 7 8 9 china_fc = ( ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017' ) .filter (ee.Filter.eq('country_na' , 'China' )) ) china_geom = china_fc.geometry() globpop = ee.ImageCollection('projects/sat-io/open-datasets/GLOBPOP_COUNT' )
这里直接使用了google earth提供的边界数据,可能会存在争议问题,建议自己上传边界使用
2、构建筛选裁剪函数 1 2 3 4 5 6 7 8 9 10 11 12 13 years = list (range (1990 , 2023 ))def get_clipped_year_image (year ): image = ( globpop .filter (ee.Filter.calendarRange(year, year, 'year' )) .mosaic() .clip(china_geom) .set ({'export_year' : year}) ) return image
按年筛选影像并设置年属性,按上面的边界裁剪
3、导出数据到本地或云盘 1 2 3 4 5 6 7 8 9 10 11 12 13 for year in years: year_img = get_clipped_year_image(year) task = ee.batch.Export.image.toDrive( image=year_img, description=f'China_Globpop_{year} ' , folder='GEE_Exports' , fileNamePrefix=f'china_globpop_{year} ' , region=china_geom, scale=1000 , maxPixels=1e13 ) task.start()
根据需要设置相关属性如scale栅格大小、fileNamePrefix文件前缀等,最后创建task上传至Google Drive
本地下载:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 output_dir = Path(r"You_path" ) output_dir.mkdir(parents=True , exist_ok=True ) proj = gdp.projection().getInfo() for year in range (1990 , 2023 ): image = ( china_collection .filter (ee.Filter.eq("year" , year)) .first() ) if image is None : print (f"{year} 年无数据,已跳过。" ) continue outfile = output_dir / f"china_gdp_per_capita_ppp_{year} .tif" geemap.ee_export_image( image, filename=str (outfile), region=china_geom, crs=proj["crs" ], crs_transform=proj["transform" ], file_per_band=False , timeout=600 ) print (f"{year} 年影像已保存:{outfile} " )
即可直接保存到本地
关于这两个函数笔者在运行时出现类似Crs Code is Unknown[….]的错误,这里查找了原因可能是由于proj.db的位置指向错误导致版本不符,可以设置环境变量解决
1 2 Set-Item Env:PROJ_LIB "C:\SoftWares\miniconda\envs\gee\Library\share\proj" Set-Item Env:GDAL_DATA "C:\SoftWares\miniconda\envs\gee\Library\share\gdal
完整代码已上传至Github
有疑问欢迎随时交流