-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage_ressource.py
559 lines (415 loc) · 21 KB
/
manage_ressource.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
import sys
import requests
from lxml import etree as ET
from geocat.geocat import GeocatAPI
from geocat import constants, utils
OGC_FORMAT = {
"WMS": {
"int": "local://srv/api/registries/entries/e3f7c60f-b40a-44b5-8221-7c66ff5d3787?lang=fre,ita,eng,ger,roh&schema=iso19139.che",
"prod": "local://srv/api/registries/entries/6c284fc7-fb99-4bb6-8e14-35466d8096d6?lang=fre,ita,eng,ger,roh&schema=iso19139.che"
},
"WFS": {
"int": "local://srv/api/registries/entries/80779add-0cf2-4c66-913b-0cf5ca7f645e?lang=fre,ita,eng,ger,roh&schema=iso19139.che",
"prod": "local://srv/api/registries/entries/80779add-0cf2-4c66-913b-0cf5ca7f645e?lang=fre,ita,eng,ger,roh&schema=iso19139.che"
},
"WMTS": {
"int": "local://srv/api/registries/entries/90b40abc-003d-4a1d-b9ca-1839a8c61a31?lang=fre,ita,eng,ger,roh&schema=iso19139.che",
"prod": "local://srv/api/registries/entries/84c21b8c-d491-47e9-a866-87b4767b206a?lang=fre,ita,eng,ger,roh&schema=iso19139.che"
}
}
class ManageOGCResource:
def __init__(self, api: object = None, env: str = "int"):
if api is None:
self.__api = GeocatAPI(env=env)
else:
self.__api = api
self.env = env
self.service = None
self.endpoint = None
self.layers = None
def __get_ogc_xpath(self, metadata: bytes, layer_id: str = None) -> list:
"""
Get the xpath of the given geoservice layer. If no layers id are given,
get the geoservice xpath. Returns a list of xpath (in case of duplicated resources)
"""
root = ET.fromstring(metadata)
tree = ET.ElementTree(root)
xpath = list()
for tag in tree.findall("./gmd:distributionInfo//gmd:transferOptions//gmd:onLine",
namespaces=constants.NS):
if tag.find(".//gmd:linkage/gmd:URL", namespaces=constants.NS) is None:
continue
url = tag.find(".//gmd:linkage/gmd:URL", namespaces=constants.NS).text
if tag.find(".//gmd:protocol/gco:CharacterString", namespaces=constants.NS) is None:
continue
protocol = tag.find(".//gmd:protocol/gco:CharacterString", namespaces=constants.NS).text
if url is None or protocol is None:
continue
name = tag.find(".//gmd:name/gco:CharacterString", namespaces=constants.NS)
if name is not None:
name = tag.find(".//gmd:name/gco:CharacterString", namespaces=constants.NS).text
if layer_id is None:
if url.split("?")[0] == self.endpoint and \
protocol.split(":")[-1].upper() == self.service and name is None:
xpath.append(tree.getpath(tag))
else:
if url.split("?")[0] == self.endpoint and \
protocol.split(":")[-1].upper() == self.service and name == layer_id.split(":")[-1]:
xpath.append(tree.getpath(tag))
return xpath
def __create_resource_container(self, metadata: bytes) -> list:
"""
Creates all necessary tags and distribution format for the given metadata
to add new OGC online resource.
Returns a list for the batch editing API's body
"""
body = list()
root = ET.fromstring(metadata)
# Add distribution element if doesn't exist
if root.find("./gmd:distributionInfo", namespaces=constants.NS) is None:
body += [
{
"xpath": "/che:CHE_MD_Metadata/",
"value": f"<gn_add>{constants.XML_TAG['distributionInfo']}</gn_add>"
},
# case if metadata is iso19139 and not iso19139che
{
"xpath": "/gmd:MD_Metadata/",
"value": f"<gn_add>{constants.XML_TAG['distributionInfo']}</gn_add>"
}
]
# Add corresponding OGC format if doesn't exist
ogc_format = False
for tag in root.findall("./gmd:distributionInfo[1]//gmd:distributionFormat",
namespaces=constants.NS):
if tag.attrib["{http://www.w3.org/1999/xlink}href"].split("?")[0] == \
OGC_FORMAT[self.service][self.env].split("?")[0]:
ogc_format = True
break
if not ogc_format:
body.append(
{
"xpath": "./gmd:distributionInfo[1]/gmd:MD_Distribution",
"value": "<gn_add><gmd:distributionFormat" \
" xmlns:gmd='http://www.isotc211.org/2005/gmd'" \
" xmlns:xlink='http://www.w3.org/1999/xlink'" \
f" xlink:href='{OGC_FORMAT[self.service][self.env]}'>" \
"</gmd:distributionFormat></gn_add>"
}
)
# Add digital transfer options element if doesn't exist
if root.find("./gmd:distributionInfo[1]//gmd:transferOptions/gmd:MD_DigitalTransferOptions",
namespaces=constants.NS) is None:
body.append(
{
"xpath": "./gmd:distributionInfo[1]/gmd:MD_Distribution",
"value": f"<gn_add>{constants.XML_TAG['transferOptions']}</gn_add>"
}
)
return body
def __create_resource_layer(self, metadata: bytes, layer_id: str) -> dict:
"""
Creates an xpath-value pair for the batch editing API.
The value contains an online resource XML element for an OGC geoservice layer.
The layer name will have all the languages set up in the given metadata.
"""
if layer_id not in self.layers:
print(utils.warningred(f"The Layer ID : {layer_id} hasn't been found in the OGC service"))
return None
languages = self.__api.get_metadata_languages(metadata=metadata)
root = ET.fromstring(constants.XML_TAG['onLine_layer'])
# Add the URL endpoint only for the main language
tag = root.find(".//gmd:linkage/che:PT_FreeURL", namespaces=constants.NS)
ET.SubElement(tag, "{http://www.geocat.ch/2008/che}URLGroup")
ET.SubElement(tag[0], "{http://www.geocat.ch/2008/che}LocalisedURL",
attrib={"locale": f'#{languages[0]}'})
if self.service == "WMS" or self.service == "WFS":
tag[0][0].text = f"{self.endpoint}?SERVICE={self.service}&REQUEST=GetCapabilities"
else:
tag[0][0].text = self.endpoint
# add protocol
root.find(".//gmd:protocol/gco:CharacterString",
namespaces=constants.NS).text = f"OGC:{self.service}"
# add layer id only for the main language
tag = root.find(".//gmd:name/gmd:PT_FreeText", namespaces=constants.NS)
ET.SubElement(tag, "{http://www.isotc211.org/2005/gmd}textGroup")
ET.SubElement(tag[0], "{http://www.isotc211.org/2005/gmd}LocalisedCharacterString",
attrib={"locale": f'#{languages[0]}'})
tag[0][0].text = layer_id.split(":")[-1]
# add layer description
tag = root.find(".//gmd:description/gmd:PT_FreeText", namespaces=constants.NS)
for lang in languages:
ET.SubElement(tag, "{http://www.isotc211.org/2005/gmd}textGroup")
ET.SubElement(tag[-1], "{http://www.isotc211.org/2005/gmd}LocalisedCharacterString",
attrib={"locale": f'#{lang}'})
tag[-1][0].text = self.layers[layer_id][lang.lower()]
return {
"xpath": "./gmd:distributionInfo[1]//gmd:transferOptions[1]/gmd:MD_DigitalTransferOptions",
"value": f"<gn_add>{ET.tostring(root, encoding='utf-8').decode()}</gn_add>".replace('"', "'")
}
def __create_resource_service(self, metadata: bytes) -> dict:
"""
Creates an xpath-value pair for the batch editing API.
The value contains an online resource XML element for an OGC geoservice.
"""
languages = self.__api.get_metadata_languages(metadata=metadata)
root = ET.fromstring(constants.XML_TAG['onLine_service'])
# Add the URL endpoint only for the main language
tag = root.find(".//gmd:linkage/che:PT_FreeURL", namespaces=constants.NS)
ET.SubElement(tag, "{http://www.geocat.ch/2008/che}URLGroup")
ET.SubElement(tag[0], "{http://www.geocat.ch/2008/che}LocalisedURL",
attrib={"locale": f'#{languages[0]}'})
if self.service == "WMS" or self.service == "WFS":
tag[0][0].text = f"{self.endpoint}?SERVICE={self.service}&REQUEST=GetCapabilities"
else:
tag[0][0].text = self.endpoint
# add protocol
root.find(".//gmd:protocol/gco:CharacterString",
namespaces=constants.NS).text = f"OGC:{self.service}"
return {
"xpath": "./gmd:distributionInfo[1]//gmd:transferOptions[1]/gmd:MD_DigitalTransferOptions",
"value": f"<gn_add>{ET.tostring(root, encoding='utf-8').decode()}</gn_add>".replace('"', "'")
}
def __cleanup_format(self, uuid: str):
"""
Clean up the OGC format in the given metadata.
If the OGC format according to to given service is not used by any resource but it is set up
as distribution format, the latter is deleted.
"""
metadata = self.__api.get_metadata_from_mef(uuid=uuid)
if metadata is None:
raise Exception("No metadata found")
root = ET.fromstring(metadata)
tree = ET.ElementTree(root)
tags_to_delete = list()
body = list()
for tag in tree.findall("./gmd:distributionInfo[1]//gmd:transferOptions//gmd:onLine//gmd:protocol/gco:CharacterString",
namespaces=constants.NS):
if tag.text.split(":")[-1] == self.service:
return
for tag in tree.findall("./gmd:distributionInfo[1]//gmd:distributionFormat",
namespaces=constants.NS):
if tag.attrib["{http://www.w3.org/1999/xlink}href"].split("?")[0] == \
OGC_FORMAT[self.service][self.env].split("?")[0]:
tags_to_delete.append(tree.getpath(tag))
if len(tags_to_delete) > 0:
for tag in reversed(tags_to_delete):
body.append(
{
"xpath": tag,
"value": "<gn_delete></gn_delete>"
}
)
response = self.__api.edit_metadata(uuid=uuid, body=body, updateDateStamp=True)
if not utils.process_ok(response):
raise Exception("Cleanup format unsuccessfull")
def __cleanup_distribution(self, uuid: str):
"""If distribution is empty, delete the entire tag"""
metadata = self.__api.get_metadata_from_mef(uuid=uuid)
if metadata is None:
raise Exception("No metadata found")
root = ET.fromstring(metadata)
if len(root.find("./gmd:distributionInfo[1]/gmd:MD_Distribution",
namespaces=constants.NS).getchildren()) == 0:
body = [
{
"xpath": "./gmd:distributionInfo[1]",
"value": "<gn_delete></gn_delete>"
}
]
response = self.__api.edit_metadata(uuid=uuid, body=body, updateDateStamp=True)
if not utils.process_ok(response):
raise Exception("Cleanup distribution unsuccessfull")
def get_ogc_layers(self, service: str, endpoint: str):
"""Connects to OGC web service and fetch all available layers"""
self.service = service.upper()
self.endpoint = endpoint.split('?')[0]
self.layers = dict()
lang = ("de", "fr", "it", "en", "rm")
if self.service == "WMS" or self.service == "WFS":
url = f"{self.endpoint}?SERVICE={self.service}&REQUEST=GetCapabilities&"
elif self.service == "WMTS":
url = f"{self.endpoint}?"
else:
print(utils.warningred("The parameter service is not valid (wms, wfs, wmts)"))
sys.exit()
if self.service == "WMS":
layer_xpath = ".//*[local-name()='Layer']"
id_xpath = "./*[local-name()='Name']"
title_xpath = "./*[local-name()='Title']"
elif self.service == "WFS":
layer_xpath = ".//*[local-name()='FeatureType']"
id_xpath = "./*[local-name()='Name']"
title_xpath = "./*[local-name()='Title']"
elif self.service == "WMTS":
layer_xpath = ".//*[local-name()='Layer']"
id_xpath = "./*[local-name()='Identifier']"
title_xpath = "./*[local-name()='Title']"
for l in lang:
response = requests.get(f"{url}lang={l}", proxies=self.__api.session.proxies)
if response.status_code != 200:
continue
root = ET.fromstring(response.content)
for layer in root.xpath(layer_xpath):
id = layer.xpath(id_xpath)[0].text
if id not in self.layers:
self.layers[id] = dict()
name = layer.xpath(title_xpath)[0].text
self.layers[id][l] = name
def add_ogc_layers(self, uuid: str, layers_id: list):
"""
Add one or more OGC geoservice layers to the given metadata. Layers are given with their
ID in form of a list.
"""
if self.service is None:
raise Exception("No service found ! The method get_ogc_service must be run")
metadata = self.__api.get_metadata_from_mef(uuid=uuid)
if metadata is None:
raise Exception("No metadata found")
body = self.__create_resource_container(metadata=metadata)
# Add OGC webservice layers
has_layer = False
layer_already_exist = True
for layer in layers_id:
if len(self.__get_ogc_xpath(metadata=metadata, layer_id=layer)) == 0:
layer_already_exist = False
if self.__create_resource_layer(metadata=metadata, layer_id=layer) is not None:
body.append(self.__create_resource_layer(metadata=metadata, layer_id=layer))
has_layer = True
if layer_already_exist:
raise Exception("Layers ID already exist")
if not has_layer:
raise Exception("No layers ID found in the OGC geoservice")
response = self.__api.edit_metadata(uuid=uuid, body=body, updateDateStamp=True)
if not utils.process_ok(response):
raise Exception("OGC Layers unsucessfully added")
def add_ogc_service(self, uuid: str, service: str, endpoint: str):
"""Add an OGC geoservice to the given metadata."""
self.service = service.upper()
self.endpoint = endpoint.split('?')[0]
metadata = self.__api.get_metadata_from_mef(uuid=uuid)
if metadata is None:
raise Exception("No metadata found")
body = self.__create_resource_container(metadata=metadata)
if len(self.__get_ogc_xpath(metadata=metadata)) == 0:
body.append(self.__create_resource_service(metadata=metadata))
else:
raise Exception("OGC service already exists")
response = self.__api.edit_metadata(uuid=uuid, body=body, updateDateStamp=True)
if not utils.process_ok(response):
raise Exception("OGC service unsucessfully added")
def delete_ogc_layers(self, uuid: str, layers_id: list):
"""
Delete one or more OGC geoservice layers from the given metadata. Layers are given
with their ID in form of a list.
"""
if self.service is None:
raise Exception("No service found ! The method get_ogc_service must be run")
metadata = self.__api.get_metadata_from_mef(uuid=uuid)
if metadata is None:
raise Exception("No metadata found")
root = ET.fromstring(metadata)
online_tags = list()
body = list()
no_layer = True
for layer in layers_id:
for xpath in self.__get_ogc_xpath(metadata=metadata, layer_id=layer):
online_tags.append(xpath)
no_layer = False
if no_layer:
raise Exception("No layers found in the metadata")
for tag in reversed(online_tags):
body.append(
{
"xpath": tag,
"value": "<gn_delete></gn_delete>"
}
)
transfer_tag = "/".join(online_tags[0].split("/")[:-1])
if len(root.xpath(transfer_tag,
namespaces=constants.NS)[0].getchildren()) == len(online_tags):
body.append(
{
"xpath": "/".join(transfer_tag.split("/")[:-1]),
"value": "<gn_delete></gn_delete>"
}
)
response = self.__api.edit_metadata(uuid=uuid, body=body, updateDateStamp=True)
if not utils.process_ok(response):
raise Exception("OGC Layers unsucessfully deleted")
self.__cleanup_format(uuid=uuid)
self.__cleanup_distribution(uuid=uuid)
def delete_ogc_service(self, uuid: str, service: str, endpoint: str):
"""Delete OGC geoservice from the given metadata."""
self.service = service.upper()
self.endpoint = endpoint.split('?')[0]
metadata = self.__api.get_metadata_from_mef(uuid=uuid)
if metadata is None:
raise Exception("No metadata found")
root = ET.fromstring(metadata)
body = list()
online_tags = self.__get_ogc_xpath(metadata=metadata)
if len(online_tags) == 0:
raise Exception("No service found in the metadata")
for tag in reversed(online_tags):
body.append(
{
"xpath": tag,
"value": "<gn_delete></gn_delete>"
}
)
transfer_tag = "/".join(online_tags[0].split("/")[:-1])
if len(root.xpath(transfer_tag,
namespaces=constants.NS)[0].getchildren()) == len(online_tags):
body.append(
{
"xpath": "/".join(transfer_tag.split("/")[:-1]),
"value": "<gn_delete></gn_delete>"
}
)
response = self.__api.edit_metadata(uuid=uuid, body=body, updateDateStamp=True)
if not utils.process_ok(response):
raise Exception("OGC service unsucessfully deleted")
self.__cleanup_format(uuid=uuid)
self.__cleanup_distribution(uuid=uuid)
def delete_all_ogc(self, uuid: str):
"""Delete all OGC online resources from the given metadata."""
metadata = self.__api.get_metadata_from_mef(uuid=uuid)
if metadata is None:
raise Exception("No metadata found")
root = ET.fromstring(metadata)
tree = ET.ElementTree(root)
xpath = list()
for tag in tree.findall("./gmd:distributionInfo//gmd:transferOptions//gmd:onLine",
namespaces=constants.NS):
if tag.find(".//gmd:protocol/gco:CharacterString", namespaces=constants.NS) is None:
continue
protocol = tag.find(".//gmd:protocol/gco:CharacterString", namespaces=constants.NS).text
if protocol.split(":")[0] == "OGC":
xpath.append(tree.getpath(tag))
if len(xpath) == 0:
raise Exception("No OGC resource found in the metadata")
body = list()
for tag in reversed(xpath):
body.append(
{
"xpath": tag,
"value": "<gn_delete></gn_delete>"
}
)
transfer_tag = "/".join(xpath[0].split("/")[:-1])
if len(root.xpath(transfer_tag,
namespaces=constants.NS)[0].getchildren()) == len(xpath):
body.append(
{
"xpath": "/".join(transfer_tag.split("/")[:-1]),
"value": "<gn_delete></gn_delete>"
}
)
response = self.__api.edit_metadata(uuid=uuid, body=body, updateDateStamp=True)
if not utils.process_ok(response):
raise Exception("OGC resources unsucessfully deleted")
for service in OGC_FORMAT:
self.service = service
self.__cleanup_format(uuid=uuid)
self.__cleanup_distribution(uuid=uuid)