6

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.”?

  • 1
    What counts as a valid wmsLayer? Is it checking if the request URL is valid (a request can be valid according to the spec, but the service may not be able to reply), or if you have a valid response - an error response by the WMS, is still a valid response. – nmtoken Jan 5 '17 at 8:42
  • thx for the hint. that´s absolutely true, a valid layer might still give an error response, as it is the case here. edited the comment accordingly. nevertheless, it leaves my question open how to catch that error response? – ADorsch Jan 5 '17 at 8:59
6
+50

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.

  • 1
    Great! This was exactly what I was looking for, and it works! thx – ADorsch Jan 11 '17 at 9:06
  • Hi Germán! Thx for your answer again. So I used your code to trigger a repaint as soon as an error/warning is thrown into the messages log. Now I am facing a new problem: the messages log is restricted to 100 messages, so after the 100th triggerRepaint(), I am not able to catch any signal...do you know if there is a possibility to catch the error/warning directly, instead of using the messages log panel? Or clear/reset the log? – ADorsch Jan 16 '17 at 12:43
  • 1
    I'm not aware of any limitation in the log viewer. In fact, if I run this couple of lines I get 500 messages as expected: for i in range(500): QgsMessageLog.instance().logMessage("Message #{}".format(i),"Test",2) What do yo get if you do so? – Germán Carrillo Jan 16 '17 at 14:50
  • Ok, you are right, this way there do not seem to be any restriction. But obviously the "WMS" provider (or whatever instance is responsible here) does have one...pls see my edit above – ADorsch Jan 17 '17 at 6:41
  • I suggest you to open a new question with the 100-messages issue. As it is, this question was already solved, so it won't get too much attention. Also, it wouldn't be fair to declare it not solved, as the original question was properly solved. – Germán Carrillo Jan 17 '17 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.