-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathimportKml.py
746 lines (682 loc) · 26.9 KB
/
importKml.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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
# -*- coding: utf-8 -*-
"""
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
import os
import re
from qgis.PyQt.QtCore import QObject, QVariant, QCoreApplication, QUrl, pyqtSignal
from qgis.PyQt.QtGui import QIcon
from qgis.core import (
QgsCoordinateReferenceSystem, QgsPoint,
QgsLineString, QgsMultiLineString, QgsPolygon, QgsMultiPolygon,
QgsFeature, QgsGeometry, QgsFields, QgsField, QgsWkbTypes)
from qgis.core import (
QgsProcessingAlgorithm,
QgsProcessingParameterFile,
QgsProcessingException,
QgsProcessingParameterFeatureSink)
from zipfile import ZipFile
import xml.sax
import xml.sax.handler
# import traceback
epsg4326 = QgsCoordinateReferenceSystem("EPSG:4326")
def tr(string):
return QCoreApplication.translate('Processing', string)
class ImportKmlAlgorithm(QgsProcessingAlgorithm):
"""
Algorithm to import KML and KMZ files.
"""
PrmInput = 'Input'
PrmPointOutputLayer = 'PointOutputLayer'
PrmLineOutputLayer = 'LineOutputLayer'
PrmPolygonOutputLayer = 'PolygonOutputLayer'
def initAlgorithm(self, config):
self.addParameter(
QgsProcessingParameterFile(
self.PrmInput,
tr('Import KML/KMZ file (*.kml, *.txt, or *.kmz)'))
)
self.addParameter(
QgsProcessingParameterFeatureSink(
self.PrmPointOutputLayer,
tr('Output point layer'),
optional=True)
)
self.addParameter(
QgsProcessingParameterFeatureSink(
self.PrmLineOutputLayer,
tr('Output line layer'),
optional=True)
)
self.addParameter(
QgsProcessingParameterFeatureSink(
self.PrmPolygonOutputLayer,
tr('Output polygon layer'),
optional=True)
)
def processAlgorithm(self, parameters, context, feedback):
self.parameters = parameters
self.context = context
filename = self.parameterAsFile(parameters, self.PrmInput, context)
f, extension = os.path.splitext(filename)
extension = extension.lower()
kmz_doc_file = 'doc.kml'
try:
if extension == '.kmz':
kmz = ZipFile(filename, 'r')
try:
kml = kmz.open(kmz_doc_file, 'r')
except Exception:
# there was not a doc.kml file, lets see if there another kml file
files = kmz.namelist()
kmz_doc_file = None
for f in files:
if f.endswith('.kml'):
kmz_doc_file = f
break
if kmz_doc_file:
kml = kmz.open(kmz_doc_file, 'r')
else:
msg = "Couldn't find kml document in kmz file"
raise QgsProcessingException(msg)
elif extension == '.kml' or extension == '.txt':
kml = open(filename, encoding="utf-8", errors="backslashreplace")
else:
msg = "Invalid extension: Should be *.kml, *.txt, or *.kmz."
feedback.reportError(msg)
raise QgsProcessingException(msg)
except Exception:
msg = "Failed to open file."
raise QgsProcessingException(msg)
skipPt = True if self.PrmPointOutputLayer not in parameters or parameters[self.PrmPointOutputLayer] is None else False
skipline = True if self.PrmLineOutputLayer not in parameters or parameters[self.PrmLineOutputLayer] is None else False
skipPoly = True if self.PrmPolygonOutputLayer not in parameters or parameters[self.PrmPolygonOutputLayer] is None else False
self.cntPt = 0
self.cntLine = 0
self.cntPoly = 0
parser = xml.sax.make_parser()
# Do a pre-pass through the KML to see if there are any extended data fields
preprocess = PreProcessHandler()
parser.setContentHandler(preprocess)
try:
input_source = xml.sax.xmlreader.InputSource()
input_source.setByteStream(kml)
input_source.setEncoding('utf-8')
parser.parse(input_source)
except Exception:
preprocess.endDocument()
# Reset the KML/KMZ file to the beginning. The handler seems to automatically close the file
# which is why we are reopening it.
if extension == '.kmz':
kmz.close()
else:
kml.close()
if extension == '.kmz':
kmz = ZipFile(filename, 'r')
kml = kmz.open(kmz_doc_file, 'r')
else:
kml = open(filename, encoding="utf-8", errors="backslashreplace")
# Set up the handler for doing the main processing
self.extData = preprocess.getExtendedDataFields()
self.extData.sort()
self.extDataMap = {}
index = 0
for item in self.extData:
self.extDataMap[item] = index
index += 1
handler = PlacemarkHandler(skipPt, skipline, skipPoly, self.extDataMap, feedback)
handler.addpoint.connect(self.addpoint)
handler.addline.connect(self.addline)
handler.addpolygon.connect(self.addpolygon)
parser.setContentHandler(handler)
try:
input_source = xml.sax.xmlreader.InputSource()
input_source.setByteStream(kml)
input_source.setEncoding('utf-8')
parser.parse(input_source)
except Exception:
'''s = traceback.format_exc()
feedback.pushInfo(s)'''
feedback.pushInfo(tr('Failure in kml extraction - May return partial results.'))
handler.endDocument()
if extension == '.kmz':
kmz.close()
else:
kml.close()
if handler.hasGoundOverlay:
feedback.pushInfo(tr('NOTICE: This file may contain GroundOverlay images.'))
feedback.pushInfo(tr('Run "Raster->KML Tools->Extract KML/KMZ Ground Overlasys" to extract them if embedded.'))
feedback.pushInfo('')
feedback.pushInfo('{} points extracted'.format(self.cntPt))
feedback.pushInfo('{} lines extracted'.format(self.cntLine))
feedback.pushInfo('{} polygons extracted'.format(self.cntPoly))
r = {}
if self.cntPt > 0:
r[self.PrmPointOutputLayer] = self.dest_id_pt
if self.cntLine > 0:
r[self.PrmLineOutputLayer] = self.dest_id_line
if self.cntPoly > 0:
r[self.PrmPolygonOutputLayer] = self.dest_id_poly
return (r)
def addpoint(self, feature):
if self.cntPt == 0:
f = QgsFields()
f.append(QgsField("name", QVariant.String))
f.append(QgsField("folders", QVariant.String))
f.append(QgsField("description", QVariant.String))
f.append(QgsField("altitude", QVariant.Double))
f.append(QgsField("alt_mode", QVariant.String))
f.append(QgsField("time_begin", QVariant.String))
f.append(QgsField("time_end", QVariant.String))
f.append(QgsField("time_when", QVariant.String))
for item in self.extData:
f.append(QgsField(item, QVariant.String))
(self.sinkPt, self.dest_id_pt) = self.parameterAsSink(
self.parameters,
self.PrmPointOutputLayer, self.context, f,
QgsWkbTypes.PointZ, epsg4326)
self.cntPt += 1
self.sinkPt.addFeature(feature)
def addline(self, feature):
if self.cntLine == 0:
f = QgsFields()
f.append(QgsField("name", QVariant.String))
f.append(QgsField("folders", QVariant.String))
f.append(QgsField("description", QVariant.String))
f.append(QgsField("altitude", QVariant.Double))
f.append(QgsField("alt_mode", QVariant.String))
f.append(QgsField("time_begin", QVariant.String))
f.append(QgsField("time_end", QVariant.String))
f.append(QgsField("time_when", QVariant.String))
for item in self.extData:
f.append(QgsField(item, QVariant.String))
(self.sinkLine, self.dest_id_line) = self.parameterAsSink(
self.parameters,
self.PrmLineOutputLayer, self.context, f,
QgsWkbTypes.MultiLineStringZ, epsg4326)
self.cntLine += 1
self.sinkLine.addFeature(feature)
def addpolygon(self, feature):
if self.cntPoly == 0:
f = QgsFields()
f.append(QgsField("name", QVariant.String))
f.append(QgsField("folders", QVariant.String))
f.append(QgsField("description", QVariant.String))
f.append(QgsField("altitude", QVariant.Double))
f.append(QgsField("alt_mode", QVariant.String))
f.append(QgsField("time_begin", QVariant.String))
f.append(QgsField("time_end", QVariant.String))
f.append(QgsField("time_when", QVariant.String))
for item in self.extData:
f.append(QgsField(item, QVariant.String))
(self.sinkPoly, self.dest_id_poly) = self.parameterAsSink(
self.parameters,
self.PrmPolygonOutputLayer, self.context, f,
QgsWkbTypes.MultiPolygonZ, epsg4326)
self.cntPoly += 1
self.sinkPoly.addFeature(feature)
def name(self):
return 'importkml'
def icon(self):
return QIcon(os.path.dirname(__file__) + '/icons/import.svg')
def displayName(self):
return tr('Import KML/KMZ')
def group(self):
return tr('Vector conversion')
def groupId(self):
return 'vectorconversion'
def helpUrl(self):
file = os.path.dirname(__file__) + '/index.html'
if not os.path.exists(file):
return ''
return QUrl.fromLocalFile(file).toString(QUrl.FullyEncoded)
def createInstance(self):
return ImportKmlAlgorithm()
class PlacemarkHandler(xml.sax.handler.ContentHandler, QObject):
addpoint = pyqtSignal(QgsFeature)
addline = pyqtSignal(QgsFeature)
addpolygon = pyqtSignal(QgsFeature)
def __init__(self, skipPt, skipLine, skipPoly, extDataMap, feedback):
QObject.__init__(self)
xml.sax.handler.ContentHandler.__init__(self)
self.schema = {}
self.skipPt = skipPt
self.skipLine = skipLine
self.skipPoly = skipPoly
self.extDataMap = extDataMap
self.feedback = feedback
self.extDataSize = len(extDataMap)
self.hasGoundOverlay = False
self.inPlacemark = False
self.resetSettings()
self.folders = []
def resetSettings(self):
'''Set all settings to a default new placemark.'''
self.inFolder = False
self.inName = False
self.inDescription = False
self.inCoordinates = False
self.inLatitude = False
self.inLongitude = False
self.inAltitude = False
self.inAltitudeMode = False
self.inLocation = False
self.inOuterBoundary = False
self.inInnerBoundary = False
self.inTimeSpan = False
self.inBegin = False
self.inEnd = False
self.inTimeStamp = False
self.inWhen = False
self.inExtendedData = False
self.inData = False
self.inSimpleData = False
self.inDataValue = False
self.inPoint = False
self.inLineString = False
self.inPolygon = False
self.lineStrings = [] # List of QgsLineString
self.ptPts = []
self.ptAltitude = []
self.polygons = []
self.innerPoly = []
self.outerPoly = ""
self.folder = ""
self.name = ""
self.description = ""
self.coord = ""
self.lon = ""
self.lat = ""
self.altitude = ""
self.altitudeMode = ""
self.begin = ""
self.end = ""
self.when = ""
self.dataName = None
self.dataValue = ""
self.extendedData = {}
def schemaBaseLookup(self, name):
if name in self.schema:
return(self.schema[name])
return(name)
def addSchema(self, name, parent):
parent = self.schemaBaseLookup(parent)
self.schema[name] = parent
def startElement(self, name, attr):
if name.startswith('kml:'):
name = name[4:]
if name == "Schema":
n = None
p = None
for (k, v) in list(attr.items()):
if k == 'name':
n = v
if k == 'parent':
p = v
if n and p:
self.addSchema(n, p)
name = self.schemaBaseLookup(name)
if name == "Folder":
self.inFolder = True
self.name = ""
elif self.inFolder and not self.inPlacemark and name == "name":
self.inName = True
self.name = ""
elif name == "Placemark":
self.inPlacemark = True
self.resetSettings()
elif self.inPlacemark:
if name == "Point":
self.inPoint = True
elif name == "LineString":
self.inLineString = True
elif name == "Polygon":
self.inPolygon = True
elif name == "Location":
self.inLocation = True
elif name == "name":
self.inName = True
self.name = ""
elif name == "description":
self.inDescription = True
self.description = ""
elif name == "coordinates":
self.inCoordinates = True
self.coord = ""
elif name == "outerBoundaryIs":
self.inOuterBoundary = True
self.coord = ""
elif name == "innerBoundaryIs":
self.inInnerBoundary = True
self.coord = ""
elif name == "TimeSpan":
self.inTimeSpan = True
elif name == "TimeStamp":
self.inTimeStamp = True
elif name == "begin":
self.inBegin = True
elif name == "end":
self.inEnd = True
elif name == "when":
self.inWhen = True
elif name == "longitude" and self.inLocation:
self.inLongitude = True
self.lon = ""
elif name == "latitude" and self.inLocation:
self.inLatitude = True
self.lat = ""
elif name == "altitude" and self.inLocation:
self.inAltitude = True
self.altitude = ""
elif name == "altitudeMode":
self.inAltitudeMode = True
self.altitudeMode = ""
elif name == "Data" and self.inExtendedData:
self.inData = True
self.dataName = None
for (k, v) in list(attr.items()):
if k == 'name':
self.dataName = v
elif name == "SimpleData" and self.inExtendedData:
self.inSimpleData = True
self.dataName = None
for (k, v) in list(attr.items()):
if k == 'name':
self.dataName = v
elif name == 'value' and self.inData:
self.inDataValue = True
elif name == "ExtendedData":
self.inExtendedData = True
elif name == 'GroundOverlay':
self.hasGoundOverlay = True
def characters(self, data):
if self.inName: # on text within tag
self.name += data # save text if in title
elif self.inDescription:
self.description += data
elif self.inCoordinates:
self.coord += data
elif self.inLongitude and self.inLocation:
self.lon += data
elif self.inLatitude and self.inLocation:
self.lat += data
elif self.inAltitude and self.inLocation:
self.altitude += data
elif self.inBegin and self.inTimeSpan:
self.begin += data
elif self.inEnd and self.inTimeSpan:
self.end += data
elif self.inWhen and self.inTimeStamp:
self.when += data
elif self.inAltitudeMode:
self.altitudeMode += data
elif self.inDataValue:
self.dataValue += data
elif self.inSimpleData:
self.dataValue += data
def endElement(self, name):
if name.startswith('kml:'):
name = name[4:]
name = self.schemaBaseLookup(name)
if self.inPlacemark:
if name == "name":
self.inName = False # on end title tag
self.name = self.name.strip()
elif name == "description":
self.inDescription = False
self.description = self.description.strip()
elif name == "Point":
self.processPoint(self.coord)
self.inPoint = False
elif name == "LineString":
self.processLineString(self.coord)
self.inLineString = False
elif name == "coordinates":
self.inCoordinates = False
if self.inPolygon:
if self.inOuterBoundary:
self.outerPoly = self.coord.strip()
elif self.inInnerBoundary:
self.innerPoly.append(self.coord.strip())
else:
self.coord = self.coord.strip()
elif name == "longitude" and self.inLocation:
self.inLongitude = False
self.lon = self.lon.strip()
elif name == "latitude" and self.inLocation:
self.inLatitude = False
self.lat = self.lat.strip()
elif name == "altitude" and self.inLocation:
self.inAltitude = False
self.altitude = self.altitude.strip()
elif name == "Location":
self.processLocation(self.lon, self.lat, self.altitude)
self.inLocation = False
self.inLongitude = False
self.inLatitude = False
self.inAltitude = False
elif name == "Polygon":
self.processPolygon()
self.inPolygon = False
self.inOuterBoundary = False
self.inInnerBoundary = False
self.outerPoly = ""
self.innerPoly = []
elif name == "outerBoundaryIs":
self.inOuterBoundary = False
elif name == "innerBoundaryIs":
self.inInnerBoundary = False
elif name == "TimeSpan":
self.inTimeSpan = False
elif name == "TimeStamp":
self.inTimeStamp = False
elif name == "begin":
self.inBegin = False
elif name == "end":
self.inEnd = False
elif name == "when":
self.inWhen = False
elif name == "altitudeMode":
self.inAltitudeMode = False
elif name == "Placemark":
self.process(self.name, self.description, self.altitudeMode, self.begin, self.end, self.when)
self.inPlacemark = False
self.resetSettings()
elif name == 'Data' and self.inExtendedData:
self.inData = False
elif name == 'SimpleData' and self.inExtendedData:
self.inSimpleData = False
if self.dataName:
self.extendedData[self.dataName] = self.dataValue.strip()
self.dataValue = ''
elif name == 'value' and self.inData:
if self.dataName:
self.extendedData[self.dataName] = self.dataValue.strip()
self.inDataValue = False
self.dataValue = ''
elif name == 'ExtendedData':
self.inExtendedData = False
self.inData = False
elif name == 'Folder':
self.inFolder = False
if len(self.folders) > 0:
del self.folders[-1]
elif self.inFolder:
if name == 'name':
self.inName = False # on end title tag
self.inFolder = False
self.name = self.name.strip()
self.folders.append(self.name)
def folderString(self):
if len(self.folders) > 0:
return(u"; ".join(self.folders))
else:
return("")
def processLineString(self, coord):
if self.skipLine:
return
lineStr = coord2ptsZ(coord)
self.lineStrings.append(lineStr)
def processPoint(self, coord):
if self.skipPt:
return
c = coord.split(',')
lat = 0.0
lon = 0.0
altitude = 0.0
try:
lon = float(c[0])
lat = float(c[1])
if len(c) >= 3:
altitude = float(c[2])
except Exception:
return
pt = QgsPoint(lon, lat, altitude)
self.ptPts.append(pt)
self.ptAltitude.append(altitude)
def processLocation(self, lon, lat, altitude):
if self.skipPt:
return
try:
lon = float(lon)
lat = float(lat)
except Exception:
return
altitude = 0.0
try:
if altitude:
altitude = float(altitude)
except Exception:
pass
pt = QgsPoint(lon, lat, altitude)
self.ptPts.append(pt)
self.ptAltitude.append(altitude)
def processPolygon(self):
if self.skipPoly:
return
interior_ring = []
poly = QgsPolygon()
outerLineStr = coord2ptsZ(self.outerPoly)
poly.setExteriorRing(outerLineStr)
if len(self.innerPoly) > 0:
for p in self.innerPoly:
innerLineStr = coord2ptsZ(p)
interior_ring.append(innerLineStr)
poly.setInteriorRings(interior_ring)
self.polygons.append(poly)
def process(self, name, desc, alt_mode, begin, end, when):
if self.extDataSize > 0:
extAttr = [''] * self.extDataSize
for key in self.extendedData.keys():
if key in self.extDataMap:
extAttr[self.extDataMap[key]] = self.extendedData[key]
# POINTS
if len(self.ptPts) != 0:
for x, pt in enumerate(self.ptPts):
feature = QgsFeature()
feature.setGeometry(QgsGeometry(pt))
attr = [name, self.folderString(), desc, self.ptAltitude[x], alt_mode, begin, end, when]
if self.extDataSize > 0:
attr.extend(extAttr)
feature.setAttributes(attr)
self.addpoint.emit(feature)
# LINES - lineStrings is a list of QgsLineString
if len(self.lineStrings) != 0:
feature = QgsFeature()
if len(self.lineStrings) == 1:
feature.setGeometry(QgsGeometry(self.lineStrings[0]))
else:
g = QgsMultiLineString()
for lineStr in self.lineStrings:
g.addGeometry(lineStr)
feature.setGeometry(QgsGeometry(g))
attr = [name, self.folderString(), desc, 0, alt_mode, begin, end, when]
if self.extDataSize > 0:
attr.extend(extAttr)
feature.setAttributes(attr)
self.addline.emit(feature)
# POLYGONS
if len(self.polygons) != 0:
feature = QgsFeature()
if len(self.polygons) == 1:
feature.setGeometry(QgsGeometry(self.polygons[0]))
else:
g = QgsMultiPolygon()
for poly in self.polygons:
g.addGeometry(poly)
feature.setGeometry(QgsGeometry(g))
attr = [name, self.folderString(), desc, 0, alt_mode, begin, end, when]
if self.extDataSize > 0:
attr.extend(extAttr)
feature.setAttributes(attr)
self.addpolygon.emit(feature)
def coord2ptsZ(coords):
lineStr = QgsLineString()
coords = coords.strip()
clist = re.split('\s+', coords)
for pt in clist:
c = pt.split(',')
if len(c) >= 6:
'''This is invalid KML syntax, but given some KMLs have been formatted
this way the invalid exception is looked for. There should be a space
between line string coordinates. This looks for a comma between them and
also assumes it is formatted as lat,lon,altitude,lat,lon,altitude...'''
i = 0
while i < len(c) - 1:
try:
lon = float(c[i])
lat = float(c[i + 1])
except Exception:
lon = 0.0
lat = 0.0
try:
altitude = float(c[i + 2])
except Exception:
altitude = 0
lineStr.addVertex(QgsPoint(lon, lat, altitude))
i += 3
else:
altitude = 0
try:
lon = float(c[0])
lat = float(c[1])
if len(c) >= 3:
altitude = float(c[2])
except Exception:
lon = 0.0
lat = 0.0
lineStr.addVertex(QgsPoint(lon, lat, altitude))
return(lineStr)
class PreProcessHandler(xml.sax.handler.ContentHandler, QObject):
def __init__(self):
QObject.__init__(self)
xml.sax.handler.ContentHandler.__init__(self)
self.extendedData = set()
self.inExtendedData = False
def startElement(self, name, attr):
if name == "Data" and self.inExtendedData:
self.dataName = None
for (k, v) in list(attr.items()):
if k == 'name':
if v:
self.extendedData.add(v)
elif name == "SimpleData" and self.inExtendedData:
self.dataName = None
for (k, v) in list(attr.items()):
if k == 'name':
if v:
self.extendedData.add(v)
elif name == "ExtendedData":
self.inExtendedData = True
def endElement(self, name):
if name == 'ExtendedData':
self.inExtendedData = False
def getExtendedDataFields(self):
return(list(self.extendedData))