-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmetadataParser.py
553 lines (442 loc) · 22.7 KB
/
metadataParser.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
import sys, json
from io import StringIO
from urllib.parse import urlencode, unquote, urlparse, parse_qsl
import xml.etree.ElementTree as ET
from .webUtil import getUrlData, metaError
CSW_URL = "https://www.nationaalgeoregister.nl/geonetwork/srv/dut/inspire"
class MDdata(object):
def __init__(self, metadataXML):
"""Parse a CSW-metadataXML
:param metadataXML: a CSW-metadata XMLdocument
"""
self.records = []
self.count = int( metadataXML.attrib["numberOfRecordsMatched"] )
mds = metadataXML.findall("{http://www.opengis.net/cat/csw/2.0.2}Record")
for md in mds:
record = {}
identifier = md.find("{http://purl.org/dc/elements/1.1/}identifier")
title = md.find('{http://purl.org/dc/elements/1.1/}title')
if identifier == None or not identifier.text: continue
record['uuid'] = identifier.text
record['title'] = title.text
description = md.find('{http://purl.org/dc/elements/1.1/}description')
if (description != None):
record['abstract'] = description.text if description.text != None else ''
bbox = md.find('{http://www.opengis.net/ows}BoundingBox')
if (bbox != None):
LowerCorner = md.find('{http://www.opengis.net/ows}LowerCorner')
UpperCorner = md.find('{http://www.opengis.net/ows}UpperCorner')
if LowerCorner and UpperCorner:
record['geoBox'] = [float(n) for n in LowerCorner.text.split(" ")] + [float(n) for n in UpperCorner.text.split(" ")]
else:
record['geoBox'] = [3.1, 50.6, 7.4, 53.6]
else:
record['geoBox'] = [3.1, 50.6, 7.4, 53.6]
record['wms'] = self._findWXS( md, "OGC:WMS" )
record['wfs'] = self._findWXS( md, "OGC:WFS" )
record['wcs'] = self._findWXS( md, "OGC:WCS" )
record['wmts'] = self._findWXS( md, "OGC:WMTS")
record['download'] = self._findDownloads( md )
print( record['download'] )
self.records.append(record)
def _findWXS(self, node, protocol= None ):
links = [n for n in node.findall("{http://purl.org/dc/elements/1.1/}URI")
if "protocol" in n.attrib and n.attrib["protocol"] == protocol]
links += [ n for n in node.findall("{http://purl.org/dc/elements/1.1/}URI")
if n.text
and 'SERVICE' in dict( parse_qsl( urlparse( n.text.upper() ).query ))
and dict(parse_qsl(urlparse( n.text.upper() ).query))['SERVICE'] in protocol ]
if len(links) > 0:
name = links[0].attrib["name"] if "name" in links[0].attrib else links[0].text
return (name, links[0].text)
return ("","")
def _findDownloads(self, node):
links = [n for n in node.findall("{http://purl.org/dc/elements/1.1/}URI")
if "protocol" in n.attrib and "DOWNLOAD" in n.attrib["protocol"].upper() ]
atoms = [n.text for n in node.findall("{http://purl.org/dc/elements/1.1/}URI")
if "protocol" in n.attrib and "ATOM" in n.attrib["protocol"].upper() ]
if len(links) == 0 and len(atoms) == 0:
return []
if len(links) > 0 and len(atoms) == 0:
results = [ [ n.attrib["name"] if "name" in n.attrib else n.text , n.text ] for n in links ]
return results
return self._getAtom(atoms[0])
def _getAtom(self, atom):
try:
resp= getUrlData(atom)
results = []
root = ET.fromstring(resp)
except ET.ParseError:
print( "WARNING: Geen correcte xml: {} ".format(atom) )
return []
except metaError as me:
print( "WARNING: http-fout {} -> geeft {}".format(atom, me.message) )
return []
entries = root.findall( ".//{http://www.w3.org/2005/Atom}entry" )
for entry in entries:
dls = [ link for link in entry.findall( "{http://www.w3.org/2005/Atom}link")
if not ( "rel" in link.attrib and link.attrib["rel"] == "describedby") ]
if len(dls) == 0: continue
titleNode = entry.find( "{http://www.w3.org/2005/Atom}title")
dl = dls[0]
if "type" in dl.attrib and "application/atom" in dl.attrib["type"]:
results += self._getAtom( dl.attrib["href"] )
else:
results.append( [ titleNode.text, dl.attrib["href"] ] )
return results
class MDReader(object):
"""Interact with CSW-service, to get info
:param proxyUrl: the proxy to use for internet calls
:param timeout: the timeout for internet calls
"""
def __init__(self):
self.geoNetworkUrl = CSW_URL
self.dataTypes = [["Dataset", "dataset"],["Service","service"]]
self.inspireServiceTypes = ["discovery","download","view","other"]
def _createFindUrl(self, q="", start=1, maxRecords=100, orgName='', dataType='', inspiretheme='', inspireServiceType=''):
url = self.geoNetworkUrl
data = {}
#escape '-sign's in CQL
q = q.replace("'", "''")
orgName = orgName.replace("'", "''")
inspiretheme = inspiretheme.replace("'", "''")
#make CQL query
CQLparts = []
if inspiretheme:
CQLparts.append("(AnyText LIKE '%"+ inspiretheme +"%' AND AnyText = 'GEMET - INSPIRE themes, version 1.0')")
if len(q.strip()) > 0:
CQLparts.append(" AnyText LIKE '%" + q + "%' ")
if orgName:
CQLparts.append(" OrganisationName = '" + orgName + "' ")
if dataType:
CQLparts.append(" type='" + dataType + "' ")
if inspireServiceType:
CQLparts.append(" ServiceType='" + inspireServiceType + "' ")
CQL = "(" + " AND ".join(CQLparts) + ")"
data["request"] = "GetRecords"
data["service"] = "CSW"
data["version"] = "2.0.2"
data["elementsetname"] = "full"
data["typenames"] = "gmd:MD_Metadata"
data["RESULTTYPE"] = "results"
data["constraintLanguage"] = "CQL_TEXT"
data["constraint_language_version"] = "1.1.0"
data["maxRecords"] = maxRecords
data["startPosition"] = start
data["constraint"] = CQL
values = urlencode(data)
resultUrl = url +"?"+ values
return resultUrl
def list_inspire_theme(self):
"""List the inspire-themes
:return: a list containing the inspire themes
"""
return [ "Administratieve eenheden", "Adressen", "Atmosferische omstandigheden", "Beschermde gebieden",
"Biogeografische gebieden", "Bodem", "Bodemgebruik", "Energiebronnen",
"Faciliteiten voor landbouw en aquacultuur",
"Faciliteiten voor productie en industrie", "Gebieden met natuurrisico's",
"Gebiedsbeheer, gebieden waar beperkingen gelden, gereguleerde gebieden en rapportage-eenheden",
"Gebouwen", "Geografisch rastersysteem", "Geografische namen", "Geologie",
"Habitats en biotopen", "Hoogte", "Hydrografie", "Kadastrale percelen", "Landgebruik",
"Menselijke gezondheid en veiligheid",
"Meteorologische geografische kenmerken", "Milieubewakingsvoorzieningen", "Minerale bronnen",
"Nutsdiensten en overheidsdiensten", "Oceanografische geografische kenmerken", "Orthobeeldvorming",
"Spreiding van de bevolking — demografie", "Spreiding van soorten", "Statistische eenheden",
"Systemen voor verwijzing door middel van coördinaten", "Vervoersnetwerken", "Zeegebieden" ]
def list_suggestionKeyword(self):
"""List the Keywords
:return: a list with the Keywords
"""
url = self.geoNetworkUrl + "?request=GetDomain&service=CSW&version=2.0.2&PropertyName=Subject"
resp = getUrlData(url)
result = ET.fromstring(resp)
return [ n.text for n in result.findall('.//{http://www.opengis.net/cat/csw/2.0.2}Value') ]
def list_organisations(self):
"""List the organisations
:return: a list with the organisations
"""
url = self.geoNetworkUrl + '?request=GetDomain&service=CSW&version=2.0.2&PropertyName=OrganisationName'
resp = getUrlData(url)
result = ET.fromstring(resp)
organisations = [ n.text for n in result.findall('.//{http://www.opengis.net/cat/csw/2.0.2}Value') ]
organisations.sort()
return organisations
def _search(self, q="", start=1, step=100, orgName='', dataType='', inspiretheme='', inspireServiceType=''):
"""Search the csw with the following parameters:
:param q: free text to seach for
:param start: initial postion of the searchresult
:param step: size of the searchresult
:param orgName: filter on the name of a organisation
:param dataType: filter on the type of record: service or dataset
:param inspiretheme: filter on inspiretheme
:param inspireServiceType: filter on inspire serviceType
:return: a XMLdocument with the results
"""
url = self._createFindUrl( q, start, step, orgName, dataType, inspiretheme, inspireServiceType)
resp = getUrlData(url)
result = ET.fromstring(resp)
return result
def searchAll(self, q="", orgName='', dataType='', inspiretheme='', inspireServiceType=''):
"""Search the csw, making multiple calls in case of large resultsets, with the following parameters:
:param q: free text to seach for
:param orgName: filter on the name of a organisation
:param dataType: filter on the type of record: service or dataset
:param inspiretheme: filter on inspiretheme
:param inspireServiceType: filter on inspire serviceType
:return: a composite XMLdocument with the results
"""
start= 1
step= 100
result = self._search(q, start, step, orgName, dataType, inspiretheme, inspireServiceType)
searchResult = result.find("{http://www.opengis.net/cat/csw/2.0.2}SearchResults")
count = int( searchResult.attrib["numberOfRecordsMatched"] )
start += step
while (start) <= count:
result = self._search(q, start, step, orgName, dataType,inspiretheme, inspireServiceType)
mds= result.findall(".//{http://www.opengis.net/cat/csw/2.0.2}Record")
for md in mds: searchResult.append( md )
start += step
mdata = MDdata( searchResult )
return mdata
def getWmsLayerNames(url):
"""List all the thr layers in a WMS
:param url: the getcapabilities url of the WMS
:param proxyUrl: the url of the network proxy
:param timeout: the timeout for internet calls
:return: a list of tuples in de form: [(name, title, style ), ...]
"""
capability = url.split("?")[0] + "?request=GetCapabilities&version=1.3.0&service=wms"
resp = getUrlData(capability)
result = ET.fromstring(resp)
layers = result.findall( ".//{http://www.opengis.net/wms}Layer" )
layerNames=[]
for lyr in layers:
name= lyr.find("{http://www.opengis.net/wms}Name")
title = lyr.find("{http://www.opengis.net/wms}Title")
style = lyr.find("{http://www.opengis.net/wms}Style/{http://www.opengis.net/wms}Name")
if ( name != None) and ( title != None ):
if style == None: layerNames.append(( name.text, title.text, ''))
else: layerNames.append(( name.text, title.text, style.text))
return layerNames
def getWFSLayerNames( url ):
"""List all the layers in a WFS
:param url: the getcapabilities url of the WFS
:param proxyUrl: the url of the network proxy
:param timeout: the timeout for internet calls
:return: a list of tuples in de form: [(name, title, srs ), ...]
"""
capability = url.split("?")[0] + "?request=GetCapabilities&version=2.0.0&service=wfs"
resp = getUrlData(capability)
result = ET.fromstring(resp)
layerNames=[]
#default
version = "2.0.0"
serv = result.find(".//{http://www.opengis.net/ows/1.1}ServiceTypeVersion")
if serv is None:
serv = result.find(".//{http://www.opengis.net/ows}ServiceTypeVersion")
if serv is not None:
version = serv.text
if version == "2.0.0":
layers = result.findall( ".//{http://www.opengis.net/wfs/2.0}FeatureType" )
for lyr in layers:
name= lyr.find("{http://www.opengis.net/wfs/2.0}Name")
title = lyr.find("{http://www.opengis.net/wfs/2.0}Title")
srs = lyr.find("{http://www.opengis.net/wfs/2.0}DefaultCRS")
isComplex = testComplex(url, name.text, version)
if ( name != None) and ( title != None ):
if srs == None: layerNames.append(( name.text, title.text, 'EPSG:28992'))
else: layerNames.append(( name.text, title.text, srs.text, isComplex))
elif version == "1.1.0":
layers = result.findall( ".//{http://www.opengis.net/wfs/2.0}FeatureType" )
for lyr in layers:
name= lyr.find("{http://www.opengis.net/wfs}Name")
title = lyr.find("{http://www.opengis.net/wfs}Title")
srs = lyr.find("{http://www.opengis.net/wfs}DefaultCRS")
isComplex = testComplex(url, name.text, version)
if ( name != None) and ( title != None ):
if srs == None: layerNames.append(( name.text, title.text, 'EPSG:28992'))
else: layerNames.append(( name.text, title.text, srs.text, isComplex))
else:
layers = result.findall( ".//{http://www.opengis.net/wfs}FeatureType" )
for lyr in layers:
name= lyr.find("{http://www.opengis.net/wfs}Name")
title = lyr.find("{http://www.opengis.net/wfs}Title")
srs = lyr.find("{http://www.opengis.net/wfs}SRS")
if ( name != None) and ( title != None ):
if srs == None: layerNames.append(( name.text, title.text, 'EPSG:28992'))
else: layerNames.append(( name.text, title.text, srs.text, False))
return { 'version': version, 'layerNames': layerNames }
def getWMTSlayersNames( url ):
"""List all the layers in a WMTS
:param url: the getcapabilities url of the WCS
:param proxyUrl: the url of the network proxy
:param timeout: the timeout for internet calls
:return: a list of tuples in de form: [(name, title, matrix, format, srs ), ...]
"""
if (not "request=getcapabilities" in url.lower()) or (not "service=wmts" in url.lower()):
capability = url.split("?")[0] + "?service=WMTS&request=Getcapabilities&version="
else:
capability = url
resp = getUrlData(capability)
result = ET.fromstring(resp)
content = result.find( "{http://www.opengis.net/wmts/1.0}Contents" )
layers = content.findall( "{http://www.opengis.net/wmts/1.0}Layer" )
layerNames = []
matrixSets = content.findall("{http://www.opengis.net/wmts/1.0}TileMatrixSet")
for lyr in layers:
name= lyr.find("{http://www.opengis.net/ows/1.1}Identifier")
title = lyr.find("{http://www.opengis.net/ows/1.1}Title")
matrix = lyr.find("{http://www.opengis.net/wmts/1.0}TileMatrixSetLink/{http://www.opengis.net/wmts/1.0}TileMatrixSet")
format = lyr.find("{http://www.opengis.net/wmts/1.0}Format")
srsList = [ n.find("{http://www.opengis.net/ows/1.1}SupportedCRS").text
for n in matrixSets if n.find("{http://www.opengis.net/ows/1.1}Identifier").text == matrix.text]
if srsList: srs = "EPSG:"+ srsList[0].split(':')[-1]
else: srs = ""
if ( name != None) and ( title != None ) and ( matrix != None ) and ( format != None ):
layerNames.append(( name.text, title.text, matrix.text, format.text, srs ))
return layerNames
def getWCSlayerNames( url, wcs_version="1.1"):
"""List all the layers in a WFS
:param url: the getcapabilities url of the WCS
:param proxyUrl: the url of the network proxy
:param wcs_version: the version of WCS to use, supported versions: 2.0, 1.1, 1.0
:param timeout: the timeout for internet calls
:return: a list of tuples in de form: [(Identifier, Title ), ...]
"""
capability = url.split("?")[0] + "?request=GetCapabilities&version=%s.0&service=WCS" % wcs_version
resp = getUrlData(capability)
result = ET.fromstring(resp)
#find namespaces to identify WCS-version returned
namespaces = dict([node for _, node in ET.iterparse( StringIO(result), events=['start-ns'])])
wcs_version = namespaces[''][-3:]
wcsNS = "http://www.opengis.net/wcs/" + wcs_version
owsNS = "http://www.opengis.net/ows/" + wcs_version
content = result.find( "{%s}Contents" % wcsNS)
layers = content.findall( "{%s}CoverageSummary" % wcsNS)
layerNames = []
if wcs_version =="1.1":
for lyr in layers: layerNames.append([lyr.find("{%s}Identifier" % wcsNS).text, lyr.find("{%s}Title" % owsNS).text ])
else:
for lyr in layers: layerNames.append([lyr.find("{%s}CoverageId" % wcsNS).text, lyr.find("{%s}CoverageId" % wcsNS).text ])
return layerNames
def makeWFSuri( url, name='', srsname="EPSG:28992", version='1.0.0' ):
"""Make a QGIS-uri to load WFS-services.
:param url: the base url of the wfs
:param name: The name of the layer
:param srsname: the crs as a string,this form <auth>:<id>, like EPSG:28992
:param version: WFS version: 1.0.0, 1.1.0 or 2.0.0
:return: the QGIS-uri
"""
params = { 'SERVICE': 'WFS',
'VERSION': version ,
'REQUEST': 'GetFeature',
'TYPENAME': name,
'SRSNAME': srsname }
uri = url.split("?")[0] + '?' + unquote( urlencode(params) )
return uri
def makeWMTSuri( url, layer, tileMatrixSet, srsname="EPSG:3857", styles='', format='image/png' ):
"""Make a QGIS-uri to load WMTS-services.
:param url: the base url of the WMTS
:param name: The name of the layer
:param tileMatrixSet: The namae of the tileMatrixSet to use
:param srsname: the crs as a string,this form <auth>:<id>, like EPSG:28992
:param styles: the name(s) of the styles to use
:param format: the name of the mimetype of the output: image/png, image/jpeg, ...
:return: the QGIS-uri
"""
params = { 'tileMatrixSet': tileMatrixSet,
'styles': styles,
'format': format ,
'layers': layer,
'crs': srsname,
'contextualWMSLegend': 0,
'url': url.split('?')[0] }
uri = unquote( urlencode(params) )
return uri
def makeWCSuri( url, layer ):
"""Make a QGIS-uri to load WCS-services.
:param url: the base url of the WMTS
:param layer: The name of the layer
:return: the QGIS-uri
"""
params = { 'dpiMode': 7 ,
'identifier': layer,
'url': url.split('?')[0] }
uri = unquote( urlencode(params) )
return uri
def testComplex(url, typeName, version="1.1.0" ):
"""Test if a WFS has complex features for a specific layer (typeName)
:param url: the url of the WFS
:param typeName: the typeName of the layer to test for complex features
:param version: WFS version: 1.1.0 or 2.0.0
:param proxyUrl: the url of the network proxy
:param timeout: the timeout for internet calls
:return: a boolean, true if the WFS has complex features
"""
if version not in ["1.1.0", "2.0.0"]: return False
if version == "1.1.0": countStr = "maxFeatures"
if version == "2.0.0": countStr = "count"
baseUrl = url.split("?")[0]
qryString = "?{}=1&SERVICE=WFS&VERSION={}&REQUEST=GetFeature&TYPENAME={}&outputFormat=json".format(countStr, version, typeName)
fullUrl = baseUrl + qryString
try:
response = getUrlData(fullUrl)
geojson = json.loads(response)
features = geojson['features']
if len(features) > 0:
properties = features[0]["properties"]
for n in properties:
val = properties[n]
if type(val) == dict: return True
return False
except:
print("Error in finding out WFS-complex:" + str( sys.exc_info() ) )
return False
def downloadWFS(url, typeName, outputLocation, crs="EPSG:4326", maxCount=10000, version="1.1.0", bbox=[]):
"""Download a WFS as GML-file for a specific area
:param url: the url of the WFS
:param typeName: the typeName of the layer to download
:param outputLocation: the path and filename of the output gml
:param maxCount: max number of features
:param version: WFS version: 1.1.0 or 2.0.0
:param bbox: limit the output to a certain area
:param proxyUrl: the url of the network proxy
:param timeout: the timeout for internet calls
:return: outputLocation
"""
if version not in ["1.1.0", "2.0.0"]:
version = "1.1.0"
if version == "1.1.0":
typeNameStr = "TYPENAME"
countStr = "maxFeatures"
if version == "2.0.0":
typeNameStr = "TYPENAMES"
countStr = "count"
if bbox: bboxS = ",".join([str(n) for n in bbox])
# Thijs Brentjens, suggestiont for workaround different axis order for 4258, 4326
# TODO: which CRSes should take into account different axis order for WFS download?
if 'EPSG' in crs and ('4258' in crs or '4326' in crs) and bbox:
bboxS = str(bbox[1])+','+str(bbox[0])+','+str(bbox[3])+','+str(bbox[2])
elif bbox: bboxS = ",".join([str(n) for n in bbox])
baseUrl = url.split("?")[0]
qryString = "?{}={}&SERVICE=WFS&VERSION={}&REQUEST=GetFeature&{}={}&srsName={}&bbox={}".format(
countStr, maxCount, version, typeNameStr, typeName, crs, bboxS)
fullUrl = baseUrl + qryString
response = getUrlData(fullUrl, returnBytes=True)
with open(outputLocation, 'wb') as out_file:
out_file.write(response)
return outputLocation
def xmlIsEmpty(xml_file, gmlException=True):
"""test if a xml file contais data
:param xml_file: a path to a xml file.
:param gmlException: also return if gmlException
return: True if empty else False
"""
try:
tree = ET.parse(xml_file)
root = tree.getroot()
if gmlException and "ExceptionReport" in root.tag:
return True
return not len(root)
except:
return True