diff --git a/lizmap_server/core.py b/lizmap_server/core.py index f189e812..f8fff25f 100755 --- a/lizmap_server/core.py +++ b/lizmap_server/core.py @@ -49,15 +49,11 @@ def find_vector_layer_from_params(params, project): return True, layer -def find_vector_layer(layer_name: str, project: QgsProject) -> Optional[QgsVectorLayer]: - """ Find vector layer with name, short name or layer id. """ +def find_layer(layer_name: str, project: QgsProject) -> Optional[QgsMapLayer]: + """ Find layer with name, short name or layer ID. """ found = None for layer in project.mapLayers().values(): - # only vector layer - if layer.type() != QgsMapLayer.VectorLayer: - continue - # check name if layer.name() == layer_name: found = layer @@ -75,18 +71,30 @@ def find_vector_layer(layer_name: str, project: QgsProject) -> Optional[QgsVecto if not found: Logger.warning( - "The vector layer '{}' has not been found in the project '{}'".format(layer_name, project.fileName())) + "The layer '{}' has not been found in the project '{}'".format(layer_name, project.fileName())) return None - found: QgsVectorLayer + found: QgsMapLayer if not found.isValid(): Logger.warning( - f"The vector layer '{layer_name}' has been found but it is not valid in the project " + f"The layer '{layer_name}' has been found but it is not valid in the project " f"'{project.fileName()}'", ) return found +def find_vector_layer(layer_name: str, project: QgsProject) -> Optional[QgsVectorLayer]: + """ Find vector layer with name, short name or layer ID. """ + layer = find_layer(layer_name, project) + if not layer: + return None + + if not layer.type() == QgsMapLayer.VectorLayer: + return None + + return layer + + def get_server_fid(feature: QgsFeature, pk_attributes: list) -> str: """ Build server feature ID. """ if not pk_attributes: diff --git a/lizmap_server/get_legend_graphic.py b/lizmap_server/get_legend_graphic.py index 7d61bf01..0d86acbc 100644 --- a/lizmap_server/get_legend_graphic.py +++ b/lizmap_server/get_legend_graphic.py @@ -11,12 +11,12 @@ from collections import namedtuple from typing import Optional -from qgis.core import Qgis, QgsProject, QgsVectorLayer +from qgis.core import Qgis, QgsMapLayer, QgsProject, QgsVectorLayer from qgis.PyQt.QtCore import QBuffer, QIODevice from qgis.PyQt.QtGui import QImage from qgis.server import QgsServerFilter -from lizmap_server.core import find_vector_layer +from lizmap_server.core import find_layer from lizmap_server.logger import Logger, exception_handler from lizmap_server.tools import to_bool @@ -86,11 +86,11 @@ def responseComplete(self): show_feature_count = to_bool(params.get('SHOWFEATURECOUNT'), default_value=False) current_style = '' - layer = find_vector_layer(layer_name, project) + layer = find_layer(layer_name, project) if not layer: - logger.info("Skipping the layer '{}' because it's not a vector layer".format(layer_name)) return + layer: QgsMapLayer if not layer.isValid(): logger.warning( f"Layer '{layer_name}' is not valid, returning a warning icon in the legend for project " @@ -109,6 +109,12 @@ def responseComplete(self): handler.appendBody(json.dumps(json_data).encode('utf8')) return + if not layer.type() != QgsMapLayer.VectorLayer: + logger.info("Skipping the layer '{}' because it's not a vector layer".format(layer_name)) + return + + layer: QgsVectorLayer + try: current_style = layer.styleManager().currentStyle()