-
Notifications
You must be signed in to change notification settings - Fork 0
/
shapefile.py
503 lines (363 loc) · 17.6 KB
/
shapefile.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
'''
Python:
accept epsg as argument
copy into new DB
create structure for 3nf
add geometry columns
Call Ruby:
Write responses into 3nf
write geometries into 3nf tables
call shapefile tool
TODO:
convert ruby calls into rbenv or system ruby calls
figure out how shell script wrapper needs to work for exporter
'''
import unicodedata
import sqlite3
import csv, codecs, cStringIO
from xml.dom import minidom
import sys
import pprint
import glob
import json
import os
import shutil
import re
import zipfile
import subprocess
import glob
import tempfile
import errno
import imghdr
import bz2
import tarfile
import platform
import lsb_release
from collections import defaultdict
import zipfile
try:
import zlib
compression = zipfile.ZIP_DEFLATED
except:
compression = zipfile.ZIP_STORED
modes = { zipfile.ZIP_DEFLATED: 'deflated',
zipfile.ZIP_STORED: 'stored',
}
print sys.argv
class UTF8Recoder:
"""
Iterator that reads an encoded stream and reencodes the input to UTF-8
"""
def __init__(self, f, encoding):
self.reader = codecs.getreader(encoding)(f)
def __iter__(self):
return self
def next(self):
return self.reader.next().encode("utf-8")
class UnicodeReader:
"""
A CSV reader which will iterate over lines in the CSV file "f",
which is encoded in the given encoding.
"""
def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
f = UTF8Recoder(f, encoding)
self.reader = csv.reader(f, dialect=dialect, **kwds)
def next(self):
row = self.reader.next()
return [unicode(s, "utf-8") for s in row]
def __iter__(self):
return self
class UnicodeWriter:
"""
A CSV writer which will write rows to CSV file "f",
which is encoded in the given encoding.
"""
def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
# Redirect output to a queue
self.queue = cStringIO.StringIO()
self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
self.stream = f
self.encoder = codecs.getincrementalencoder(encoding)()
def convertBuffer(self, obj):
#print type(obj)
if isinstance(obj, basestring):
#print obj.encode("utf-8", errors="replace")
return obj.encode("utf-8", errors="replace").replace('"',"''")
if isinstance(obj, buffer):
bufferCon = sqlite3.connect(':memory:')
bufferCon.enable_load_extension(True)
bufferCon.load_extension(LIBSPATIALITE)
foo = bufferCon.execute("select astext(?);", ([obj])).fetchone()
return foo[0]
if obj == None:
return ""
return obj
def writerow(self, row):
self.writer.writerow(['"%s"' % self.convertBuffer(s) for s in row])
# Fetch UTF-8 output from the queue ...
data = self.queue.getvalue()
data = data.decode("utf-8")
# ... and reencode it into the target encoding
data = self.encoder.encode(data)
# write to the target stream
self.stream.write(data.replace('"""','"').replace('"None"',''))
# empty queue
self.queue.truncate(0)
self.stream.flush()
def writerows(self, rows):
for row in rows:
self.writerow(row)
def dict_factory(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
def upper_repl(match):
if (match.group(1) == None):
return ""
return match.group(1).upper()
def clean(str):
out = re.sub(" ([a-z])|[^A-Za-z0-9]+", upper_repl, str)
return out
def cleanWithUnder(str):
out = re.sub("[^a-zA-Z0-9]+", "_", str)
return out
def makeSurePathExists(path):
try:
os.makedirs(path)
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
originalDir = sys.argv[1]
exportDir = tempfile.mkdtemp()+"/"
finalExportDir = sys.argv[2]+"/"
importDB = originalDir+"db.sqlite3"
exportDB = exportDir+"shape.sqlite3"
jsondata = json.load(open(originalDir+'module.settings'))
srid = jsondata['srid']
arch16nFiles=[]
for file in glob.glob(originalDir+"*.properties"):
arch16nFiles.append(file)
arch16nFile = next((s for s in arch16nFiles if '.0.' in s), arch16nFiles[0])
# print jsondata
moduleName = clean(jsondata['name'])
fileNameType = "Identifier" #Original, Unchanged, Identifier
if lsb_release.get_lsb_information()['RELEASE'] == '16.04':
LIBSPATIALITE = 'mod_spatialite.so'
else:
LIBSPATIALITE = 'libspatialite.so.5'
images = None
overrideFormattedIdentifiers = None
try:
foo= json.load(open(sys.argv[3],"r"))
# print foo["Export Images and Files?"]
if (foo["Export Images and Files?"] != []):
images = True
else:
images = False
# Ugh. But the interface is buggy.
if (foo["Export identifier components in plain as well as formatted state (if in doubt, leave the setting as is)?"] != []):
overrideFormattedIdentifiers = False
else:
overrideFormattedIdentifiers = True
except:
sys.stderr.write("Json input failed")
images = True
print "Exporting Images %s" % (images)
print "Overwriting Identifier FormatStrings %s" % (overrideFormattedIdentifiers)
print foo
def zipdir(path, zip):
for root, dirs, files in os.walk(path):
for file in files:
zip.write(os.path.join(root, file))
try:
os.remove(exportDB)
except OSError:
pass
importCon = sqlite3.connect(importDB)
importCon.enable_load_extension(True)
importCon.load_extension(LIBSPATIALITE)
exportCon = sqlite3.connect(exportDB)
exportCon.enable_load_extension(True)
exportCon.load_extension(LIBSPATIALITE)
exportCon.execute("select initSpatialMetaData(1)")
'''
for line in importCon.iterdump():
try:
exportCon.execute(line)
except sqlite3.Error:
pass
'''
exifCon = sqlite3.connect(exportDB)
exifCon.row_factory = dict_factory
exportCon.enable_load_extension(True)
exportCon.load_extension(LIBSPATIALITE)
exportCon.execute("create table keyval (key text, val text);")
f = open(arch16nFile, 'r')
for line in f:
if "=" in line:
keyval = line.replace("\n","").replace("\r","").decode("utf-8").split('=', 1)
keyval[0] = '{'+keyval[0]+'}'
exportCon.execute("replace into keyval(key, val) VALUES(?, ?)", keyval)
f.close()
for aenttypeid, aenttypename in importCon.execute("select aenttypeid, aenttypename from aenttype"):
aenttypename = clean(aenttypename)
attributes = ['identifier', 'createdBy', 'createdAtGMT', 'modifiedBy', 'modifiedAtGMT']
for attr in importCon.execute("select distinct attributename from attributekey join idealaent using (attributeid) where aenttypeid = ? group by attributename order by aentcountorder", [aenttypeid]):
attrToInsert = clean(attr[0])
if attrToInsert not in attributes:
attributes.append(attrToInsert)
attribList = " TEXT, \n\t".join(attributes)
createStmt = "Create table if not exists %s (\n\tuuid TEXT PRIMARY KEY,\n\t%s TEXT);" % (aenttypename, attribList)
exportCon.execute(createStmt)
geometryColumns = []
for row in importCon.execute("select aenttypename, geometrytype(geometryn(geospatialcolumn,1)) as geomtype, count(distinct geometrytype(geometryn(geospatialcolumn,1))) from latestnondeletedarchent join aenttype using (aenttypeid) where geomtype is not null group by aenttypename having count(distinct geometrytype(geometryn(geospatialcolumn,1))) = 1"):
geometryColumns.append(row[0])
geocolumn = "select addGeometryColumn('%s', 'geospatialcolumn', %s, '%s', 'XY');" %(clean(row[0]),srid,row[1]);
exportCon.execute(geocolumn)
for aenttypename, uuid, createdAt, createdBy, modifiedAt, modifiedBy,geometry in importCon.execute("select aenttypename, uuid, createdAt || ' GMT', createdBy, datetime(modifiedAt) || ' GMT', modifiedBy, geometryn(transform(geospatialcolumn,casttointeger(%s)),1) from latestnondeletedarchent join aenttype using (aenttypeid) join createdModifiedAtBy using (uuid) order by createdAt" % (srid)):
if (aenttypename in geometryColumns):
insert = "insert into %s (uuid, createdAtGMT, createdBy, modifiedAtGMT, modifiedBy, geospatialcolumn) VALUES(?, ?, ?, ?, ?, ?)" % (clean(aenttypename))
exportCon.execute(insert, [str(uuid), createdAt, createdBy, modifiedAt, modifiedBy, geometry])
else:
insert = "insert into %s (uuid, createdAtGMT, createdBy, modifiedAtGMT, modifiedBy) VALUES(?, ?, ?, ?, ?)" % (clean(aenttypename))
exportCon.execute(insert, [str(uuid), createdAt, createdBy, modifiedAt, modifiedBy])
try:
os.remove(exportDir+'shape.out')
except OSError:
pass
subprocess.call(["bash", "./format.sh", originalDir, exportDir, exportDir])
updateArray = []
for line in codecs.open(exportDir+'shape.out', 'r', encoding='utf-8').readlines():
out = line.replace("\n","").replace("\\r","").split("\t")
#print "!!%s -- %s!!" %(line, out)
if (len(out) ==4):
update = "update %s set %s = ? where uuid = %s;" % (clean(out[1]), clean(out[2]), out[0])
data = (unicode(out[3].replace("\\n","\n").replace("'","''")),)
# print update, data
exportCon.execute(update, data)
if (overrideFormattedIdentifiers):
subprocess.call(["bash", "./override.sh", originalDir, exportDir, exportDir])
for line in codecs.open(exportDir+'override.out', 'r', encoding='utf-8').readlines():
out = line.replace("\n","").replace("\\r","").split("\t")
#print "!!%s -- %s!!" %(line, out)
if (len(out) ==4):
update = "update %s set %s = ? where uuid = %s;" % (clean(out[1]), clean(out[2]), out[0])
data = (unicode(out[3].replace("\\n","\n").replace("'","''")),)
# print update, data
exportCon.execute(update, data)
exportCon.commit()
files = ['shape.sqlite3']
if (overrideFormattedIdentifiers):
files.append('override.out')
files.append('shape.out')
if images:
for directory in importCon.execute("select distinct aenttypename, attributename from latestnondeletedaentvalue join attributekey using (attributeid) join latestnondeletedarchent using (uuid) join aenttype using (aenttypeid) where attributeisfile is not null and measure is not null"):
makeSurePathExists("%s/%s/%s" % (exportDir,clean(directory[0]), clean(directory[1])))
filehash = defaultdict(int)
print "* File list exported:"
for filename in importCon.execute("select uuid, measure, freetext, certainty, attributename, aenttypename from latestnondeletedaentvalue join attributekey using (attributeid) join latestnondeletedarchent using (uuid) join aenttype using (aenttypeid) where attributeisfile is not null and measure is not null"):
try:
oldPath = filename[1].split("/")
oldFilename = oldPath[2]
aenttypename = clean(filename[5])
attributename = clean(filename[4])
newFilename = "%s/%s/%s" % (aenttypename, attributename, oldFilename)
if os.path.isfile(originalDir+filename[1]):
if (fileNameType == "Identifier"):
# print filename[0]
filehash["%s%s" % (filename[0], attributename)] += 1
foo = exportCon.execute("select identifier from %s where uuid = %s" % (aenttypename, filename[0]))
identifier=cleanWithUnder(foo.fetchone()[0])
r= re.search("(\.[^.]*)$",oldFilename)
delimiter = ""
if filename[2]:
delimiter = "a"
newFilename = "%s/%s/%s_%s%s%s" % (aenttypename, attributename, identifier, filehash["%s%s" % (filename[0], attributename)],delimiter, r.group(0))
exifdata = exifCon.execute("select * from %s where uuid = %s" % (aenttypename, filename[0])).fetchone()
iddata = []
for id in importCon.execute("select coalesce(measure, vocabname, freetext) from latestnondeletedarchentidentifiers where uuid = %s union select aenttypename from latestnondeletedarchent join aenttype using (aenttypeid) where uuid = %s" % (filename[0], filename[0])):
iddata.append(id[0])
shutil.copyfile(originalDir+filename[1], exportDir+newFilename)
mergedata = exifdata.copy()
mergedata.update(jsondata)
mergedata.pop("geospatialcolumn", None)
exifjson = {"SourceFile":exportDir+newFilename,
"UserComment": [json.dumps(mergedata)],
"ImageDescription": exifdata['identifier'],
"XPSubject": "Annotation: %s" % (filename[2]),
"Keywords": iddata,
"Artist": exifdata['createdBy'],
"XPAuthor": exifdata['createdBy'],
"Software": "FAIMS Project",
"ImageID": exifdata['uuid'],
"Copyright": jsondata['name']
}
with open(exportDir+newFilename+".json", "w") as outfile:
json.dump(exifjson, outfile)
if imghdr.what(exportDir+newFilename):
subprocess.call(["exiftool", "-m", "-q", "-sep", "\"; \"", "-overwrite_original", "-j=%s" % (exportDir+newFilename+".json"), exportDir+newFilename])
exportCon.execute("update %s set %s = ? where uuid = ?" % (aenttypename, attributename), (newFilename, filename[0]))
print " * %s" % (newFilename)
files.append(newFilename+".json")
files.append(newFilename)
else:
print "<b>Unable to find file %s, from uuid: %s" % (originalDir+filename[1], filename[0])
except:
print "<b>Unable to find file (exception thrown) %s, from uuid: %s" % (originalDir+filename[1], filename[0])
# check input flag as to what filename to export
for row in importCon.execute("select aenttypename, geometrytype(geometryn(geospatialcolumn,1)) as geomtype, count(distinct geometrytype(geometryn(geospatialcolumn,1))) from latestnondeletedarchent join aenttype using (aenttypeid) where geomtype is not null group by aenttypename having count(distinct geometrytype(geometryn(geospatialcolumn,1))) = 1"):
cmd = ["spatialite_tool", "-e", "-shp", "%s" % (clean(row[0]).decode("ascii")), "-d", "%sshape.sqlite3" % (exportDir), "-t", "%s" % (clean(row[0])), "-c", "utf-8", "-g", "geospatialcolumn", "-s", "%s" % (srid), "--type", "%s" % (row[1])]
files.append("%s.dbf" % (clean(row[0])))
files.append("%s.shp" % (clean(row[0])))
files.append("%s.shx" % (clean(row[0])))
# print cmd
subprocess.call(cmd, cwd=exportDir)
for at in importCon.execute("select aenttypename from aenttype"):
aenttypename = "%s" % (clean(at[0]))
cursor = exportCon.cursor()
try:
cursor.execute("select * from %s" % (aenttypename))
except:
cursor.execute("select * from %s" % (aenttypename))
files.append("Entity-%s.csv" % (aenttypename))
csv_writer = UnicodeWriter(open(exportDir+"Entity-%s.csv" % (aenttypename), "wb+"))
csv_writer.writerow([i[0] for i in cursor.description]) # write headers
csv_writer.writerows(cursor)
#spatialite_tool -e -shp surveyUnitTransectBuffer -d db.sqlite3 -t surveyUnitWithTransectBuffer -c utf-8 -g surveyBuffer --type polygon
relntypequery = '''select distinct relntypeid, relntypename from relntype join latestnondeletedrelationship using (relntypeid);'''
relnquery = '''select parent.uuid as fromuuid, child.uuid as touuid, parent.participatesverb
from (select *
from latestnondeletedaentreln
join relationship using (relationshipid)
where relationshipid in (select relationshipid
from relationship
join relntype using (relntypeid)
where relntypename = ?)
) parent
join (latestnondeletedaentreln
join relationship using (relationshipid)
) child on (parent.relationshipid = child.relationshipid and parent.uuid != child.uuid)
join user using (userid)'''
relntypecursor = importCon.cursor()
relncursor = importCon.cursor()
for relntypeid, relntypename in relntypecursor.execute(relntypequery):
relncursor.execute(relnquery, [relntypename])
exportCon.execute("CREATE TABLE %s (parentuuid TEXT, childuuid TEXT, participatesverb TEXT);" % (clean(relntypename)))
for i in relncursor:
exportCon.execute("INSERT INTO %s VALUES(?,?,?)" % (clean(relntypename)), i)
files.append("Relationship-%s.csv" % (clean(relntypename)))
relncursor.execute(relnquery, [relntypename])
csv_writer = UnicodeWriter(open(exportDir+"Relationship-%s.csv" % (clean(relntypename)), "wb+"))
csv_writer.writerow([i[0] for i in relncursor.description]) # write headers
csv_writer.writerows(relncursor)
tarf = tarfile.open("%s/%s-export.tar.bz2" % (finalExportDir,moduleName), 'w:bz2')
try:
for file in files:
tarf.add(exportDir+file, arcname=moduleName+'/'+file)
finally:
tarf.close()
try:
os.remove(exportDir)
except OSError:
pass