Troubleshooting QGIS Server 'Layer(s) Not Valid' Exception With Shared WMS Layers
Introduction
When deploying QGIS Server with multiple projects that utilize the same WMS layer, encountering the dreaded Layer(s) not valid
exception can be a frustrating experience. This issue, which has been observed in QGIS Server versions 3.40 and later, manifests when the second project accessed attempts to load the shared WMS layer. This article delves into the intricacies of this bug, providing a comprehensive understanding of the problem, steps to reproduce it, and potential solutions.
The QGIS Server is a powerful tool for publishing QGIS projects as WMS, WFS, and WCS services. However, when dealing with multiple projects that share common data sources, such as WMS layers, unexpected issues can arise. The Layer(s) not valid
exception is one such issue, which can disrupt the smooth operation of your QGIS Server deployment. Understanding the root cause of this problem and how to address it is crucial for maintaining a reliable and efficient QGIS Server environment. This article aims to provide the necessary insights and guidance to tackle this challenge effectively.
Understanding the Bug
The core of the problem lies in how QGIS Server handles shared WMS layers across multiple projects. The error message <ServerException>Layer(s) not valid</ServerException>
indicates that the server is unable to validate the layer definition when processing a request. This typically happens when the same WMS source is used in multiple QGIS projects served by the same QGIS Server instance. The first project accessed usually works without issues, but subsequent requests to other projects using the same WMS layer trigger the error. This behavior suggests a potential caching or session management issue within QGIS Server.
When a QGIS Server serves multiple projects, it needs to efficiently manage resources and data connections. This often involves caching data and layer definitions to improve performance. However, in the case of shared WMS layers, it seems that the caching mechanism might not be correctly handling concurrent access from different projects. This can lead to a situation where the server tries to use an outdated or invalid layer definition, resulting in the Layer(s) not valid
error. Additionally, the order in which projects are accessed plays a crucial role, as the error typically occurs only for the second project accessed, further hinting at a caching or session-related problem.
Steps to Reproduce the Issue
To effectively troubleshoot and resolve the Layer(s) not valid
exception, it is essential to be able to consistently reproduce the issue. Here are the detailed steps to reproduce the problem:
1. Create Multiple QGIS Project Files
Start by creating several QGIS project files (.qgs) that contain the same WMS sources as layers. For instance, you can create projects named qgis1.qgs
, qgis2.qgs
, qgis3.qgs
, and so on. Each project should include the same WMS layer(s). A common example is using a publicly available WMS service such as the Swiss Topo pixelkarte-grau or the German TopPlusOpen web_grau.
For example, in qgis1.qgs
, you might add the following WMS layers:
-
Layer 1:
- URL:
https://wms.geo.admin.ch/?VERSION=1.3.0
- Source:
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/?VERSION%3D1.3.0
- URL:
-
Layer 2:
- URL:
https://sgx.geodatenzentrum.de/wms_topplus_open
- Source:
contextualWMSLegend=0&crs=EPSG:3857&dpiMode=7&featureCount=10&format=image/png&layers=web_grau&styles&tilePixelRatio=0&url=https://sgx.geodatenzentrum.de/wms_topplus_open
- URL:
Repeat this step for the other projects (qgis2.qgs
, qgis3.qgs
, etc.), ensuring that they contain the exact same WMS layer definitions.
2. Serve the Projects with QGIS Server
Deploy the QGIS project files to QGIS Server. A common method is to use Docker Compose, as demonstrated in the QGIS Docker repository. This involves setting up a docker-compose.yml
file that defines the QGIS Server service and mounts the directory containing the project files.
3. Test with Curl Requests
Use curl
commands to send GetCapabilities requests to the QGIS Server for each project. This will help you observe the error pattern.
- First Request: `curl