-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_location.py
347 lines (298 loc) · 12 KB
/
check_location.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import gzip
import logging
import re
from fiona import listlayers
from geopandas import read_file
from glob import glob
from os import mkdir, remove
from os.path import basename, dirname, join
from pandas import isna, read_excel
from requests import head
from shutil import copyfileobj, rmtree
from zipfile import ZipFile, is_zipfile
from hdx.data.dataset import Dataset
from hdx.data.hdxobject import HDXError
from hdx.location.country import Country
from hdx.utilities.uuid import get_uuid
logger = logging.getLogger(__name__)
def get_global_pcodes(dataset_info, retriever, locations=None):
dataset = Dataset.read_from_hdx(dataset_info["dataset"])
resource = [r for r in dataset.get_resources() if r["name"] == dataset_info["name"]]
headers, iterator = retriever.get_tabular_rows(resource[0]["url"], dict_form=True)
pcodes = {"WORLD": []}
miscodes = {"WORLD": []}
for row in iterator:
pcode = row[dataset_info["p-code"]]
iso3_code = row[dataset_info["admin"]]
if locations and len(locations) > 0 and iso3_code not in locations and "WORLD" not in locations:
continue
if iso3_code in pcodes:
pcodes[iso3_code].append(pcode)
else:
pcodes[iso3_code] = [pcode]
if iso3_code in miscodes:
miscodes[iso3_code].append(pcode)
else:
miscodes[iso3_code] = [pcode]
pcodes["WORLD"].append(pcode)
iso2_code = Country.get_iso2_from_iso3(iso3_code)
if iso3_code not in pcode and iso2_code not in pcode:
continue
pcode_no0 = pcode.replace("0", "")
miscodes[iso3_code].append(pcode_no0)
miscodes["WORLD"].append(pcode_no0)
if iso3_code in pcode_no0:
miscode = pcode_no0.replace(iso3_code, iso2_code)
miscodes[iso3_code].append(miscode)
miscodes["WORLD"].append(miscode)
continue
if iso2_code in pcode_no0:
miscode = pcode_no0.replace(iso2_code, iso3_code)
miscodes[iso3_code].append(miscode)
miscodes["WORLD"].append(miscode)
for iso in miscodes:
miscodes[iso] = list(set(miscodes[iso]))
miscodes[iso].sort()
return pcodes, miscodes
def download_resource(resource, fileext, retriever):
try:
resource_file = retriever.download_file(resource["url"])
except:
error = f"Unable to download file"
return None, None, error
if fileext in ["xls", "xlsx"] and ".zip" not in basename(resource_file):
resource_files = [resource_file]
return resource_files, None, None
if is_zipfile(resource_file) or ".zip" in basename(resource_file) or ".gz" in basename(resource_file):
parent_folder = join(retriever.temp_dir, get_uuid())
parent_folders = [parent_folder, resource_file]
if ".gz" in basename(resource_file):
try:
mkdir(parent_folder)
with gzip.open(resource_file, "rb") as gz:
with open(join(parent_folder, basename(resource_file.replace(".gz", ".gpkg"))), "wb") as gz_out:
copyfileobj(gz, gz_out)
except:
error = f"Unable to unzip resource"
return None, parent_folders, error
else:
try:
with ZipFile(resource_file, "r") as z:
z.extractall(parent_folder)
except:
error = f"Unable to unzip resource"
return None, parent_folders, error
resource_files = glob(join(parent_folder, "**", f"*.{fileext}"), recursive=True)
if len(resource_files) > 1: # make sure to remove directories containing the actual files
resource_files = [r for r in resource_files
if sum([r in rs for rs in resource_files if not rs == r]) == 0]
if fileext == "xlsx" and len(resource_files) == 0:
resource_files = [resource_file]
if fileext in ["gdb", "gpkg"]:
resource_files = [join(r, i) for r in resource_files for i in listlayers(r)]
elif fileext in ["gdb", "gpkg"] and ".zip" not in basename(resource_file) and ".gz" not in basename(resource_file):
resource_files = [join(resource_file, r) for r in listlayers(resource_file)]
parent_folders = [resource_file]
else:
resource_files = [resource_file]
parent_folders = None
return resource_files, parent_folders, None
def read_downloaded_data(resource_files, fileext):
data = dict()
error = None
for resource_file in resource_files:
if fileext in ["xlsx", "xls"]:
try:
contents = read_excel(
resource_file, sheet_name=None, nrows=200
)
except:
error = f"Unable to read resource"
continue
for key in contents:
if contents[key].empty:
continue
data[get_uuid()] = parse_tabular(contents[key], fileext)
if fileext == "csv":
try:
contents = read_file(resource_file, rows=200)
data[get_uuid()] = parse_tabular(contents, fileext)
except:
error = f"Unable to read resource"
continue
if fileext in ["geojson", "json", "shp", "topojson"]:
try:
data = {
get_uuid(): read_file(resource_file, rows=200)
}
except:
error = f"Unable to read resource"
continue
if fileext in ["gdb", "gpkg"]:
try:
data = {
get_uuid(): read_file(dirname(resource_file), layer=basename(resource_file), rows=200)
}
except:
error = f"Unable to read resource"
continue
return data, error
def parse_tabular(df, fileext):
df = df.dropna(how="all", axis=0).dropna(how="all", axis=1).reset_index(drop=True)
df.columns = [str(c) for c in df.columns]
if all([bool(re.match("Unnamed.*", c)) for c in df.columns]): # if all columns are unnamed, move down a row
df.columns = [str(c) if not isna(c) else f"Unnamed: {i}" for i, c in enumerate(df.loc[0])]
df = df.drop(index=0).reset_index(drop=True)
if not all(df.dtypes == "object"): # if there are mixed types, probably read correctly
return df
if len(df) == 1: # if there is only one row, return
return df
hxlrow = None # find hxl row and incorporate into header
i = 0
while i < 10 and i < len(df) and hxlrow is None:
hxltags = [bool(re.match("#.*", t)) if t else True for t in df.loc[i].astype(str)]
if all(hxltags):
hxlrow = i
i += 1
if hxlrow is not None:
columns = []
for c in df.columns:
cols = [str(col) for col in df[c][:hxlrow + 1] if col]
if "Unnamed" not in c:
cols = [c] + cols
columns.append("||".join(cols))
df.columns = columns
df = df.drop(index=range(hxlrow + 1)).reset_index(drop=True)
return df
if fileext == "csv" and not hxlrow: # assume first row of csv is header if there are no hxl tags
return df
columns = []
datarow = 3
if hxlrow:
datarow = hxlrow + 1
if len(df) < 3:
datarow = len(df)
for c in df.columns:
cols = [str(col) for col in df[c][:datarow] if col]
if "Unnamed" not in c:
cols = [c] + cols
columns.append("||".join(cols))
df.columns = columns
df = df.drop(index=range(datarow)).reset_index(drop=True)
return df
def check_pcoded(df, pcodes, miscodes=False):
pcoded = None
header_exp = "((adm)?.*p?.?cod.*)|(#\s?adm\s?\d?\+?\s?p?(code)?)"
for h in df.columns:
if pcoded:
break
headers = h.split("||")
pcoded_header = any([bool(re.match(header_exp, hh, re.IGNORECASE)) for hh in headers])
if not pcoded_header:
continue
column = df[h].dropna().astype("string").str.upper()
column = column[~column.isin(["NA", "NAN", "NONE", "NULL", ""])]
if len(column) == 0:
continue
if miscodes:
column = column.str.replace("0", "")
matches = sum(column.isin(pcodes))
pcnt_match = matches / len(column)
if pcnt_match >= 0.9:
pcoded = True
return pcoded
def remove_files(files=None, folders=None):
if files:
to_delete = files
if folders:
to_delete = files + folders
elif folders:
to_delete = folders
for f in to_delete:
try:
remove(f)
except (FileNotFoundError, NotADirectoryError, TypeError):
pass
try:
rmtree(f)
except (FileNotFoundError, NotADirectoryError, TypeError):
pass
def process_resource(
resource, dataset, global_pcodes, global_miscodes, retriever, configuration, update=True, cleanup=True
):
pcoded = None
mis_pcoded = None
locations = dataset.get_location_iso3s()
pcodes = [pcode for iso in global_pcodes for pcode in global_pcodes[iso] if iso in locations]
miscodes = [pcode for iso in global_miscodes for pcode in global_miscodes[iso] if iso in locations]
filetype = resource.get_file_type()
fileext = filetype
if fileext == "geodatabase":
fileext = "gdb"
if fileext == "geopackage":
fileext = "gpkg"
if dataset.get_organization()["name"] in configuration["org_exceptions"]:
pcoded = False
if filetype.lower() not in configuration["allowed_filetypes"]:
pcoded = False
if pcoded is None:
size = resource["size"]
if (size is None or size == 0) and resource["resource_type"] == "api":
try:
resource_info = head(resource["url"])
# if size cannot be determined, set to the limit set in configuration so the resource is excluded
size = int(resource_info.headers.get("Content-Length", configuration["resource_size"]))
except:
size = configuration["resource_size"]
if size >= configuration["resource_size"]:
pcoded = False
if pcoded is False:
return pcoded, mis_pcoded
resource_files, parent_folders, error = download_resource(resource, fileext, retriever)
if not resource_files:
if cleanup:
remove_files(folders=parent_folders)
if error:
logger.error(f"{dataset['name']}: {resource['name']}: {error}")
return None, None
contents, error = read_downloaded_data(resource_files, fileext)
if len(contents) == 0:
if cleanup:
remove_files(resource_files, parent_folders)
if error:
logger.error(f"{dataset['name']}: {resource['name']}: {error}")
return None, None
for key in contents:
if pcoded:
break
pcoded = check_pcoded(contents[key], pcodes)
if pcoded:
if cleanup:
remove_files(resource_files, parent_folders)
if error:
logger.error(f"{dataset['name']}: {resource['name']}: {error}")
return pcoded, mis_pcoded
for key in contents:
if mis_pcoded:
break
mis_pcoded = check_pcoded(contents[key], miscodes, miscodes=True)
if not error and pcoded is None:
pcoded = False
if mis_pcoded:
logger.warning(f"{dataset['name']}: {resource['name']}: may be mis-pcoded")
if error:
logger.error(f"{dataset['name']}: {resource['name']}: {error}")
if cleanup:
remove_files(resource_files, parent_folders)
if update:
try:
dataset.update_in_hdx(
hxl_update=False,
operation="patch",
batch_mode="KEEP_OLD",
skip_validation=True,
ignore_check=True,
)
except HDXError:
logger.exception(f"Could not update resource {resource['id']} in dataset {dataset['name']}")
return pcoded, mis_pcoded