-
Notifications
You must be signed in to change notification settings - Fork 2
/
schema.py
358 lines (267 loc) · 9.92 KB
/
schema.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
'''
Created on 17 sept. 2015
@author: arxit
'''
import os.path
import xml.etree.ElementTree as ET
from PyQt4.QtCore import QFile, QIODevice, QVariant
from qgis.core import *
import main
class GeometryType:
'''
Geometry types
'''
POINT='POINT'
POLYLINE='LINESTRING'
POLYGON='POLYGON'
class DataType:
'''
Data types
'''
STRING='string'
INTEGER='integer'
DOUBLE='double'
DATE='date'
XSD_GEOMETRY_TYPES = {'LUREF':GeometryType.POINT,
'gml:CurvePropertyType':GeometryType.POLYLINE,
'gml:SurfacePropertyType':GeometryType.POLYGON}
XSD_QGIS_DATATYPE_MAP = {DataType.STRING:QVariant.String,
DataType.INTEGER:QVariant.LongLong,
DataType.DOUBLE:QVariant.Double,
DataType.DATE:QVariant.String}
XSD_QGIS_GEOMETRYTYPE_MAP = {GeometryType.POINT:QGis.Point,
GeometryType.POLYLINE:QGis.Line,
GeometryType.POLYGON:QGis.Polygon}
class PAGSchema(object):
'''
The PAG schema parsed from the XSD
'''
def __init__(self):
'''
Constructor
'''
self.parseXSD()
def parseXSD(self):
'''
Parses the XSD
'''
xsd_path = os.path.join(
main.plugin_dir,
'assets',
'PAGschema.xsd')
# Parse as QgsGmlSchema
file = QFile(xsd_path)
file.open(QIODevice.ReadOnly)
xsdcontent = file.readAll()
schema = QgsGmlSchema()
schema.parseXSD(xsdcontent)
#Parse as XML
ns = {'xsd': 'http://www.w3.org/2001/XMLSchema'} # XSD namespace
xsd = ET.parse(xsd_path)
topics = list() # Topics : PAG, ARTIKEL17, GESTION
concrete_typenames = list()
# Loop GML schema type names
for typename in schema.typeNames():
# Remove GEOMETRIE types
if '.' in typename:
continue
# Topics ILI
if len(schema.fields(typename))==1 and schema.fields(typename)[0].name() == 'member':
topics.append((typename,self._getTopicMembers(schema.fields(typename)[0].typeName(), xsd.getroot(), ns)))
continue
# Concretes types
concrete_typenames.append(typename)
self.types = list()
# Loop concrete type names and parse to PAGType
for typename in concrete_typenames:
xml_element = xsd.getroot().find('xsd:complexType[@name="{}Type"]'.format(typename),ns)
pag_type = PAGType()
pag_type.parse(typename, xml_element, ns)
self.types.append(pag_type)
# Add topic to types
for topic, members in topics:
for member in members:
for type in self.types:
if type.name == member:
type.name = '{}.{}'.format(topic,member)
break
def getType(self, typename):
'''
Get a type from the name
:param typename: The type name (ex : BIOTOPE_LIGNE)
:type typename: str, QString
'''
for type in self.types:
if type.friendlyName() == typename:
return type
return None
def getTypeFromTableName(self, tablename):
'''
Get a type from the table name
:param tablename: The table name (ex : ARTIKEL17.BIOTOPE_LIGNE)
:type tablename: str, QString
'''
for type in self.types:
if type.name == tablename:
return type
return None
def _getTopicMembers(self, typename, xml_root, ns):
'''
Parse XML node
:param typename: The type name of the topic (ex : PAGMemberType)
:type typename: str, QString
:param ns: XSD namespaces
:type ns: dict
'''
xml_element = xml_root.find('xsd:complexType[@name="{}"]'.format(typename), ns)
members_elements = xml_element.findall('.//xsd:choice/xsd:element', ns)
members = list()
for element in members_elements:
name = element.get('ref')
if '.' not in name: # Remove geometrie type
members.append(name)
return members
class PAGType(object):
'''
A PAG XSD type
'''
def __init__(self):
'''
Constructor
'''
self.geometry_type = None
self.fields = list()
def parse(self, name, xml_element, ns):
'''
Parse XML node
:param name: The name of the type
:type name: str, QString
:param xml_element: The root XML node of the type (xsd:complexType)
:type xml_element: Element
:param ns: XSD namespaces
:type ns: dict
'''
# Type name
self.name = name
self.geometry_type = None
self.geometry_fieldname = None
self.fields = list()
self.ordered_field_names = list()
sequence = xml_element.find('.//xsd:sequence',ns)
elements = sequence.findall('xsd:element',ns)
for element in elements:
# Process geometry
element_type = element.get('type')
if element_type in XSD_GEOMETRY_TYPES:
self.geometry_type = XSD_GEOMETRY_TYPES[element_type]
self.geometry_fieldname = element.get('name')
# Process field
else:
pag_field = PAGField()
pag_field.parse(element, ns)
self.fields.append(pag_field)
self.ordered_field_names.append(element.get('name'))
def friendlyName(self):
'''
Gets the friendly name of the type aka table name
E.g. BIOTOPE_LIGNE for ARTIKEL17.BIOTOPE_LIGNE
'''
if self.name is None:
return ''
split = self.name.split('.')
return split[-1]
def topic(self):
'''
Gets the topic of the type
E.g. ARTIKEL17 for ARTIKEL17.BIOTOPE_LIGNE
'''
if self.name is None:
return ''
split = self.name.split('.')
return split[0]
def getField(self, fieldname):
'''
Get a field from the name
:param fieldname: The field name (ex : CODE)
:type fieldname: str, QString
'''
for field in self.fields:
if field.name == fieldname:
return field
return None
class PAGField(object):
'''
A PAG XSD field
'''
def __init__(self):
'''
Constructor
'''
self.nullable = True
self.type = DataType.STRING
self.length = None
self.minvalue = None
self.maxvalue = None
self.listofvalues = None
def parse(self, xml_element, ns):
'''
Parse XML node
:param xml_element: The root XML node of the field (xsd:element)
:type xml_element: Element
:param ns: XSD namespaces
:type ns: dict
'''
# Field name
self.name = xml_element.get('name')
# Field is nullable
self.nullable = xml_element.get('minOccurs') is not None and xml_element.get('minOccurs')=='0'
type = self._getDataType(xml_element, ns)
# Data type
self.type = type[0]
# Text length
self.length = type[1]
# Numeric or date min value
self.minvalue = type[2]
# Numeric or date max value
self.maxvalue = type[3]
# List of possible values, enumeration
self.listofvalues = type[4]
def _getDataType(self, xml_element, ns):
'''
Returns the datatype of the field
:param xml_element: The root XML node of the data type (xsd:element)
:type xml_element: Element
:param ns: XSD namespaces
:type ns: dict
'''
# Dictionary of data types
datatypes = {'xsd:string':DataType.STRING,
'xsd:normalizedString':DataType.STRING,
'xsd:integer':DataType.INTEGER,
'xsd:double':DataType.DOUBLE,
'xsd:decimal':DataType.DOUBLE,
'xsd:date':DataType.DATE}
# XSD restriction element
restriction = xml_element.find('.//xsd:restriction',ns)
# Data type
type = datatypes[restriction.get('base')]
# Length
length_element = restriction.find('xsd:maxLength',ns)
length = length_element.get('value') if length_element is not None else None
# Minimum inclusive value
minvalue_element = restriction.find('xsd:minInclusive',ns)
minvalue = minvalue_element.get('value') if minvalue_element is not None else None
# Maximum inclusive value
maxvalue_element = restriction.find('xsd:maxInclusive',ns)
maxvalue = maxvalue_element.get('value') if maxvalue_element is not None else None
# Enumeration
enumeration_elements = restriction.findall('xsd:enumeration',ns)
enumeration = list()
for enumeration_element in enumeration_elements:
enumeration.append(enumeration_element.get('value'))
return type, length, minvalue, maxvalue, enumeration if len(enumeration)>0 else None
def getEnumerationMap(self):
map = dict()
for element in self.listofvalues:
map[element]=element
return map