Example #1
0
def test_gdalbuildvrt_lib_warnings_and_custom_error_handler():
    class GdalErrorHandler(object):
        def __init__(self):
            self.got_failure = False
            self.got_warning = False

        def handler(self, err_level, err_no, err_msg):
            if err_level == gdal.CE_Failure:
                self.got_failure = True
            elif err_level == gdal.CE_Warning:
                self.got_warning = True

    # Heterogeneous band numbers should result in a warning from BuildVRT()
    ds_one_band = gdal.Open('../gcore/data/byte.tif')
    ds_two_band = gdal.Translate('',
                                 ds_one_band,
                                 bandList=[1, 1],
                                 format='VRT')

    err_handler = GdalErrorHandler()
    with gdaltest.error_handler(err_handler.handler):
        with gdaltest.enable_exceptions():
            vrt_ds = gdal.BuildVRT('', [ds_one_band, ds_two_band])
    assert vrt_ds
    assert not err_handler.got_failure
    assert err_handler.got_warning

    err_handler = GdalErrorHandler()
    with gdaltest.error_handler(err_handler.handler):
        with gdaltest.enable_exceptions():
            vrt_ds = gdal.BuildVRT('', [ds_two_band, ds_one_band])
    assert vrt_ds
    assert not err_handler.got_failure
    assert err_handler.got_warning
Example #2
0
def test_gdalbuildvrt_lib_strict_mode():

    with gdaltest.enable_exceptions():
        with gdaltest.error_handler():
            assert gdal.BuildVRT(
                '', ['../gcore/data/byte.tif', 'i_dont_exist.tif'],
                strict=False) is not None

    with gdaltest.enable_exceptions():
        with pytest.raises(Exception):
            gdal.BuildVRT('', ['../gcore/data/byte.tif', 'i_dont_exist.tif'],
                          strict=True)
Example #3
0
def test_numpy_rw_failure_in_readasarray():

    ds = gdal.Open('data/idontexist2.vrt')
    assert ds is not None

    exception_raised = False
    with gdaltest.enable_exceptions():
        try:
            ds.ReadAsArray()
        except RuntimeError:
            exception_raised = True
    assert exception_raised

    exception_raised = False
    with gdaltest.enable_exceptions():
        try:
            ds.GetRasterBand(1).ReadAsArray()
        except RuntimeError:
            exception_raised = True
    assert exception_raised
Example #4
0
def test_numpy_rw_failure_in_readasarray():

    if gdaltest.numpy_drv is None:
        pytest.skip()

    ds = gdal.Open('data/idontexist2.vrt')
    assert ds is not None

    exception_raised = False
    with gdaltest.enable_exceptions():
        try:
            ds.ReadAsArray()
        except RuntimeError:
            exception_raised = True
    assert exception_raised

    exception_raised = False
    with gdaltest.enable_exceptions():
        try:
            ds.GetRasterBand(1).ReadAsArray()
        except RuntimeError:
            exception_raised = True
    assert exception_raised