forked from wmgeolab/geoBoundaries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildCore.py
executable file
·544 lines (443 loc) · 20.7 KB
/
buildCore.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
import os
import pandas as pd
from datetime import datetime
import glob
from collections import Counter
from joblib import Parallel, delayed, parallel_backend
from os.path import expanduser
import re
import requests
from pathlib import Path
import ciso8601
import zipfile
import time
import shutil
import fiona
import shapely
import subprocess
import shapely
import shapely.geometry
from collections import OrderedDict
home = expanduser("~")
userKey = os.environ.get('USER')
def geoLog(timestamp, errorType, errorMessage):
folderPath = home + "/gbRelease/buildLogs/" + timestamp + "/"
if not os.path.exists(folderPath):
os.makedirs(folderPath)
filePath = folderPath + errorType + ".txt"
while True:
try:
with open(filePath, 'a') as f:
f.write(errorMessage + "\n")
except:
break
else:
return 0
###################################################
###################################################
###################################################
###################################################
###################################################
###################################################
#Core Class
###################################################
###################################################
###################################################
###################################################
###################################################
class geoBoundary:
"Class with helper functions to build geoBoundaries releases."
#import classImports.geoLog as geoLog
#import classImports.preProcessing as preProcessing
def __init__(self,gbMeta, nightlyVersion, home):
self.iso = str(gbMeta["Processed File Name"])[:3]
self.adm = str(gbMeta["Processed File Name"])[4:8]
self.source1 = str(gbMeta["Source 1"])
self.source2 = str(gbMeta["Source 2"])
self.version = str(nightlyVersion)
self.license = str(gbMeta["License"])
self.home = home
self.gDriveURL = str(gbMeta["gDriveID"])
try:
self.year = str(int(gbMeta["Year"]))
except:
self.year = "None"
self.geoLog("WARN", ("ISO " + self.iso + " | " + self.adm +
" does not have a valid year."))
self.lastUpdate = str(gbMeta["Last Updated Date"])
def geoLog(self, errorType, errorMessage):
folderPath = self.home + "/gbRelease/buildLogs/" + self.version + "/"
if not os.path.exists(folderPath):
os.makedirs(folderPath)
filePath = folderPath + errorType + ".txt"
while True:
try:
with open(filePath, 'a') as f:
f.write(errorMessage + "\n")
except:
break
else:
return 0
def metaCheck(self):
self.metaFail = False
#ISO Valid Check
validISOList = pd.read_csv(self.home + "/gbRelease/gbRawData/ISO_0_Standards/ISO_3166_1_Alpha_3.csv", encoding="ISO-8859-1")
if(not self.iso in validISOList["Alpha-3code"].values):
self.geoLog("CRITICAL", ("ISO " +
self.iso + " is invalid."))
self.metaFail = True
#Boundary Type Valid Check
if(not self.adm in
["ADM0", "ADM1", "ADM2", "ADM3", "ADM4", "ADM5"]):
self.geoLog("CRITICAL", ("ADM " +
self.adm + " is invalid."))
self.metaFail = True
#Source Exists Check
if((len(self.source1) < 1) and (len(self.source2) < 1)):
self.geoLog("CRITICAL", ("ISO " + self.iso + " | " + self.adm +
" does not have a valid source for the file origination."))
self.metaFail = True
#License Valid Type Check
licenses = list(
pd.read_csv(self.home + "/gbRelease/gbRawData/gbLicenses.txt",
header=None)[0])
if(not (self.license in licenses)):
self.geoLog("CRITICAL",("ISO " + self.iso + " | " + self.adm +
" does not have a valid license."))
self.metaFail = True
def checkForUpdatesDownload(self, previousCSV):
self.retrieveFail = False
self.metaChange = False
#Check if the metadata has changed since the last version
previousCSV = previousCSV[(previousCSV["boundaryISO"] == self.iso) &
(previousCSV["boundaryType"] == self.adm)]
if(len(previousCSV) == 1):
if(str(previousCSV["boundaryYear"]) != self.year):
self.metaChange = True
if(str(previousCSV["boundarySource-1"]) != self.source1):
self.metaChange = True
if(str(previousCSV["boundarySource-2"]) != self.source2):
self.metaChange = True
if(str(previousCSV["boundaryLicense"]) != self.license):
self.metaChange = True
else:
self.geoLog("INFO",("ISO " + self.iso + " | " + self.adm +
" was not in the last release, so cannot be compared for changes."))
#Check if the file on google drive is different than what
#we have local
self.remoteFileDiff = True
#expected path to zip
zipDir = home + "/gbRelease/gbRawData/currentZips/"
self.zipPath = zipDir + self.iso + "_" + self.adm + ".zip"
if(not os.path.isfile(os.path.join(zipDir, (self.iso + "_" + self.adm + ".zip")))):
self.remoteFileDiff = True
self.geoLog("INFO",("ISO " + self.iso + " | " + self.adm +
" did not have a local file copy. Downloading."))
else:
rCloneCall = (home + "/libs/rclone -v --drive-impersonate " +
userKey + " check remote:releaseCandidates/"+
"gbReleaseCandidate_current/" + self.adm + "/" +
self.iso + "_" + self.adm + ".zip " + zipDir)
process = subprocess.Popen([rCloneCall], shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
process.wait()
output, error = process.communicate()
if(("0 differences found" in str(error)) and os.path.isfile(os.path.join(zipDir, (self.iso + "_" + self.adm + ".zip")))):
#We already have a matching file, so we can skip this download.
self.remoteFileDiff = False
if((self.metaChange == True) and (self.remoteFileDiff == False)):
self.geoLog("WARN",("ISO " + self.iso + " | " + self.adm +
" Metadata changed, but the file did not."))
if((self.metaChange == False) and (self.remoteFileDiff == True)):
self.geoLog("WARN",("ISO " + self.iso + " | " + self.adm +
" File changed, but the metadata did not."))
if(self.remoteFileDiff == True):
self.geoLog("INFO",("ISO " + self.iso + " | " + self.adm +
" Downloading new copy from remote."))
rCloneCall = (home + "/libs/rclone -v --drive-impersonate " +
userKey + " copy remote:releaseCandidates/"+
"gbReleaseCandidate_current/" + self.adm + "/" +
self.iso + "_" + self.adm + ".zip " + zipDir)
dlProcess = subprocess.Popen([rCloneCall], shell=True)
dlProcess.wait()
if((dlProcess.returncode != 0) or (not os.path.isfile(os.path.join(zipDir, (self.iso + "_" + self.adm + ".zip"))))):
self.geoLog("CRITICAL",("ISO " + self.iso + " | " + self.adm +
" did not have a local file copy, and the download failed."))
self.retrieveFail = True
return False
rawUnzipTarget = (self.home + "/gbRelease/gbRawData/current/" +
self.iso + "/" + self.adm + "/")
if(((self.retrieveFail == False) and (self.remoteFileDiff == True)) or (not os.path.isdir(rawUnzipTarget))):
#Delete any old data and unzip the new data
if(not os.path.isdir((self.home + "/gbRelease/gbRawData/current/" +
self.iso + "/"))):
while True:
try:
os.mkdir((self.home + "/gbRelease/gbRawData/current/" +
self.iso + "/"))
except:
pass
else:
break
if(os.path.isdir(rawUnzipTarget)):
shutil.rmtree(rawUnzipTarget)
os.mkdir(rawUnzipTarget)
if(not os.path.isdir(rawUnzipTarget)):
os.mkdir(rawUnzipTarget)
try:
with zipfile.ZipFile(self.zipPath, "r") as zipObj:
zipObj.extractall(rawUnzipTarget)
except:
self.geoLog("CRITICAL", ("ISO " + self.iso + " | " + self.adm + ": had an invalid zip file."))
self.retrieveFail = True
return False
def shapeCheckBuild(self):
self.shapeFail = False
#Basic geometry checks
#Only do these if the file hasn't already been processed.
correctHome = (self.home +
"/gbRelease/gbRawData/current/" +
self.iso + "/" + self.adm + "/shapeFixes/")
corrected_shp = (self.home +
"/gbRelease/gbRawData/current/" +
self.iso + "/" + self.adm + "/shapeFixes/" +
self.iso + "_" + self.adm + "_fixedInternalTopology.shp")
shpPath = (self.home +
"/gbRelease/gbRawData/current/" +
self.iso + "/" + self.adm + "/" +
self.iso + "_" + self.adm + ".shp")
if(not os.path.isdir(correctHome)):
os.mkdir(correctHome)
if(not os.path.isfile(os.path.join(correctHome, (self.iso + "_" + self.adm + "_fixedInternalTopology.shp")))):
try:
shpFile = fiona.open(shpPath)
except:
self.geoLog("CRITICAL", ("ISO " + self.iso + " | " + self.adm + ": had an invalid shapefile."))
self.shapeFail = True
return 0
if(not shpFile.crs['init'] == "epsg:4326"):
self.geoLog("CRITICAL", ("ISO " + self.iso + " | " + self.adm + ": had an invalid projection."))
self.shapeFail = True
return 0
xmin = shpFile.bounds[0]
ymin = shpFile.bounds[1]
xmax = shpFile.bounds[2]
ymax = shpFile.bounds[3]
tol = 1e-12
valid = ((xmin >= -180-tol) and (xmax <= 180+tol) and (ymin >= -90-tol) and (ymax <= 90+tol))
if not valid:
self.geoLog("CRITICAL", ("ISO " + self.iso + " | " + self.adm + ": bounds were in another castle."))
self.shapeFail = True
return 0
#Shapefile Topology checks
valid = True
error = None
fixed = []
for feature in shpFile:
try:
raw_shape = shapely.geometry.shape(feature['geometry'])
valid = raw_shape.is_valid
except:
self.geoLog("CRITICAL", ("ISO " + self.iso + " | " + self.adm + ": At least one feature is topologically invalid and cannot be fixed automatically."))
self.shapeFail = True
return 0
if valid:
fixed.append(feature)
if not valid:
fixed_shape = raw_shape.buffer(0)
fix_valid = fixed_shape.is_valid
if fix_valid and error is None:
self.geoLog("WARN", ("ISO " + self.iso + " | " + self.adm + ": At least one feature had a minor topological issue, fixed by shapely buffer=0."))
feature["geometry"] = shapely.geometry.mapping(fixed_shape)
fixed.append(feature)
elif not fix_valid:
if error is not None:
self.geoLog("CRITICAL", ("ISO " + self.iso + " | " + self.adm + ":CRITICAL ERROR: An error in the geometry of the file " + shpPath + " exists that we could not automatically fix."))
self.shapeFail = True
return 0
else:
self.geoLog("CRITICAL", ("ISO " + self.iso + " | " + self.adm + "CRITICAL ERROR: A really bad error in the geometry of the file" + shpPath + "exists that we could not automatically fix."))
self.shapeFail = True
return 0
try:
#Attribute schema checks and fixes
#Only doing these in case of a file update.
schema = shpFile.schema.copy()
#First, identify which of the existing columns represent:
validNameColumns = ['Name', 'name', 'NAME']
validISOcolumns = ['ISO', 'ISO_code', 'ISO_Code', 'iso']
dictName = None
dictISO = None
for attribute in list(schema['properties'].items()):
if(attribute[0] in validNameColumns):
dictName = attribute[0]
if(attribute[0] in validISOcolumns):
dictISO = attribute[0]
if(dictName == None):
self.geoLog("WARN", ("ISO " + self.iso + " | " + self.adm + ": No name information was found in a validly named column for: " + shpPath))
schema["properties"]["shapeName"] = 'str:254'
else:
#Rename our name field to the schema field.
schema["properties"] = OrderedDict([('shapeName', v) if k==dictName else (k,v) for k,v in schema["properties"].items()])
if(dictISO == None):
self.geoLog("WARN", ("ISO " + self.iso + " | " + self.adm + ": No ISO information was found in a validly named column for: " + shpPath))
schema["properties"]["shapeISO"] = 'str:10'
else:
schema["properties"] = OrderedDict([('shapeISO', v) if k==dictISO else (k,v) for k,v in schema["properties"].items()])
#remove invalid elements
for attribute in list (schema['properties'].items()):
if(attribute[0] not in ["shapeISO", "shapeName"]):
schema['properties'].pop(attribute[0])
#Add additional schema elements
schema["properties"]["shapeID"] = 'str:10'
schema["properties"]["shapeGroup"] = 'str:50'
schema["properties"]["shapeType"] = 'str:10'
#Repeat for ordered dicts representing each feature.
year = self.year
featureID = 0
for elem in fixed:
featureID = featureID + 1
if(dictName == None):
elem["properties"]["shapeName"] = 'None'
else:
elem["properties"] = OrderedDict([('shapeName', v) if k==dictName else (k,v) for k,v in elem["properties"].items()])
if(dictISO == None):
elem["properties"]["shapeISO"] = 'None'
else:
elem["properties"] = OrderedDict([('shapeISO', v) if k==dictISO else (k,v) for k,v in elem["properties"].items()])
#remove invalid elements
for attribute in list (elem['properties'].items()):
if(attribute[0] not in ["shapeISO", "shapeName"]):
elem['properties'].pop(attribute[0])
elem["properties"]["shapeID"] = (self.iso + "-" + self.adm + "-" + self.version + '-B' + str(featureID))
elem["properties"]["shapeGroup"] = self.iso
elem["properties"]["shapeType"] = self.adm
except:
self.geoLog("CRITICAL", ("ISO " + self.iso + " | " + self.adm + ": schema could not be corrected."))
self.shapeFail = True
return 0
#Update the original file with a corrected version:
try:
with fiona.open(corrected_shp, 'w', 'ESRI Shapefile', schema, shpFile.crs) as output:
for elem in fixed:
output.write(elem)
except:
self.geoLog("CRITICAL", ("ISO " + self.iso + " | " + self.adm + ": update did not write correctly."))
self.shapeFail = True
def gbBuild (nightlyVersion, gbMeta, home, previousCSV):
try:
boundary = geoBoundary(gbMeta, nightlyVersion, home)
except ValueError as e:
geoLog(nightlyVersion, "CRITICAL", "A boundary failed to initialize. Here is what we know: " + str(e) + "\n" + str(gbMeta))
return 0
boundary.metaCheck()
if(boundary.metaFail):
return 0
boundary.checkForUpdatesDownload(previousCSV)
if(boundary.retrieveFail):
return 0
boundary.shapeCheckBuild()
#Remove the last iteration to ensure a full file check
if(os.path.isdir(home + "/gbRelease/gbRawData/current/")):
shutil.rmtree(home + "/gbRelease/gbRawData/current/")
os.mkdir(home + "/gbRelease/gbRawData/current/")
#Grab the most recent geoBoundaries metadata
sheet = "https://docs.google.com/spreadsheets/d/1SJSpZxgM4rKw9Qb8tj7qtL2G6E3GKLrJL0s-QyTIYIU/edit?usp=sharing"
csv = sheet.replace('/edit?usp=sharing', '/export?format=csv')
currentCSV = pd.read_csv(csv)
#for now, remove OCHA cases
currentCSV = currentCSV[currentCSV["geoBoundaryType"] != "OCHA"]
#Grab the last pushed build to create delta
#Identify the most recent version of geoBoundaries
r = requests.get("https://www.geoboundaries.org/gbRequest.html?ISO=USA&ADM=ADM0")
lastVersion = (re.search("[0-9]_[0-9]_[0-9]", r.json()[0]
['boundaryID'])[0])
previousCSV = pd.read_csv("https://www.geoboundaries.org/data/geoBoundaries-" +
lastVersion + "/geoBoundaries-" + lastVersion + ".csv")
#Define build timestamp (version ID)
nightlyVersion = str(round(datetime.timestamp(datetime.now()),0))[:-2]
#Save the current version
currentCSV.to_csv("./gbRelease/gbRawData/metadata/" + nightlyVersion + ".csv")
#Critical error if there are any duplicates (same ISO and ADM level)
criticalDuplicate = currentCSV["Processed File Name"].value_counts() > 1
if(len(criticalDuplicate[criticalDuplicate > 1]) > 0):
geoLog(nightlyVersion, "CRITICAL",
"There is at least one duplicate ISO code / ADM level combination.")
#Critical error if there are any duplicate google drive links
criticalDuplicate = currentCSV["gDriveID"].value_counts() > 1
if(len(criticalDuplicate[criticalDuplicate > 1]) > 0):
geoLog(nightlyVersion, "CRITICAL",
"There is at least one duplicate Google Drive link.")
#Check if any boundaries have been added or removed vs. the last recorded metadata.
#Flag these in the logs if so, with a WARN tag.
prevCases = previousCSV["boundaryISO"] + "_" + previousCSV["boundaryType"] + ".zip"
caseCounts = pd.DataFrame.from_dict(
Counter(prevCases.tolist() +
currentCSV["Processed File Name"].tolist()), orient= "index")
caseCounts = caseCounts[caseCounts[0] != 2]
if(len(caseCounts) > 0):
geoLog(nightlyVersion, "WARN", "NEW OR REMOVED: \n" + caseCounts.to_string())
#Begin high precision single country build for each country.
with parallel_backend("loky", inner_max_num_threads=1):
(Parallel(n_jobs=-2, verbose=100)
(delayed(gbBuild)
(nightlyVersion, currentCSV.iloc[i], home, previousCSV)
for i in range(len(currentCSV))))
############################################################
############################################################
#Metadata Standardization for Release
def metaStandardization(metaData, version):
metaData["boundaryID"] = "ERROR"
metaData["boundaryISO"] = "ERROR"
metaData["boundaryYear"] = "ERROR"
metaData["boundaryType"] = "ERROR"
metaData["boundarySource-1"] = "ERROR"
metaData["boundarySource-2"] = "ERROR"
metaData["boundaryLicense"] = "ERROR"
metaData["licenseDetail"] = "ERROR"
metaData["licenseSource"] = "ERROR"
metaData["boundarySourceURL"] = "ERROR"
metaData["boundaryUpdate"] = "ERROR"
metaData["downloadURL"] = "ERROR"
bndCnt = 0
for i, row in metaData.iterrows():
bndCnt = bndCnt + 1
rowISO = row["Processed File Name"][:3]
rowADM = row["Boundary Level"].replace(" ","")
rowGroup = ["boundaryGroup"]
#Create unique ID for each row
metaData.at[i,'boundaryID'] = rowISO + "-" + rowADM + "-" + version + "-G" + str(bndCnt)
#Save ISO for each row
metaData.at[i, 'boundaryISO'] = rowISO
#Save Year
metaData.at[i, "boundaryYear"] = row["Year"]
#Save Type
metaData.at[i, "boundaryType"] = rowADM
#Save sources
metaData.at[i, "boundarySource-1"] = row["Source 1"]
metaData.at[i, "boundarySource-2"] = row["Source 2"]
#License
metaData.at[i, "boundaryLicense"] = row["License"]
#License Detail
metaData.at[i, "licenseDetail"] = row["License Detail"]
#License Source
metaData.at[i, "licenseSource"] = row["License Source"]
#Boundary Source URL
metaData.at[i, "boundarySourceURL"] = row["Link to Source Data"]
if(not str(row["Last Updated Date"]).find("-") == -1):
metaData.at[i, "boundaryUpdate"] = str(pd.to_datetime(row["Last Updated Date"], format='%Y-%m-%d', errors="coerce").date())
else:
metaData.at[i, "boundaryUpdate"] = str(pd.to_datetime('today', format='%Y-%m-%d', errors='ignore').date())
#Download URL
fullURL = "https://geoboundaries.org/data/geoBoundaries-" + version + "/" + rowISO + "/" + rowADM + "/"
fullFile = "geoBoundaries-" + version + "-" + rowISO + "-" + rowADM + "-all.zip"
metaData.at[i, "downloadURL"] = fullURL + fullFile
cleanMeta = metaData[["boundaryID", "boundaryISO", "boundaryYear", "boundaryType",
"boundarySource-1", "boundarySource-2", "boundaryLicense", "licenseDetail",
"licenseSource", "boundarySourceURL", "boundaryUpdate", "downloadURL"]]
#Generate the final CSV for the package.
cleanMeta.to_csv(home + "/gbRelease/gbRawData/current/geoBoundaries-" + version + ".csv", index=False)
try:
metaStandardization(currentCSV, nightlyVersion)
except:
geoLog(nightlyVersion, "CRITICAL", "The metadata build failed.")