Python osgeo.gdal.PushErrorHandler() Examples

The following are code examples for showing how to use osgeo.gdal.PushErrorHandler(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: geobricks_qgis_plugin_trmm   Author: geobricks   File: geometry.py    GNU General Public License v2.0 6 votes vote down vote up
def ReprojectGeom(geom,src_srs,tgt_srs):
    ''' Reproject a geometry object.

        @type geom:     C{ogr.Geometry}
        @param geom:    OGR geometry object
        @type src_srs:  C{osr.SpatialReference}
        @param src_srs: OSR SpatialReference object
        @type tgt_srs:  C{osr.SpatialReference}
        @param tgt_srs: OSR SpatialReference object
        @rtype:         C{ogr.Geometry}
        @return:        OGRGeometry object
    '''
    gdal.ErrorReset()
    gdal.PushErrorHandler( 'CPLQuietErrorHandler' )
    geom.AssignSpatialReference(src_srs)
    geom.TransformTo(tgt_srs)
    err = gdal.GetLastErrorMsg()
    if err:warnings.warn(err.replace('\n',' '))
    gdal.PopErrorHandler()
    gdal.ErrorReset()
    return geom 
Example 2
Project: buzzard   Author: airware   File: helper_classes.py    Apache License 2.0 5 votes vote down vote up
def __call__(self, *args, **kwargs):
        errs, res = None, None

        def error_handler(err_level, err_no, err_msg):
            nonlocal errs
            if err_level >= gdal.CE_Failure:
                errs = err_no, err_msg

        gdal.PushErrorHandler(error_handler)
        try:
            res = self._fn(*args, **kwargs)
        except Exception as e:
            if errs is None:
                # This is not a gdal error, this might be a swig error or something else
                raise
            else:
                # This is a gdal error, and `gdal.GetUseExceptions()` is True
                # Read problems from the `errs` variable, the details stored in this exception
                # are not reliable.
                pass
        finally:
            gdal.PopErrorHandler()

        if errs:
            return False, errs
        if self._none_is_error and res is None:
            return False, (0, str(gdal.GetLastErrorMsg()).strip('\n'))
        if self._nonzero_int_is_error and isinstance(res, int) and res != 0:
            return False, (res, conv.str_of_cple(res))
        return True, res