Saturday, 26 October 2019

pyqgis - Catching WMS error message from log messages panel in QGIS with python?



I am using a WMS-service as background map in an automatized atlas print process.


Unfortunately the WMS-service seems to have a problem on server side, as it is sometimes not responding (see error message: map request failed ... server replied bad request (see attached image)). By just triggering a repaint after some seconds or minutes, the WMS loads correctly.


Now I would like to catch this behaviour via python/pyqgis because the atlas print process produces empty background pages when the WMS is not responding. So I would need to check if this particular error occurs, and force a triggerRepaint() until the WMS-server responds correctly. I don´t know how, since the error appears only in the log message panel, under the tab "WMS". I already tried with try:...except... or by checking wmsLayer.isValid(), but this did not do the trick...


Is there anybody who generaly knows a way to catch this particular error appearing in the log messages panel?


Unfortunately the WMS-service is licensed, so I´m afraid the problem might not be reproducable...But I hope there is anybody out there who knows a way to catch errors from the log message panel.


I am using QGIS 2.18.2


original wms log messages panel error message


With advice from Germán I was able to catch the error in the following way:


# coding=utf-8


from qgis.core import *

wmsLayer_name="wms-dtk50_wgs"
url_with_params ='url=http://sg.geodatenzentrum.de/wms_dtk50?&crs=EPSG:25832&featureCount=10&format=image/png&layers=DTK50&styles='

wmsLayer = QgsRasterLayer(url_with_params, wmsLayer_name,'wms')
QgsMapLayerRegistry.instance().addMapLayer(wmsLayer)

def errorCatcher( msg, tag, level ):
if tag == 'WMS' and level != 0: #Warnings or Errors (0: Info, 1:Warning, 2:Error)

print "WMS error detected!"
myWMSLayer = QgsMapLayerRegistry.instance().mapLayersByName("wms-dtk50_wgs")[0]
myWMSLayer.triggerRepaint()

# connect with messageReceived SIGNAL from QgsMessageLog to an errorCatcher custom function
# instantly reacts if error/warning occurs
QgsMessageLog.instance().messageReceived.connect( errorCatcher )

#after 100 times triggering a "wmsLayer.triggerRepaint()",
# I get following warning in log messages panel "WMS":

# "2017-01-17T07:17:52 1 Not logging more than 100 request errors."

Obviously the "WMS" provider seems to have a restriction of sending error requests to the messages log...opened a new question with this issue here: How to solve issue with log messages panel in QGIS: “Not logging more than 100 request errors.”?



Answer



In a Python QGIS Console write this:


def errorCatcher( msg, tag, level ):
if tag == 'WMS' and level != 0:
print "WMS Error caught! Details: {}".format( msg )
# Do here other stuff you consider, such as triggering a repaint


QgsMessageLog.instance().messageReceived.connect( errorCatcher )

What this code snippet does is to connect the messageReceived SIGNAL from QgsMessageLog to an errorCatcher custom function, which filters all messages of the WMS tab that are Warnings or Errors (0: Info, 1:Warning, 2:Error) and allows you to react to them.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...