Skip to content

Commit

Permalink
Handle invalid layer other than vector in the legend
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry committed May 15, 2024
1 parent c556ad1 commit 21ce21e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
26 changes: 17 additions & 9 deletions lizmap_server/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
14 changes: 10 additions & 4 deletions lizmap_server/get_legend_graphic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 "
Expand All @@ -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()

Expand Down

0 comments on commit 21ce21e

Please sign in to comment.