从粮农组织(FAO)下载数据
faodata的Python项目详细描述
一个简单的python接口,用于从粮农组织(fao)下载数据。
什么是福田?
- faodata是一个简单的python接口,用于从 联合国粮食及农业组织 (FAO)。
- 包使用FAO API。
- 用于绘制数据的国家边界来自Natural Earth (1:110m resolution)
安装
pip install faodata或下载source code和 python setup.py install
基本用途
要下载数据,数据库、数据集和字段的id是 必需:
获取粮农组织数据库列表:
fromfaodataimportfaodownloaddatabases=faodownload.get_databases()
获取给定数据库(如faostat)中粮农组织数据集的列表:
database_id='faostat'datasets=faodownload.get_datasets(database_id)
获取给定数据集中fao字段的列表(例如live prod):
database_id='faostat'dataset_id='live-prod'fields=faodownload.get_fields(database_id,dataset_id)
当已知所有前面的元素时,下载过程是
database_id='faostat'dataset_id='live-prod'field_id='m5111'# Define the year (if None, all years are retrieved)year=2010# Define country (if None, all countries are retrieved)# The country id is the ISO3 code# see https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3country_id=None# Get datadata=faodownload.get_data(database_id,dataset_id,field_id,country=country_id,year=year)
下载数据后,它可以显示在世界地图上
importnumpyasnpimportmatplotlib.pyplotaspltfrommpl_toolkitsimportbasemapfromfaodataimportfaodownload,faomap# Download datadatabase_id='faostat'dataset_id='live-prod'field_id='m5111'year=2013data=faodownload.get_data(database_id,dataset_id,field_id,year=year)# Select dataitem='Cattle'idx=data['Item']==itemdata=data.loc[idx,['country','value']]# Instantiate matplotlib and basemap objectsplt.close('all')fig,ax=plt.subplots()map=basemap.Basemap(projection='robin', \ lon_0=10,lat_0=50,ax=ax)map.drawcoastlines(color='grey')map.drawcountries(color='grey')# Categorize data according to percentilescat=[np.percentile(data['value'],pp) \ forppinrange(10,100,10)]# Draw plotfaomap.plot(map,data,cat,ndigits=0)map.ax.legend(loc=3)ax.set_title('%s population, %d'%(item,year),fontsize=15)# Add a footer to the figure to# indicate data sourcefaomap.mapfooter(fig,database_id,dataset_id,field_id)
example folder目录中的更多示例。