[GIS] How to detect a GDAL/OGR ‘Warning’

gdalnetogrpython

  • When I run a batch script using a GDAL/OGR program, I can detect if an error occured by checking ERRORLEVEL for the value 1.

  • When using GDAL/OGR in python or .NET, I get an exception when an error occurs.

Question:
How do I detect if a Warning was issued during the execution?


Why?
Even though a process can execute to the end despite of warnings, the output might be corrupt with respect to the data I expect. You could argue that I ought to check for these corruptions, but since I have control of the entire process, I would like to be able to stop when a warning is issued.

Best Answer

If using GDAL 1.10+ the python bindings allow you to specify a python callable as an error handler. However, these error handlers appear to be called in a separate thread and any exceptions raised do not propagate back to the main thread. So something like this won't work:

from osgeo import gdal

def error_handler(err_level, err_no, err_msg):

    if err_class >= gdal.CE_Warning:
        raise RuntimeError(err_level, err_no, err_msg) #this exception does not propagate back to main thread!
    else: print err_msg

if __name__=='__main__':

    #Test custom error handler
    gdal.PushErrorHandler(error_handler)
    gdal.Error(gdal.CE_Warning,2,'test warning message')
    gdal.PopErrorHandler()

But you can do something like this:

from osgeo import gdal

class GdalErrorHandler(object):
    def __init__(self):
        self.err_level=gdal.CE_None
        self.err_no=0
        self.err_msg=''

    def handler(self, err_level, err_no, err_msg):
        self.err_level=err_level
        self.err_no=err_no
        self.err_msg=err_msg

if __name__=='__main__':

    err=GdalErrorHandler()
    handler=err.handler # Note don't pass class method directly or python segfaults
                        # due to a reference counting bug 
                        # http://trac.osgeo.org/gdal/ticket/5186#comment:4

    gdal.PushErrorHandler(handler)
    gdal.UseExceptions() #Exceptions will get raised on anything >= gdal.CE_Failure

    try:
        gdal.Error(gdal.CE_Warning,1,'gdal.CE_Warning warning')
    except Exception as e:
        print 'Operation raised an exception'
        print e
    else:
        print 'No exception'
        if err.err_level >= gdal.CE_Warning:
            raise RuntimeError(err.err_level, err.err_no, err.err_msg)
    finally:
        print err.err_level, err.err_no, err.err_msg
        gdal.PopErrorHandler()