-
Notifications
You must be signed in to change notification settings - Fork 3
/
geobricks_faostat_connector.py
171 lines (156 loc) · 4.27 KB
/
geobricks_faostat_connector.py
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import urllib
import urllib2
import json
from qgis.core import QgsMessageLog
BASE_URL = 'http://fenixservices.fao.org/faostat/api/v1/'
CODES_URL = 'codes/'
DATA_URL = 'data/'
def get_items(domain_code, lang='en'):
out = []
try:
r = BASE_URL + lang + '/codes/items/' + domain_code
except TypeError:
raise TypeError('Domain code is null')
req = urllib2.Request(r)
fp = urllib2.urlopen(req)
response = ''
while 1:
data = fp.read()
if not data:
break
response += data
json_data = response
items = json.loads(json_data)['data']
for item in items:
out.append({
'code': item['code'],
'label': item['label']
})
return out
def get_elements(domain_code, lang='en'):
out = []
try:
r = BASE_URL + lang + '/codes/elements/' + domain_code
except TypeError:
raise TypeError('Domain code is null')
req = urllib2.Request(r)
fp = urllib2.urlopen(req)
response = ''
while 1:
data = fp.read()
if not data:
break
response += data
json_data = response
try:
elements = json.loads(json_data)['data']
for element in elements:
out.append({
'code': element['code'],
'label': element['label']
})
return out
except ValueError:
raise ValueError('No elements available for this domain.')
def get_groups(lang='en'):
out = []
url = BASE_URL + lang + '/groups/'
req = urllib2.Request(url)
fp = urllib2.urlopen(req)
response = ''
while 1:
data = fp.read()
if not data:
break
response += data
json_data = response
groups = json.loads(json_data)['data']
for group in groups:
out.append({
'code': group['code'],
'label': group['label']
})
return out
def get_domains(groups_code, lang='en'):
out = []
blacklist = ['TM', 'FT', 'EA', 'HS']
url = BASE_URL + lang + '/domains/' + groups_code
req = urllib2.Request(url)
fp = urllib2.urlopen(req)
response = ''
while 1:
data = fp.read()
if not data:
break
response += data
json_data = response
domains = json.loads(json_data)['data']
for domain in domains:
if str(domain['code']) not in blacklist:
out.append({
'code': domain['code'],
'label': domain['label']
})
return out
def get_data(domain_code, element_code, item_code, lang='en'):
out = []
url = BASE_URL + lang + '/data/' + domain_code + '?'
values = {
'area': '_1',
'area_cs': 'FAO',
'element': element_code,
'item': item_code,
'year': '_1',
'show_code': True,
'show_unit': True,
'show_flags': False,
'null_values': False,
'limit': -1,
'output_type': 'objects'
}
data = urllib.urlencode(values, True)
data_url = str(url) + str(data)
fp = urllib2.urlopen(data_url)
response = ''
while 1:
data = fp.read()
if not data:
break
response += data
json_data = response
rows = json.loads(json_data)['data']
for row in rows:
if 'Value' in row and 'Area Code' in row:
out.append({
'code': row['Area Code'],
'value': row['Value'],
'year': row['Year']
})
if 'Value' in row and 'Country Code' in row:
out.append({
'code': row['Country Code'],
'value': row['Value'],
'year': row['Year']
})
return out
def create_years_parameter():
years = []
for y in range(1961, 2015):
years.append(str(y))
return years
def create_countries_parameter(domain_code, lang='en'):
out = []
r = BASE_URL + lang + '/codes/countries/' + domain_code
req = urllib2.Request(r)
fp = urllib2.urlopen(req)
response = ''
while 1:
data = fp.read()
if not data:
break
response += data
json_data = response
countries = json.loads(json_data)['data']
for country in countries:
out.append(str(country['code']))
return out