Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.
  • 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.

share|improve this question
    
You can modify the error handling behaviour at the C level (see gdal.org/cpl__error_8h.html), including turning warning into errors and setting error handlers. I don't see anything that will expose that to non-C APIs though. –  BradHards Dec 6 '12 at 20:16
1  
I think this question is more fitted to be asked at stackoverflow.com ? –  nickves Dec 7 '12 at 0:57
    
@BradHards: I would rather not dive into the C code and compile the stuff my self. –  Chau Dec 7 '12 at 7:38
1  
@nickves: I have asked the question here because I expect the folks using GDAL/OGR to be using this forum. –  Chau Dec 7 '12 at 7:39
add comment

1 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()
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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