|
22 | 22 | """ |
23 | 23 | import json |
24 | 24 | import os.path |
| 25 | +import urllib |
25 | 26 |
|
26 | 27 | from qgis.core import ( |
27 | 28 | Qgis, |
|
33 | 34 | QgsProject, |
34 | 35 | QgsRasterLayer, |
35 | 36 | QgsSettings, |
| 37 | + QgsVectorLayer |
36 | 38 | ) |
37 | 39 |
|
38 | 40 | # some things for doing http requests |
39 | | -from qgis.PyQt.QtCore import QCoreApplication, QEventLoop, QSettings, Qt, QTranslator, QUrl |
| 41 | +from qgis.PyQt.QtCore import QCoreApplication, QSettings, Qt, QTranslator, QUrl |
40 | 42 | from qgis.PyQt.QtGui import QDesktopServices, QIcon, QPixmap |
41 | | -from qgis.PyQt.QtNetwork import QNetworkRequest, QSslSocket |
42 | | -from qgis.PyQt.QtWidgets import QAction, QMessageBox, QTreeWidgetItem |
| 43 | +from qgis.PyQt.QtNetwork import QNetworkRequest |
| 44 | +from qgis.PyQt.QtWidgets import QAction |
43 | 45 |
|
44 | 46 | from .models.Application import Application |
45 | 47 | from .qgis_shogun_editor_dialog import QgisShogunEditorDialog |
@@ -350,80 +352,97 @@ def createWmsLayerFromShogun(self, layer_src_conf): |
350 | 352 | else: |
351 | 353 | return False |
352 | 354 |
|
353 | | - def createLayer(self, layer_src_conf): |
354 | | - # layerurl = request_url |
355 | | - |
356 | | - # dataType = layerItem.datatype |
357 | | - |
358 | | - # every layerItem.source should have an attribute 'dataType' |
359 | | - # if dataType == 'vector' or dataType == 'Vector': |
360 | | - # url = layerItem.ressource.baseurl.rstrip('/shogun2-webapp/') + layerurl + '?' |
361 | | - # return createWfsLayer(layerItem, url, epsg) |
362 | | - |
363 | | - # elif dataType == 'Raster': |
364 | | - # url = layerItem.ressource.baseurl.rstrip('/shogun2-webapp/') + layerurl + '?' |
365 | | - # return createRasterLayer(layerItem, url, epsg) |
366 | | - |
367 | | - # elif dataType == 'WMS': |
368 | | - # if layerurl == '/shogun2-webapp/geoserver.action': |
369 | | - # url = layerItem.ressource.baseurl.rstrip('/shogun2-webapp/') + layerurl + '?' |
370 | | - return self.createWmsLayerFromShogun(layer_src_conf) |
371 | | - # else: |
372 | | - # return createWmsLayer(layerItem, layerurl, epsg) |
373 | | - |
374 | | - # if for any reason the parameter 'dataType' is not set correctly, we check the url |
375 | | - # of the layer to determine if it's a WFS/WCS from the shogun-geoserver |
376 | | - # (url has'shogun2-webapp') or if it's a WMS from an outer source (other url) |
377 | | - # elif dataType == 'unknown' or dataType == None or dataType == '': |
378 | | - # if layerurl.startswith('/shogun2-webapp'): |
379 | | - # url = layerItem.ressource.baseurl.rstrip('/shogun2-webapp/') + layerurl + '?' |
380 | | - # try: |
381 | | - # lyr = createWfsLayer(layerItem, url, epsg) |
382 | | - # if lyr.isValid(): |
383 | | - # return lyr |
384 | | - # except: |
385 | | - # pass |
386 | | - # try: |
387 | | - # lyr = createWmsLayerFromShogun(layerItem, url, epsg) |
388 | | - # if lyr.isValid(): |
389 | | - # return lyr |
390 | | - # except: |
391 | | - # pass |
392 | | - # try: |
393 | | - # lyr = createRasterLayer(layerItem, url, epsg) |
394 | | - # if lyr.isValid(): |
395 | | - # return lyr |
396 | | - # except: |
397 | | - # pass |
398 | | - # else: |
399 | | - # return createWmsLayerNormal(layerItem, layerurl, epsg) |
400 | | - |
401 | | - # else: |
402 | | - # info = 'Layer source '+ layerurl + ' could not be loaded' |
403 | | - # QMessageBox.warning(None, 'Warning', info, QMessageBox.Ok) |
404 | | - |
405 | | - def addQgsLayer(self, layer_src_conf, layer_group): |
| 355 | + def createTileWmsLayerFromShogun(self, layer_src_conf): |
| 356 | + layerNames = layer_src_conf['layerNames'] |
| 357 | + layer_url = layer_src_conf['url'] |
| 358 | + if str(layer_url).startswith('/'): |
| 359 | + layer_url = self.dlg.entryUrl.text() + layer_url |
| 360 | + |
| 361 | + params = { |
| 362 | + 'layers': layerNames, |
| 363 | + 'styles': '', |
| 364 | + 'format': 'image/png', |
| 365 | + 'crs': 'EPSG:' + str(QgsProject.instance().crs().srsid()), |
| 366 | + 'url': layer_url, |
| 367 | + 'tiled': 'true' |
| 368 | + } |
| 369 | + |
| 370 | + # set transparency if present |
| 371 | + try: |
| 372 | + params['transparent'] = layer_src_conf['requestParams']['TRANSPARENT'] |
| 373 | + print('set transparent') |
| 374 | + except KeyError: |
| 375 | + print('no transparency') |
| 376 | + |
| 377 | + uri = '&'.join([f"{k}={v}" for k, v in params.items()]) |
| 378 | + layer = QgsRasterLayer(uri, layerNames, 'wms') |
| 379 | + |
| 380 | + if layer.isValid(): |
| 381 | + return layer |
| 382 | + else: |
| 383 | + return False |
| 384 | + |
| 385 | + def createWfsLayerFromShogun(self, layer_src_conf): |
| 386 | + layerNames = layer_src_conf['layerNames'] |
| 387 | + layer_url = layer_src_conf['url'] |
| 388 | + if str(layer_url).startswith('/'): |
| 389 | + layer_url = self.dlg.entryUrl.text() + layer_url |
| 390 | + |
| 391 | + params = { |
| 392 | + 'service': 'WFS', |
| 393 | + 'version': '2.0.0', |
| 394 | + 'request': 'GetFeature', |
| 395 | + 'typename': layerNames, |
| 396 | + 'srsname': 'EPSG:' + str(QgsProject.instance().crs().srsid()) |
| 397 | + } |
| 398 | + |
| 399 | + uri = layer_url + urllib.parse.unquote(urllib.parse.urlencode(params)) |
| 400 | + layer = QgsVectorLayer(uri, layerNames, 'WFS') |
| 401 | + |
| 402 | + if layer.isValid(): |
| 403 | + return layer |
| 404 | + else: |
| 405 | + return False |
| 406 | + |
| 407 | + def createLayer(self, layer_in_tree): |
| 408 | + layer_src_conf = layer_in_tree.source_config |
| 409 | + data_type = layer_in_tree.layerType |
| 410 | + print('createLayer', ) |
| 411 | + |
| 412 | + if data_type == 'WMS': |
| 413 | + print('create WMS layer') |
| 414 | + return self.createWmsLayerFromShogun(layer_src_conf) |
| 415 | + |
| 416 | + elif data_type == 'TILEWMS': |
| 417 | + print('create TILEWMS layer') |
| 418 | + return self.createTileWmsLayerFromShogun(layer_src_conf) |
| 419 | + |
| 420 | + elif data_type == 'WFS': |
| 421 | + print('create WFS layer') |
| 422 | + return self.createWfsLayerFromShogun(layer_src_conf) |
| 423 | + |
| 424 | + def addQgsLayer(self, layer_in_tree, layer_group): |
406 | 425 | self.qgisLayers = [] |
407 | 426 | # layerutils |
408 | | - layer = self.createLayer(layer_src_conf) |
| 427 | + layer = self.createLayer(layer_in_tree) |
409 | 428 | if not layer: |
410 | 429 | QgsMessageLog.logMessage( |
411 | | - f"Could not create layer: {layer_src_conf}", 'QgisShogunEditor', |
| 430 | + f"Could not create layer: {layer_in_tree}", 'QgisShogunEditor', |
412 | 431 | level=Qgis.Critical |
413 | 432 | ) |
414 | 433 | return |
415 | 434 |
|
416 | 435 | QgsProject.instance().addMapLayer(layer, False) # implicit addition |
417 | 436 | layer_group.addLayer(layer) # eplicit addition |
| 437 | + return layer |
418 | 438 |
|
419 | 439 | def buildLayerTree(self, applications_layertree, layers_content, root): |
420 | 440 | if 'layerId' not in applications_layertree: |
421 | 441 | new_group = root.addGroup(applications_layertree['title']) # option: take the name of the application |
422 | 442 |
|
423 | 443 | if 'layerId' in applications_layertree: |
424 | | - result = [layer for layer in layers_content if layer.get_id() == applications_layertree['layerId']] |
425 | | - layer_src_conf = result[0].source_config |
426 | | - self.addQgsLayer(layer_src_conf, root) |
| 444 | + layer_in_tree = [layer for layer in layers_content if layer.get_id() == applications_layertree['layerId']][0] |
| 445 | + layer = self.addQgsLayer(layer_in_tree, root) |
427 | 446 |
|
428 | 447 | if 'children' in applications_layertree: |
429 | 448 | for child in applications_layertree['children']: |
|
0 commit comments