Skip to content

Commit

Permalink
Minor code refactoring about WMS properties #597
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry committed Aug 23, 2024
1 parent 297058f commit 211a486
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 90 deletions.
58 changes: 29 additions & 29 deletions lizmap/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@
from lizmap.toolbelt.git import current_git_hash, next_git_tag
from lizmap.toolbelt.i18n import setup_translation, tr
from lizmap.toolbelt.layer import (
get_layer_wms_parameters,
layer_property,
layer_wms_parameters,
relative_path,
remove_all_ghost_layers,
)
Expand Down Expand Up @@ -2444,18 +2444,19 @@ def from_data_to_ui_for_layer_group(self):
# deactivate wms checkbox if not needed
if key == 'externalWmsToggle':
wms_enabled = self.get_item_wms_capability(selected_item)
if wms_enabled is not None:
self.dlg.cbExternalWms.setEnabled(wms_enabled)
if wms_enabled:
self.dlg.cbExternalWms.toggled.connect(self.external_wms_toggled)
self.external_wms_toggled()
else:
self.dlg.cbExternalWms.setChecked(False)
try:
self.dlg.cbExternalWms.toggled.disconnect(self.external_wms_toggled)
except TypeError:
# The object was not connected
pass
self.dlg.cbExternalWms.setEnabled(wms_enabled)
if wms_enabled:
self.dlg.setToolTip("If the layer can be requested directly to the source provider.")
self.dlg.cbExternalWms.toggled.connect(self.external_wms_toggled)
self.external_wms_toggled()
else:
self.dlg.setToolTip("The selected item is not a WMS layer with an URL")
self.dlg.cbExternalWms.setChecked(False)
try:
self.dlg.cbExternalWms.toggled.disconnect(self.external_wms_toggled)
except TypeError:
# The object was not connected
pass

layer = self._current_selected_layer() # It can be a layer or a group

Expand Down Expand Up @@ -2567,21 +2568,20 @@ def external_wms_toggled(self):
""" Disable the format combobox is the checkbox third party WMS is checked. """
self.dlg.liImageFormat.setEnabled(not self.dlg.cbExternalWms.isChecked())

def get_item_wms_capability(self, selected_item) -> Optional[bool]:
"""
Check if an item in the tree is a layer
and if it is a WMS layer
"""
wms_enabled = False
is_layer = selected_item['type'] == 'layer'
if is_layer:
layer = self.get_qgis_layer_by_id(selected_item['id'])
if not layer:
return
if layer.providerType() in ['wms']:
if get_layer_wms_parameters(layer):
wms_enabled = True
return wms_enabled
def get_item_wms_capability(self, selected_item) -> bool:
""" Check if an item in the tree is a WMS layer. """
if not selected_item['type'] == 'layer':
return False

layer = self.get_qgis_layer_by_id(selected_item['id'])

if not layer or not layer.providerType() in ('wms', ):
return False

if not layer_wms_parameters(layer):
return False

return True

@staticmethod
def existing_group(
Expand Down Expand Up @@ -4053,7 +4053,7 @@ def project_config_file(
if isinstance(layer, QgsMapLayer) and to_bool(layer_options.get('externalWmsToggle', False)):
# Only for layers stored in disk
if layer.providerType() == 'wms':
wms_params = get_layer_wms_parameters(layer)
wms_params = layer_wms_parameters(layer)
if wms_params:
layer_options['externalAccess'] = wms_params
else:
Expand Down
73 changes: 24 additions & 49 deletions lizmap/resources/ui/ui_lizmap.ui
Original file line number Diff line number Diff line change
Expand Up @@ -2031,54 +2031,30 @@ This is different to the map maximum extent (defined in QGIS project properties,
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_25">
<item>
<widget class="QLabel" name="label_56">
<property name="font">
<font>
<weight>50</weight>
<bold>false</bold>
</font>
</property>
<property name="text">
<string>Third-party WMS layers</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="cbExternalWms">
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_59">
<property name="text">
<string>Get images directly from WMS Server</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_34">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
<widget class="QGroupBox" name="groupBox_3">
<property name="title">
<string>Third-party WMS layers</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_63">
<item>
<widget class="QLabel" name="label_59">
<property name="text">
<string>Get images directly from WMS Server</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="cbExternalWms">
<property name="text">
<string>Request directly</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QCheckBox" name="cbSingleTile">
Expand Down Expand Up @@ -5470,7 +5446,6 @@ This is different to the map maximum extent (defined in QGIS project properties,
<tabstop>group_layer_map_options</tabstop>
<tabstop>liImageFormat</tabstop>
<tabstop>inClientCacheExpiration</tabstop>
<tabstop>cbExternalWms</tabstop>
<tabstop>cbSingleTile</tabstop>
<tabstop>checkbox_server_cache</tabstop>
<tabstop>inCacheExpiration</tabstop>
Expand Down
24 changes: 24 additions & 0 deletions lizmap/test/test_toolbelt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import unittest

from qgis.core import QgsRasterLayer

from lizmap.toolbelt.layer import layer_wms_parameters
from lizmap.toolbelt.strings import human_size

__copyright__ = 'Copyright 2024, 3Liz'
Expand All @@ -17,3 +20,24 @@ def test_human_size(self):
self.assertEqual("53 KB", human_size("54512"))
self.assertEqual("14 KB", human_size(15145))
self.assertEqual("14 KB", human_size("15145"))

def test_wms_properties(self):
""" Test WMS layer properties. """
layer = QgsRasterLayer(
"contextualWMSLegend=0&crs=EPSG:2056&dpiMode=7&featureCount=10&format=image/jpeg&"
"layers=ch.swisstopo.pixelkarte-grau&styles&tilePixelRatio=0&url=https://wms.geo.admin.ch/",
"test",
"wms"
)
expected = {
'contextualWMSLegend': '0',
'crs': 'EPSG:2056',
'dpiMode': '7',
'featureCount': '10',
'format': 'image/jpeg',
'layers': 'ch.swisstopo.pixelkarte-grau',
# 'styles': '',
'tilePixelRatio': '0',
'url': 'https://wms.geo.admin.ch/',
}
self.assertDictEqual(expected, layer_wms_parameters(layer))
21 changes: 9 additions & 12 deletions lizmap/toolbelt/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
QgsMapLayer,
QgsProject,
QgsProviderRegistry,
QgsRasterLayer,
QgsVectorLayer,
)

Expand Down Expand Up @@ -85,21 +86,17 @@ def is_ghost_layer(layer):
return count == 0


def get_layer_wms_parameters(layer):
"""
Get WMS parameters for a raster WMS layers
"""
def layer_wms_parameters(layer: QgsRasterLayer):
""" WMS parameters from a WMS layer. """
uri = layer.dataProvider().dataSourceUri()
# avoid WMTS layers (not supported yet in Lizmap Web Client)
if 'wmts' in uri or 'WMTS' in uri:
if 'wmts' in uri.lower():
# Avoid WMTS layers (not supported yet in Lizmap Web Client)
# This test is fragile as WMTS might not be in the URL
return None

# Split WMS parameters
wms_params = dict((p.split('=') + [''])[:2] for p in uri.split('&'))

# urldecode WMS url
wms_params['url'] = urllib.parse.unquote(wms_params['url']).replace('&&', '&').replace('==', '=')

# noinspection PyUnresolvedReferences
wms_params = urllib.parse.parse_qs(uri)
wms_params = {k: v[0] for k, v in wms_params.items()}
return wms_params


Expand Down

0 comments on commit 211a486

Please sign in to comment.