Example #1
0
def vsiwebhdfs_unlink():

    if gdaltest.webserver_port == 0:
        return 'skip'

    gdal.VSICurlClearCache()

    # Success
    handler = webserver.SequentialHandler()
    handler.add('DELETE', '/webhdfs/v1/foo/bar?op=DELETE', 200,
                {}, '{"boolean":true}')
    with webserver.install_http_handler(handler):
        ret = gdal.Unlink(gdaltest.webhdfs_base_connection + '/foo/bar')
    if ret != 0:
        gdaltest.post_reason('fail')
        return 'fail'

    gdal.VSICurlClearCache()


    # With permissions

    gdal.VSICurlClearCache()

    handler = webserver.SequentialHandler()
    handler.add('DELETE', '/webhdfs/v1/foo/bar?op=DELETE&user.name=root&delegation=token', 200,
                {}, '{"boolean":true}')
    with gdaltest.config_options({'WEBHDFS_USERNAME': '******',
                                  'WEBHDFS_DELEGATION': 'token'}):
        with webserver.install_http_handler(handler):
            ret = gdal.Unlink(gdaltest.webhdfs_base_connection + '/foo/bar')
        if ret != 0:
            gdaltest.post_reason('fail')
            return 'fail'

    # Failure
    handler = webserver.SequentialHandler()
    handler.add('DELETE', '/webhdfs/v1/foo/bar?op=DELETE', 200,
                {}, '{"boolean":false}')
    with webserver.install_http_handler(handler):
        with gdaltest.error_handler():
            ret = gdal.Unlink(gdaltest.webhdfs_base_connection + '/foo/bar')
    if ret != -1:
        gdaltest.post_reason('fail')
        return 'fail'

    gdal.VSICurlClearCache()

    # Failure
    handler = webserver.SequentialHandler()
    handler.add('DELETE', '/webhdfs/v1/foo/bar?op=DELETE', 404,
                {})
    with webserver.install_http_handler(handler):
        with gdaltest.error_handler():
            ret = gdal.Unlink(gdaltest.webhdfs_base_connection + '/foo/bar')
    if ret != -1:
        gdaltest.post_reason('fail')
        return 'fail'

    return 'success'
Example #2
0
def ogr_plscenes_data_v1_catalog_no_paging():

    if gdaltest.plscenes_drv is None:
        return 'skip'

    gdal.FileFromMemBuffer('/vsimem/data_v1/item-types', '{ "item_types": [ { "id": "PSScene3Band" } ] }')
    gdal.SetConfigOption('PL_URL', '/vsimem/data_v1/')
    ds = gdal.OpenEx('PLScenes:', gdal.OF_VECTOR, open_options = ['VERSION=data_v1', 'API_KEY=foo'])
    gdal.SetConfigOption('PL_URL', None)
    if ds is None:
        gdaltest.post_reason('fail')
        return 'fail'
    with gdaltest.error_handler():
        if ds.GetLayerByName('non_existing') is not None:
            gdaltest.post_reason('fail')
            return 'fail'
    if ds.GetLayerByName('PSScene3Band') is None:
        gdaltest.post_reason('fail')
        return 'fail'
    if ds.GetLayerCount() != 1:
        gdaltest.post_reason('fail')
        return 'fail'
    with gdaltest.error_handler():
        if ds.GetLayerByName('non_existing') is not None:
            gdaltest.post_reason('fail')
            return 'fail'

    gdal.Unlink('/vsimem/data_v1/item-types')

    return 'success'
Example #3
0
def stats_all_nodata():

    ds = gdal.GetDriverByName('MEM').Create('', 2000, 2000)
    ds.GetRasterBand(1).SetNoDataValue(0)
    approx_ok = 1
    force = 1
    with gdaltest.error_handler():
        stats = ds.GetRasterBand(1).GetStatistics(approx_ok, force)
    if stats != [0.0, 0.0, 0.0, 0.0]:
        gdaltest.post_reason('did not get expected stats')
        print(stats)
        return 'fail'

    ds = gdal.GetDriverByName('MEM').Create('', 2000, 2000, 1,
                                            gdal.GDT_Float32)
    ds.GetRasterBand(1).SetNoDataValue(0)
    approx_ok = 1
    force = 1
    with gdaltest.error_handler():
        stats = ds.GetRasterBand(1).GetStatistics(approx_ok, force)
    if stats != [0.0, 0.0, 0.0, 0.0]:
        gdaltest.post_reason('did not get expected stats')
        print(stats)
        return 'fail'

    return 'success'
Example #4
0
def test_postgisraster_test_serial():
    """
    Test the ability to connect to a data source if it has no primary key,
    but uses a sequence instead.
    """
    if gdaltest.postgisrasterDriver is None:
        pytest.skip()

    with gdaltest.error_handler():
        src_ds = gdal.Open(gdaltest.postgisraster_connection_string + "table='small_world_serial'")

    src_md = src_ds.GetMetadata('SUBDATASETS')

    import re

    # Check each subdataset
    for k in src_md.keys():
        if k[-4:] == 'NAME':
            # Ensure the subdataset has upperleftx and upperlefty coords,
            # as there is no unique key on the table
            assert re.search("where='\"serialid\" = \d+'", src_md[k]), (k, ':', src_md[k])

    with gdaltest.error_handler():
        ds = gdal.Open(gdaltest.postgisraster_connection_string + "table='small_world_serial' mode=2")
    cs = [ds.GetRasterBand(i + 1).Checksum() for i in range(3)]
    assert cs == [30111, 32302, 40026]
Example #5
0
def vrtderived_8():

    try:
        import numpy
        numpy.ones
    except (ImportError, AttributeError):
        return 'skip'

    gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', 'NO')
    ds = gdal.Open('data/n43_hillshade.vrt')
    with gdaltest.error_handler():
        cs = ds.GetRasterBand(1).Checksum()
    gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', None)
    if cs != 0:
        gdaltest.post_reason('invalid checksum')
        print(cs)
        return 'fail'

    ds = gdal.Open('data/n43_hillshade.vrt')
    with gdaltest.error_handler():
        cs = ds.GetRasterBand(1).Checksum()
    if cs != 0:
        gdaltest.post_reason('invalid checksum')
        print(cs)
        return 'fail'

    return 'success'
Example #6
0
def test_visoss_1():

    if not gdaltest.built_against_curl():
        pytest.skip()

    # Missing OSS_SECRET_ACCESS_KEY
    gdal.ErrorReset()
    with gdaltest.error_handler():
        f = open_for_read('/vsioss/foo/bar')
    assert f is None and gdal.VSIGetLastErrorMsg().find('OSS_SECRET_ACCESS_KEY') >= 0

    gdal.ErrorReset()
    with gdaltest.error_handler():
        f = open_for_read('/vsioss_streaming/foo/bar')
    assert f is None and gdal.VSIGetLastErrorMsg().find('OSS_SECRET_ACCESS_KEY') >= 0

    gdal.SetConfigOption('OSS_SECRET_ACCESS_KEY', 'OSS_SECRET_ACCESS_KEY')

    # Missing OSS_ACCESS_KEY_ID
    gdal.ErrorReset()
    with gdaltest.error_handler():
        f = open_for_read('/vsioss/foo/bar')
    assert f is None and gdal.VSIGetLastErrorMsg().find('OSS_ACCESS_KEY_ID') >= 0

    gdal.SetConfigOption('OSS_ACCESS_KEY_ID', 'OSS_ACCESS_KEY_ID')
Example #7
0
def basic_test_15():

    try:
        with gdaltest.error_handler():
            gdal.GetDriverByName('MEM').CreateCopy('', gdal.GetDriverByName('MEM').Create('',1,1), callback = 'foo')
        gdaltest.post_reason('fail')
        return 'fail'
    except:
        pass

    with gdaltest.error_handler():
        ds = gdal.GetDriverByName('MEM').CreateCopy('', gdal.GetDriverByName('MEM').Create('',1,1), callback = basic_test_15_cbk_no_argument)
    if ds is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    with gdaltest.error_handler():
        ds = gdal.GetDriverByName('MEM').CreateCopy('', gdal.GetDriverByName('MEM').Create('',1,1), callback = basic_test_15_cbk_no_ret)
    if ds is None:
        gdaltest.post_reason('fail')
        return 'fail'

    with gdaltest.error_handler():
        ds = gdal.GetDriverByName('MEM').CreateCopy('', gdal.GetDriverByName('MEM').Create('',1,1), callback = basic_test_15_cbk_bad_ret)
    if ds is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    return 'success'
def test_gdal_translate_lib_colorinterp():

    src_ds = gdal.Open('../gcore/data/rgbsmall.tif')

    # Less bands specified than available
    ds = gdal.Translate('', src_ds, options='-f MEM -colorinterp blue,gray')
    assert ds.GetRasterBand(1).GetColorInterpretation() == gdal.GCI_BlueBand
    assert ds.GetRasterBand(2).GetColorInterpretation() == gdal.GCI_GrayIndex
    assert ds.GetRasterBand(3).GetColorInterpretation() == gdal.GCI_BlueBand

    # More bands specified than available and a unknown color interpretation
    with gdaltest.error_handler():
        ds = gdal.Translate('', src_ds, options='-f MEM -colorinterp alpha,red,undefined,foo')
    assert ds.GetRasterBand(1).GetColorInterpretation() == gdal.GCI_AlphaBand
    assert ds.GetRasterBand(2).GetColorInterpretation() == gdal.GCI_RedBand
    assert ds.GetRasterBand(3).GetColorInterpretation() == gdal.GCI_Undefined

    # Test colorinterp_
    ds = gdal.Translate('', src_ds, options='-f MEM -colorinterp_2 alpha')
    assert ds.GetRasterBand(1).GetColorInterpretation() == gdal.GCI_RedBand
    assert ds.GetRasterBand(2).GetColorInterpretation() == gdal.GCI_AlphaBand
    assert ds.GetRasterBand(3).GetColorInterpretation() == gdal.GCI_BlueBand

    # Test invalid colorinterp_
    with pytest.raises(Exception):
        with gdaltest.error_handler():
            gdal.Translate('', src_ds, options='-f MEM -colorinterp_0 alpha')
Example #9
0
def test_gdalwarp_lib_121():

    # No option
    with gdaltest.error_handler():
        gdal.wrapper_GDALWarpDestName('', [], None)

    # Will create an implicit options structure
    with gdaltest.error_handler():
        gdal.wrapper_GDALWarpDestName('', [], None, gdal.TermProgress)

    # Null dest name
    try:
        gdal.wrapper_GDALWarpDestName(None, [], None)
    except:
        pass

    # No option
    with gdaltest.error_handler():
        gdal.wrapper_GDALWarpDestDS(gdal.GetDriverByName('MEM').Create('', 1, 1), [], None)

    # Will create an implicit options structure
    with gdaltest.error_handler():
        gdal.wrapper_GDALWarpDestDS(gdal.GetDriverByName('MEM').Create('', 1, 1), [], None, gdal.TermProgress)

    return 'success'
Example #10
0
def numpy_rw_16():

    if gdaltest.numpy_drv is None:
        return 'skip'

    import numpy
    from osgeo import gdal_array

    # 1D
    array = numpy.empty([1], numpy.uint8)
    with gdaltest.error_handler():
        ds = gdal_array.OpenArray(array)
    if ds is not None:
        gdaltest.post_reason('failure')
        return 'fail'

    # 4D
    array = numpy.empty([1, 1, 1, 1], numpy.uint8)
    with gdaltest.error_handler():
        ds = gdal_array.OpenArray(array)
    if ds is not None:
        gdaltest.post_reason('failure')
        return 'fail'

    # Unsupported data type
    array = numpy.empty([1, 1], numpy.float16)
    with gdaltest.error_handler():
        ds = gdal_array.OpenArray(array)
    if ds is not None:
        gdaltest.post_reason('failure')
        return 'fail'

    return 'success'
Example #11
0
def numpy_rw_17():

    if gdaltest.numpy_drv is None:
        return 'skip'

    import numpy
    from osgeo import gdal_array

    # Disabled by default
    array = numpy.empty([1, 1], numpy.uint8)
    with gdaltest.error_handler():
        ds = gdal.Open(gdal_array.GetArrayFilename(array))
    if ds is not None:
        gdaltest.post_reason('failure')
        return 'fail'

    gdal.SetConfigOption('GDAL_ARRAY_OPEN_BY_FILENAME', 'TRUE')
    ds = gdal.Open(gdal_array.GetArrayFilename(array))
    gdal.SetConfigOption('GDAL_ARRAY_OPEN_BY_FILENAME', None)
    if ds is None:
        gdaltest.post_reason('failure')
        return 'fail'

    # Invalid value
    with gdaltest.error_handler():
        ds = gdal.Open('NUMPY:::invalid')
    if ds is not None:
        gdaltest.post_reason('failure')
        return 'fail'

    return 'success'
Example #12
0
def test_ogr_pds_1():

    ds = ogr.Open('data/ap01578l.lbl')
    assert ds is not None, 'cannot open dataset'

    lyr = ds.GetLayerByName('RAMAPPING')
    assert lyr is not None, 'cannot find layer'

    assert lyr.GetFeatureCount() == 74786, 'did not get expected feature count'

    with gdaltest.error_handler():
        feat = lyr.GetNextFeature()
    if feat.GetField('NOISE_COUNTS_1') != 96:
        feat.DumpReadable()
        pytest.fail()
    geom = feat.GetGeometryRef()
    if ogrtest.check_feature_geometry(feat, 'POINT (146.1325 -55.648)',
                                      max_error=0.000000001) != 0:
        print('did not get expected geom')
        pytest.fail(geom.ExportToWkt())

    with gdaltest.error_handler():
        feat = lyr.GetFeature(1)
    if feat.GetField('MARS_RADIUS') != 3385310.2:
        feat.DumpReadable()
        pytest.fail()
Example #13
0
def jpeg_27():

    # Should error out with 'Reading this strip would require
    # libjpeg to allocate at least...'
    gdal.ErrorReset()
    ds = gdal.Open('/vsisubfile/146,/vsizip/../gcore/data/eofloop_valid_huff.tif.zip')
    with gdaltest.error_handler():
        cs = ds.GetRasterBand(1).Checksum()
        if cs != 0 or gdal.GetLastErrorMsg() == '':
            gdaltest.post_reason('fail')
            return 'fail'

    # Should error out with 'Scan number...
    gdal.ErrorReset()
    ds = gdal.Open('/vsisubfile/146,/vsizip/../gcore/data/eofloop_valid_huff.tif.zip')
    with gdaltest.error_handler():
        gdal.SetConfigOption('GDAL_ALLOW_LARGE_LIBJPEG_MEM_ALLOC', 'YES')
        gdal.SetConfigOption('GDAL_JPEG_MAX_ALLOWED_SCAN_NUMBER', '10')
        cs = ds.GetRasterBand(1).Checksum()
        gdal.SetConfigOption('GDAL_ALLOW_LARGE_LIBJPEG_MEM_ALLOC', None)
        gdal.SetConfigOption('GDAL_JPEG_MAX_ALLOWED_SCAN_NUMBER', None)
        if gdal.GetLastErrorMsg() == '':
            gdaltest.post_reason('fail')
            return 'fail'

    return 'success'
Example #14
0
def test_derived_test3():

    with gdaltest.error_handler():
        # Missing filename
        ds = gdal.Open('DERIVED_SUBDATASET:LOGAMPLITUDE')
    assert ds is None

    with gdaltest.error_handler():
        ds = gdal.Open('DERIVED_SUBDATASET:invalid_alg:../gcore/data/byte.tif')
    assert ds is None

    with gdaltest.error_handler():
        ds = gdal.Open('DERIVED_SUBDATASET:LOGAMPLITUDE:dataset_does_not_exist')
    assert ds is None

    with gdaltest.error_handler():
        # Raster with zero band
        ds = gdal.Open('DERIVED_SUBDATASET:LOGAMPLITUDE:data/CSK_DGM.h5')
    assert ds is None

    for function in ['real', 'imag', 'complex', 'mod', 'phase', 'conj',
                     'sum', 'diff', 'mul', 'cmul', 'inv', 'intensity',
                     'sqrt', 'log10', 'dB', 'dB2amp', 'dB2pow']:
        ds = gdal.Open('<VRTDataset rasterXSize="1" rasterYSize="1"><VRTRasterBand subClass="VRTDerivedRasterBand"><PixelFunctionType>%s</PixelFunctionType></VRTRasterBand></VRTDataset>' % function)
        with gdaltest.error_handler():
            ds.GetRasterBand(1).Checksum()
Example #15
0
def test_vrtmask_11():

    # Cannot create mask band at raster band level when a dataset mask band already exists
    ds = gdal.Translate('', 'data/byte.tif', format='VRT')
    ds.CreateMaskBand(gdal.GMF_PER_DATASET)
    with gdaltest.error_handler():
        ret = ds.GetRasterBand(1).CreateMaskBand(0)
    assert ret != 0, 'expected an error, but got success'

    # This VRT dataset has already a mask band
    ds = gdal.Translate('', 'data/byte.tif', format='VRT')
    ds.CreateMaskBand(gdal.GMF_PER_DATASET)
    with gdaltest.error_handler():
        ret = ds.CreateMaskBand(gdal.GMF_PER_DATASET)
    assert ret != 0, 'expected an error, but got success'

    # This VRT band has already a mask band
    ds = gdal.Translate('', 'data/byte.tif', format='VRT')
    ds.GetRasterBand(1).CreateMaskBand(0)
    with gdaltest.error_handler():
        ret = ds.GetRasterBand(1).CreateMaskBand(0)
    assert ret != 0, 'expected an error, but got success'

    ds = gdal.Translate('', 'data/byte.tif', format='VRT')
    ret = ds.GetRasterBand(1).CreateMaskBand(gdal.GMF_PER_DATASET)
    assert ret == 0
Example #16
0
def ogr_pds_1():

    ds = ogr.Open('data/ap01578l.lbl')
    if ds is None:
        gdaltest.post_reason('cannot open dataset')
        return 'fail'

    lyr = ds.GetLayerByName('RAMAPPING')
    if lyr is None:
        gdaltest.post_reason('cannot find layer')
        return 'fail'

    if lyr.GetFeatureCount() != 74786:
        gdaltest.post_reason('did not get expected feature count')
        return 'fail'

    with gdaltest.error_handler():
        feat = lyr.GetNextFeature()
    if feat.GetField('NOISE_COUNTS_1') != 96:
        feat.DumpReadable()
        return 'fail'
    geom = feat.GetGeometryRef()
    if ogrtest.check_feature_geometry(feat,'POINT (146.1325 -55.648)',
                                      max_error = 0.000000001 ) != 0:
        print('did not get expected geom')
        print(geom.ExportToWkt())
        return 'fail'

    with gdaltest.error_handler():
        feat = lyr.GetFeature(1)
    if feat.GetField('MARS_RADIUS') != 3385310.2:
        feat.DumpReadable()
        return 'fail'

    return 'success'
Example #17
0
def postgisraster_test_unique():
    """
    Test the ability to connect to a data source if it has no primary key,
    but uses a unique constraint instead.
    """
    if gdaltest.postgisrasterDriver is None:
        return 'skip'

    with gdaltest.error_handler():
        src_ds = gdal.Open(gdaltest.postgisraster_connection_string + "table='small_world_unique'")

    src_md = src_ds.GetMetadata('SUBDATASETS')

    import re

    # Check each subdataset
    for k in src_md.keys():
        if k[-4:] == 'NAME':
            # Ensure the subdataset has upperleftx and upperlefty coords,
            # as there is no unique key on the table
            if not re.search("where='\"uniq\" = \d+'", src_md[k]):
                print(k, ':', src_md[k])
                return 'fail'

    with gdaltest.error_handler():
        ds = gdal.Open(gdaltest.postgisraster_connection_string + "table='small_world_unique' mode=2")
    cs = [ds.GetRasterBand(i + 1).Checksum() for i in range(3)]
    if cs != [30111, 32302, 40026]:
        gdaltest.post_reason('fail')
        print(cs)
        return 'fail'

    return 'success'
Example #18
0
def postgisraster_test_norid():
    """
    Test the ability to connect to a data source if it has no 'rid' column.
    """
    if gdaltest.postgisrasterDriver is None:
        return 'skip'

    with gdaltest.error_handler():
        src_ds = gdal.Open(gdaltest.postgisraster_connection_string + "table='small_world_noid'")

    src_md = src_ds.GetMetadata('SUBDATASETS')

    # Check each subdataset
    for k in src_md.keys():
        if k[-4:] == 'NAME':
            # Ensure the subdataset has upperleftx and upperlefty coords,
            # as there is no unique key on the table
            if src_md[k].find('ST_UpperLeftX') < 0 or src_md[k].find('ST_UpperLeftY') < 0:
                print(src_md[k])
                return 'fail'

    with gdaltest.error_handler():
        ds = gdal.Open(gdaltest.postgisraster_connection_string + "table='small_world_noid' mode=2")
    cs = [ds.GetRasterBand(i + 1).Checksum() for i in range(3)]
    if cs != [30111, 32302, 40026]:
        gdaltest.post_reason('fail')
        print(cs)
        return 'fail'

    return 'success'
Example #19
0
def test_numpy_rw_16():

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

    import numpy
    from osgeo import gdal_array

    # 1D
    array = numpy.empty([1], numpy.uint8)
    with gdaltest.error_handler():
        ds = gdal_array.OpenArray(array)
    assert ds is None

    # 4D
    array = numpy.empty([1, 1, 1, 1], numpy.uint8)
    with gdaltest.error_handler():
        ds = gdal_array.OpenArray(array)
    assert ds is None

    # Unsupported data type
    array = numpy.empty([1, 1], numpy.float16)
    with gdaltest.error_handler():
        ds = gdal_array.OpenArray(array)
    assert ds is None
Example #20
0
def derived_test3():

    with gdaltest.error_handler():
        # Missing filename
        ds = gdal.Open('DERIVED_SUBDATASET:LOGAMPLITUDE')
    if ds is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    with gdaltest.error_handler():
        ds = gdal.Open('DERIVED_SUBDATASET:invalid_alg:../gcore/data/byte.tif')
    if ds is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    with gdaltest.error_handler():
        ds = gdal.Open('DERIVED_SUBDATASET:LOGAMPLITUDE:dataset_does_not_exist')
    if ds is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    with gdaltest.error_handler():
        # Raster with zero band
        ds = gdal.Open('DERIVED_SUBDATASET:LOGAMPLITUDE:data/CSK_DGM.h5')
    if ds is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    return 'success'
Example #21
0
def vrtderived_12():

    try:
        import numpy
        numpy.ones
    except:
        return 'skip'

    for dt in [ "Byte", "UInt16", "Int16", "UInt32", "Int32",
                "Float32", "Float64",
                "CInt16", "CInt32", "CFloat32", "CFloat64" ]:
        ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10">
<VRTRasterBand dataType="%s" band="1" subClass="VRTDerivedRasterBand">
    <ColorInterp>Gray</ColorInterp>
    <PixelFunctionType>vrtderived.one_pix_func</PixelFunctionType>
    <PixelFunctionLanguage>Python</PixelFunctionLanguage>
</VRTRasterBand>
</VRTDataset>""" % dt)

        gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', "YES")
        with gdaltest.error_handler():
            cs = ds.GetRasterBand(1).Checksum()
        gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', None)
        # CInt16/CInt32 do not map to native numpy types
        if dt == 'CInt16' or dt == 'CInt32':
            expected_cs = 0 # error
        else:
            expected_cs = 100
        if cs != expected_cs:
            gdaltest.post_reason( 'invalid checksum' )
            print(dt)
            print(cs)
            print(gdal.GetLastErrorMsg())
            return 'fail'

    # Same for SourceTransferType
    for dt in [ "CInt16", "CInt32" ]:
        ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10">
<VRTRasterBand dataType="%s" band="1" subClass="VRTDerivedRasterBand">
    <SourceTransferType>Byte</SourceTransferType>
    <ColorInterp>Gray</ColorInterp>
    <PixelFunctionType>vrtderived.one_pix_func</PixelFunctionType>
    <PixelFunctionLanguage>Python</PixelFunctionLanguage>
</VRTRasterBand>
</VRTDataset>""" % dt)

        gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', "YES")
        with gdaltest.error_handler():
            cs = ds.GetRasterBand(1).Checksum()
        gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', None)
        if cs != 0:
            gdaltest.post_reason( 'invalid checksum' )
            print(dt)
            print(cs)
            print(gdal.GetLastErrorMsg())
            return 'fail'


    return 'success'
Example #22
0
File: pds.py Project: hdfeos/gdal
def pds_10():

    gdal.FileFromMemBuffer('/vsimem/pds_10',
                           """PDS_VERSION_ID                       = "PDS3"
DATA_FORMAT                          = "PDS"
^IMAGE                               = 1 <BYTES>

# Non sensical but just to parse nested arrays
NOTE                                 = ((1, 2, 3))
PRODUCT_ID                           = ({1, 2}, {3,4})

OBJECT                               = IMAGE
    BANDS                            = 1
    BAND_STORAGE_TYPE                = "BAND SEQUENTIAL"
    LINES                            = 1
    LINE_SAMPLES                     = 1
    SAMPLE_BITS                      = 8

END_OBJECT                           = IMAGE

END
""")

    ds = gdal.Open('/vsimem/pds_10')

    if ds.GetMetadataItem('NOTE') != '((1,2,3))':
        gdaltest.post_reason('fail')
        print(ds.GetMetadataItem('NOTE'))
        return 'fail'

    if ds.GetMetadataItem('PRODUCT_ID') != '({1,2},{3,4})':
        gdaltest.post_reason('fail')
        print(ds.GetMetadataItem('NOTE'))
        return 'fail'

    gdal.FileFromMemBuffer('/vsimem/pds_10',
                           """PDS_VERSION_ID                       = "PDS3"
# Unpaired
NOTE                                 = (x, y}
END
""")

    with gdaltest.error_handler():
        gdal.Open('/vsimem/pds_10')

    gdal.FileFromMemBuffer('/vsimem/pds_10',
                           """PDS_VERSION_ID                       = "PDS3"
# Unpaired
NOTE                                 = {x, y)
END
""")

    with gdaltest.error_handler():
        gdal.Open('/vsimem/pds_10')

    gdal.Unlink('/vsimem/pds_10')

    return 'success'
Example #23
0
def vsis3_1():
    try:
        drv = gdal.GetDriverByName("HTTP")
    except:
        drv = None

    if drv is None:
        return "skip"

    # Missing AWS_SECRET_ACCESS_KEY
    gdal.ErrorReset()
    with gdaltest.error_handler():
        f = gdal.VSIFOpenL("/vsis3/foo/bar", "rb")
    if f is not None or gdal.GetLastErrorMsg().find("AWS_SECRET_ACCESS_KEY") < 0:
        gdaltest.post_reason("fail")
        print(gdal.GetLastErrorMsg())
        return "fail"

    gdal.ErrorReset()
    with gdaltest.error_handler():
        f = gdal.VSIFOpenL("/vsis3_streaming/foo/bar", "rb")
    if f is not None or gdal.GetLastErrorMsg().find("AWS_SECRET_ACCESS_KEY") < 0:
        gdaltest.post_reason("fail")
        print(gdal.GetLastErrorMsg())
        return "fail"

    gdal.SetConfigOption("AWS_SECRET_ACCESS_KEY", "AWS_SECRET_ACCESS_KEY")

    # Missing AWS_ACCESS_KEY_ID
    gdal.ErrorReset()
    with gdaltest.error_handler():
        f = gdal.VSIFOpenL("/vsis3/foo/bar", "rb")
    if f is not None or gdal.GetLastErrorMsg().find("AWS_ACCESS_KEY_ID") < 0:
        gdaltest.post_reason("fail")
        print(gdal.GetLastErrorMsg())
        return "fail"

    gdal.SetConfigOption("AWS_ACCESS_KEY_ID", "AWS_ACCESS_KEY_ID")

    # ERROR 1: The AWS Access Key Id you provided does not exist in our records.
    gdal.ErrorReset()
    with gdaltest.error_handler():
        f = gdal.VSIFOpenL("/vsis3/foo/bar.baz", "rb")
    if f is not None or gdal.GetLastErrorMsg() == "":
        gdaltest.post_reason("fail")
        print(gdal.GetLastErrorMsg())
        return "fail"

    gdal.ErrorReset()
    with gdaltest.error_handler():
        f = gdal.VSIFOpenL("/vsis3_streaming/foo/bar.baz", "rb")
    if f is not None or gdal.GetLastErrorMsg() == "":
        gdaltest.post_reason("fail")
        print(gdal.GetLastErrorMsg())
        return "fail"

    return "success"
Example #24
0
def postgisraster_init():
    gdaltest.postgisrasterDriver = gdal.GetDriverByName('PostGISRaster')
    if gdaltest.postgisrasterDriver is None:
        return 'skip'

    if gdal.GetConfigOption('APPVEYOR'):
        gdaltest.postgisrasterDriver = None
        return 'skip'

    val = gdal.GetConfigOption('GDAL_PG_CONNECTION_STRING', None)
    if val is not None:
        gdaltest.pg_connection_string = val
    else:
        gdaltest.pg_connection_string = "host='localhost' dbname='autotest'"

    gdaltest.postgisraster_connection_string_without_schema = "PG:" + gdaltest.pg_connection_string
    gdaltest.postgisraster_connection_string = gdaltest.postgisraster_connection_string_without_schema

    # Make sure we have SRID=26711 in spatial_ref_sys
    with gdaltest.error_handler():
        ds = ogr.Open(gdaltest.postgisraster_connection_string, update=1)
    if ds is None:
        gdaltest.postgisrasterDriver = None
        return 'skip'

    sr = osr.SpatialReference()
    sr.ImportFromEPSG(26711)
    wkt = sr.ExportToWkt()
    proj4 = sr.ExportToProj4()
    ds.ExecuteSQL("DELETE FROM spatial_ref_sys WHERE auth_srid = 26711")
    ds.ExecuteSQL("INSERT INTO spatial_ref_sys (srid,auth_name,auth_srid,srtext,proj4text) VALUES (26711,'EPSG',26711,'%s','%s')" % (wkt, proj4))
    ds.ExecuteSQL("ALTER DATABASE autotest SET postgis.gdal_enabled_drivers TO 'GTiff PNG JPEG'")
    ds.ExecuteSQL("ALTER DATABASE autotest SET postgis.enable_outdb_rasters = true")
    ds.ExecuteSQL("SELECT pg_reload_conf()")
    ds = None

    gdaltest.postgisraster_connection_string += " schema='gis_schema' "

    with gdaltest.error_handler():
        ds = gdal.Open(gdaltest.postgisraster_connection_string + "table='utm'")

    # If we cannot open the table, try force loading the data
    if ds is None:
        gdaltest.runexternal('bash data/load_postgisraster_test_data.sh')

        with gdaltest.error_handler():
            ds = gdal.Open(gdaltest.postgisraster_connection_string + "table='utm'")

    if ds is None:
        gdaltest.postgisrasterDriver = None

    if gdaltest.postgisrasterDriver is None:
        return 'skip'

    return 'success'
Example #25
0
def test_vsicrypt_6(testnonboundtoswig_setup):  # noqa

    try:
        import ctypes
    except ImportError:
        pytest.skip()

    testnonboundtoswig_setup.VSISetCryptKey.argtypes = [ctypes.c_char_p, ctypes.c_int]
    testnonboundtoswig_setup.VSISetCryptKey.restype = None

    # Set a valid key
    testnonboundtoswig_setup.VSISetCryptKey('DONT_USE_IN_PROD'.encode('ASCII'), 16)

    if not gdaltest.has_vsicrypt:
        pytest.skip()

    fp = gdal.VSIFOpenL('/vsicrypt/add_key_check=yes,file=/vsimem/file.bin', 'wb+')
    assert fp is not None
    gdal.VSIFWriteL('hello', 1, 5, fp)
    gdal.VSIFCloseL(fp)

    fp = gdal.VSIFOpenL('/vsicrypt//vsimem/file.bin', 'rb')
    content = gdal.VSIFReadL(1, 5, fp).decode('latin1')
    gdal.VSIFCloseL(fp)

    assert content == 'hello'

    fp = gdal.VSIFOpenL('/vsicrypt//vsimem/file.bin', 'wb+')
    assert fp is not None
    gdal.VSIFWriteL('hello', 1, 5, fp)
    gdal.VSIFCloseL(fp)

    fp = gdal.VSIFOpenL('/vsicrypt//vsimem/file.bin', 'rb')
    content = gdal.VSIFReadL(1, 5, fp).decode('latin1')
    gdal.VSIFCloseL(fp)

    assert content == 'hello'

    # Set a too short key
    testnonboundtoswig_setup.VSISetCryptKey('bbc'.encode('ASCII'), 3)
    with gdaltest.error_handler():
        fp = gdal.VSIFOpenL('/vsicrypt//vsimem/file.bin', 'rb')
    assert fp is None

    with gdaltest.error_handler():
        fp = gdal.VSIFOpenL('/vsicrypt//vsimem/file.bin', 'wb+')
    assert fp is None

    # Erase key
    testnonboundtoswig_setup.VSISetCryptKey(None, 0)
    with gdaltest.error_handler():
        fp = gdal.VSIFOpenL('/vsicrypt//vsimem/file.bin', 'wb+')
    assert fp is None

    gdal.Unlink('/vsimem/file.bin')
Example #26
0
def vsiswift_real_server_errors():

    if not gdaltest.built_against_curl():
        return 'skip'

    # Nothing set
    gdal.ErrorReset()
    with gdaltest.error_handler():
        f = open_for_read('/vsiswift/foo/bar')
    if f is not None or gdal.VSIGetLastErrorMsg().find('SWIFT_STORAGE_URL') < 0:
        gdaltest.post_reason('fail')
        print(gdal.VSIGetLastErrorMsg())
        return 'fail'

    gdal.ErrorReset()
    with gdaltest.error_handler():
        f = open_for_read('/vsiswift_streaming/foo/bar')
    if f is not None or gdal.VSIGetLastErrorMsg().find('SWIFT_STORAGE_URL') < 0:
        gdaltest.post_reason('fail')
        print(gdal.VSIGetLastErrorMsg())
        return 'fail'


    gdal.SetConfigOption('SWIFT_STORAGE_URL', 'http://0.0.0.0')

    # Missing SWIFT_AUTH_TOKEN
    gdal.ErrorReset()
    with gdaltest.error_handler():
        f = open_for_read('/vsiswift/foo/bar')
    if f is not None or gdal.VSIGetLastErrorMsg().find('SWIFT_AUTH_TOKEN') < 0:
        gdaltest.post_reason('fail')
        print(gdal.VSIGetLastErrorMsg())
        return 'fail'

    gdal.SetConfigOption('SWIFT_AUTH_TOKEN', 'SWIFT_AUTH_TOKEN')

    gdal.ErrorReset()
    with gdaltest.error_handler():
        f = open_for_read('/vsiswift/foo/bar.baz')
    if f is not None:
        if f is not None:
            gdal.VSIFCloseL(f)
        gdaltest.post_reason('fail')
        print(gdal.VSIGetLastErrorMsg())
        return 'fail'

    gdal.ErrorReset()
    with gdaltest.error_handler():
        f = open_for_read('/vsiswift_streaming/foo/bar.baz')
    if f is not None:
        gdaltest.post_reason('fail')
        print(gdal.VSIGetLastErrorMsg())
        return 'fail'

    return 'success'
Example #27
0
def test_mask_25():

    ds = gdal.GetDriverByName('GTiff').Create('/vsimem/mask_25.tif', 1, 1)
    assert ds.GetRasterBand(1).GetMaskFlags() == gdal.GMF_ALL_VALID
    ds = None

    # No INTERNAL_MASK_FLAGS_x metadata
    gdal.GetDriverByName('GTiff').Create('/vsimem/mask_25.tif.msk', 1, 1)
    ds = gdal.Open('/vsimem/mask_25.tif')
    assert ds.GetRasterBand(1).GetMaskFlags() == gdal.GMF_ALL_VALID
    cs = ds.GetRasterBand(1).GetMaskBand().Checksum()
    assert cs == 3
    ds = None
    gdal.Unlink('/vsimem/mask_25.tif')
    gdal.Unlink('/vsimem/mask_25.tif.msk')

    # Per-band mask
    ds = gdal.GetDriverByName('GTiff').Create('/vsimem/mask_25.tif', 1, 1)
    ds.GetRasterBand(1).CreateMaskBand(0)
    ds = None
    ds = gdal.Open('/vsimem/mask_25.tif')
    assert ds.GetRasterBand(1).GetMaskFlags() == 0
    cs = ds.GetRasterBand(1).GetMaskBand().Checksum()
    assert cs == 0
    ds = None
    gdal.Unlink('/vsimem/mask_25.tif')
    gdal.Unlink('/vsimem/mask_25.tif.msk')

    # .msk file does not have enough bands
    gdal.GetDriverByName('GTiff').Create('/vsimem/mask_25.tif', 1, 1, 2)
    ds = gdal.GetDriverByName('GTiff').Create('/vsimem/mask_25.tif.msk', 1, 1)
    ds.SetMetadataItem('INTERNAL_MASK_FLAGS_2', '0')
    ds = None
    ds = gdal.Open('/vsimem/mask_25.tif')
    with gdaltest.error_handler():
        assert ds.GetRasterBand(2).GetMaskFlags() == gdal.GMF_ALL_VALID
    ds = None
    gdal.Unlink('/vsimem/mask_25.tif')
    gdal.Unlink('/vsimem/mask_25.tif.msk')

    # Invalid sequences of CreateMaskBand() calls
    ds = gdal.GetDriverByName('GTiff').Create('/vsimem/mask_25.tif', 1, 1, 2)
    ds.GetRasterBand(1).CreateMaskBand(gdal.GMF_PER_DATASET)
    with gdaltest.error_handler():
        assert ds.GetRasterBand(2).CreateMaskBand(0) != 0
    ds = None
    gdal.Unlink('/vsimem/mask_25.tif')
    gdal.Unlink('/vsimem/mask_25.tif.msk')

    # CreateMaskBand not supported by this dataset
    with gdaltest.error_handler():
        ds = gdal.GetDriverByName('MEM').Create('', 1, 1)
        ds.CreateMaskBand(0)
Example #28
0
def test_pds4_8():

    filename = '/vsimem/out.xml'
    for proj4 in ['+proj=eqc +lat_ts=43.75 +lat_0=10 +lon_0=-112.5 +x_0=0 +y_0=0 +R=2439400 +units=m +no_defs',
                  '+proj=lcc +lat_1=10 +lat_0=10 +lon_0=-112.5 +k_0=0.9 +x_0=0 +y_0=0 +R=2439400 +units=m +no_defs',  # LCC_1SP
                  '+proj=lcc +lat_0=10 +lon_0=-112.5 +lat_1=9 +lat_2=11 +x_0=0 +y_0=0 +R=2439400 +units=m +no_defs',  # LCC_2SP
                  '+proj=omerc +lat_0=10 +lonc=11 +alpha=12 +gamma=12 +k=0.9 +x_0=0 +y_0=0 +R=2439400 +units=m +no_defs',  # Oblique Mercator Azimuth Center
                  '+proj=omerc +lat_0=10 +lat_1=12 +lon_1=11 +lat_2=14 +lon_2=13 +k=0.9 +x_0=0 +y_0=0 +R=2439400 +units=m +no_defs',  # Oblique Mercator 2 points
                  '+proj=stere +lat_0=90 +lon_0=10 +k=0.9 +x_0=0 +y_0=0 +R=2439400 +units=m +no_defs',  # Polar Stereographic
                  '+proj=poly +lat_0=9 +lon_0=10 +x_0=0 +y_0=0 +R=2439400 +units=m +no_defs',
                  '+proj=sinu +lon_0=10 +x_0=0 +y_0=0 +R=2439400 +units=m +no_defs',
                  '+proj=tmerc +lat_0=11 +lon_0=10 +k=0.9 +x_0=0 +y_0=0 +R=2439400 +units=m +no_defs',
                 ]:
        ds = gdal.GetDriverByName('PDS4').Create(filename, 1, 1)
        sr = osr.SpatialReference()
        sr.ImportFromProj4(proj4)
        ds.SetProjection(sr.ExportToWkt())
        ds.SetGeoTransform([0, 1, 0, 0, 0, -1])
        with gdaltest.error_handler():
            ds = None

        ret = validate_xml(filename)
        assert ret, ('validation of file for %s failed' % proj4)

        ds = gdal.Open(filename)
        wkt = ds.GetProjectionRef()
        sr = osr.SpatialReference()
        sr.SetFromUserInput(wkt)
        got_proj4 = sr.ExportToProj4().strip()
        assert got_proj4 == proj4, ''

    # longlat doesn't roundtrip as such
    ds = gdal.GetDriverByName('PDS4').Create(filename, 1, 1)
    sr = osr.SpatialReference()
    sr.ImportFromProj4('+proj=longlat +R=2439400 +no_defs')
    ds.SetProjection(sr.ExportToWkt())
    ds.SetGeoTransform([2, 1, 0, 49, 0, -2])
    with gdaltest.error_handler():
        ds = None
    ds = gdal.Open(filename)
    wkt = ds.GetProjectionRef()
    sr = osr.SpatialReference()
    sr.SetFromUserInput(wkt)
    got_proj4 = sr.ExportToProj4().strip()
    proj4 = '+proj=eqc +lat_ts=0 +lat_0=0 +lon_0=0 +x_0=0 +y_0=0 +R=2439400 +units=m +no_defs'
    assert got_proj4 == proj4, ''

    got_gt = ds.GetGeoTransform()
    expected_gt = (85151.12354629935, 42575.561773149675, 0.0, 2086202.5268843342, 0.0, -85151.12354629935)
    assert max([abs(got_gt[i] - expected_gt[i]) for i in range(6)]) <= 1, ''
    ds = None

    gdal.GetDriverByName('PDS4').Delete(filename)
Example #29
0
def pds4_17():

    filename = '/vsimem/out.xml'

    with gdaltest.error_handler():
        gdal.GetDriverByName('PDS4').Create(filename, 1, 1, 1, options=['ARRAY_TYPE=Array_2D'])

    ret = validate_xml(filename)
    if ret == 'fail':
        gdaltest.post_reason('validation failed')
        return 'fail'

    f = gdal.VSIFOpenL(filename, 'rb')
    if f:
        data = gdal.VSIFReadL(1, 10000, f).decode('ascii')
        gdal.VSIFCloseL(f)
    if data.find('<Array_2D>') < 0 or data.find('<axes>2</axes>') < 0 or \
       data.find('<axis_name>Band</axis_name>') >= 0 or \
       data.find('<sequence_number>3</sequence_number>') >= 0:
        gdaltest.post_reason('fail')
        print(data)
        return 'fail'

    gdal.GetDriverByName('PDS4').Delete(filename)

    # Test multi-band creation with Array_2D
    with gdaltest.error_handler():
        ds = gdal.GetDriverByName('PDS4').Create(filename, 1, 1, 2, options=['ARRAY_TYPE=Array_2D'])
    if ds is not None:
        gdaltest.post_reason('expected failure')
        return 'fail'

    # Test multi-band creation with Array_3D_Spectrum
    with gdaltest.error_handler():
        gdal.GetDriverByName('PDS4').Create(filename, 1, 1, 2, options=['ARRAY_TYPE=Array_3D_Spectrum'])

    ret = validate_xml(filename)
    if ret == 'fail':
        gdaltest.post_reason('validation failed')
        return 'fail'

    f = gdal.VSIFOpenL(filename, 'rb')
    if f:
        data = gdal.VSIFReadL(1, 10000, f).decode('ascii')
        gdal.VSIFCloseL(f)
    if data.find('<Array_3D_Spectrum>') < 0 or data.find('<axes>3</axes>') < 0:
        gdaltest.post_reason('fail')
        print(data)
        return 'fail'

    gdal.GetDriverByName('PDS4').Delete(filename)

    return 'success'
Example #30
0
def vrtderived_code_that_only_makes_sense_with_GDAL_VRT_ENABLE_PYTHON_equal_IF_SAFE_but_that_is_now_disabled():

    # untrusted import
    ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10">
  <VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand">
    <PixelFunctionType>my_func</PixelFunctionType>
    <PixelFunctionLanguage>Python</PixelFunctionLanguage>
    <PixelFunctionCode><![CDATA[
def my_func(in_ar, out_ar, xoff, yoff, xsize, ysize, raster_xsize, raster_ysize, r, gt, **kwargs):
    import foo
]]>
    </PixelFunctionCode>
  </VRTRasterBand>
</VRTDataset>
""")
    with gdaltest.error_handler():
        cs = ds.GetRasterBand(1).Checksum()
    if cs != 0:
        print(gdal.GetLastErrorMsg())
        pytest.fail('invalid checksum')

    # untrusted function
    ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10">
  <VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand">
    <PixelFunctionType>my_func</PixelFunctionType>
    <PixelFunctionLanguage>Python</PixelFunctionLanguage>
    <PixelFunctionCode><![CDATA[
def my_func(in_ar, out_ar, xoff, yoff, xsize, ysize, raster_xsize, raster_ysize, r, gt, **kwargs):
    open('/etc/passwd').read()
]]>
    </PixelFunctionCode>
  </VRTRasterBand>
</VRTDataset>
""")
    with gdaltest.error_handler():
        cs = ds.GetRasterBand(1).Checksum()
    if cs != 0:
        print(gdal.GetLastErrorMsg())
        pytest.fail('invalid checksum')

    # GDAL_VRT_ENABLE_PYTHON not set to YES
    ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10">
  <VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand">
    <PixelFunctionType>vrtderived.one_pix_func</PixelFunctionType>
    <PixelFunctionLanguage>Python</PixelFunctionLanguage>
  </VRTRasterBand>
</VRTDataset>
""")
    with gdaltest.error_handler():
        cs = ds.GetRasterBand(1).Checksum()
    if cs != 0:
        print(gdal.GetLastErrorMsg())
        pytest.fail('invalid checksum')
Example #31
0
def bag_vr_resampled():

    if gdaltest.bag_drv is None:
        return 'skip'

    ds = gdal.OpenEx('data/test_vr.bag', open_options=['MODE=RESAMPLED_GRID'])
    if ds is None:
        gdaltest.post_reason('fail')
        return 'fail'

    got_md = gdal.Info(ds, computeChecksum=True, format='json')
    expected_md = {
        'bands': [{
            'band': 1,
            'block': [36, 24],
            'checksum': 4582,
            'colorInterpretation': 'Undefined',
            'description': 'elevation',
            'max': 10.0,
            'metadata': {},
            'min': -10.0,
            'noDataValue': 1000000.0,
            'type': 'Float32'
        }, {
            'band': 2,
            'block': [36, 24],
            'checksum': 6237,
            'colorInterpretation': 'Undefined',
            'description': 'uncertainty',
            'max': 10.0,
            'metadata': {},
            'min': 0.0,
            'noDataValue': 1000000.0,
            'type': 'Float32'
        }],
        'coordinateSystem': {
            'wkt':
            'PROJCS["NAD83 / UTM zone 10N",\n    GEOGCS["NAD83",\n        DATUM["North American Datum 1983",\n            SPHEROID["GRS 1980",6378137,298.2572221010041,\n                AUTHORITY["EPSG","7019"]],\n            TOWGS84[0,0,0,0,0,0,0],\n            AUTHORITY["EPSG","6269"]],\n        PRIMEM["Greenwich",0,\n            AUTHORITY["EPSG","8901"]],\n        UNIT["degree",0.0174532925199433,\n            AUTHORITY["EPSG","9122"]],\n        AUTHORITY["EPSG","4269"]],\n    PROJECTION["Transverse_Mercator",\n        AUTHORITY["EPSG","16010"]],\n    PARAMETER["latitude_of_origin",0],\n    PARAMETER["central_meridian",-123],\n    PARAMETER["scale_factor",0.9996],\n    PARAMETER["false_easting",500000],\n    PARAMETER["false_northing",0],\n    UNIT["metre",1,\n        AUTHORITY["EPSG","9001"]],\n    AUTHORITY["EPSG","26910"]]'
        },
        'geoTransform': [
            85.0, 4.983333110809326, 0.0, 500111.5999984741, 0.0,
            -5.316666603088379
        ],
        'metadata': {
            '': {
                'AREA_OR_POINT': 'Point',
                'BAG_DATETIME': '2018-08-08T12:34:56',
                'BagVersion': '1.6.2'
            },
            'IMAGE_STRUCTURE': {
                'INTERLEAVE': 'PIXEL'
            }
        },
        'size': [36, 24]
    }

    for key in expected_md:
        if key not in got_md or got_md[key] != expected_md[key]:
            gdaltest.post_reason('fail')
            print(key)
            import pprint
            pp = pprint.PrettyPrinter()
            pp.pprint(got_md)
            return 'fail'

    data_ref = ds.ReadRaster()

    # Test that block size has no influence on the result
    for block_size in (2, 5, 9, 16):
        with gdaltest.config_option('GDAL_BAG_BLOCK_SIZE', str(block_size)):
            ds = gdal.OpenEx('data/test_vr.bag',
                             open_options=['MODE=RESAMPLED_GRID'])
            if ds.GetRasterBand(1).GetBlockSize() != [block_size, block_size]:
                gdaltest.post_reason('fail')
                print(block_size)
                print(ds.GetRasterBand(1).GetBlockSize())
                return 'fail'
            data = ds.ReadRaster()

        if data != data_ref:
            gdaltest.post_reason('fail')
            print(block_size)
            return 'fail'

    # Test overviews
    with gdaltest.config_option('GDAL_BAG_MIN_OVR_SIZE', '4'):
        ds = gdal.OpenEx('data/test_vr.bag',
                         open_options=['MODE=RESAMPLED_GRID'])
    if ds.GetRasterBand(1).GetOverviewCount() != 2:
        gdaltest.post_reason('fail')
        print(ds.GetRasterBand(1).GetOverviewCount())
        return 'fail'
    if ds.GetRasterBand(1).GetOverview(-1) is not None:
        gdaltest.post_reason('fail')
        return 'fail'
    if ds.GetRasterBand(1).GetOverview(2) is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    ovr = ds.GetRasterBand(1).GetOverview(0)
    cs = ovr.Checksum()
    if cs != 681:
        gdaltest.post_reason('fail')
        print(cs)
        return 'fail'
    ovr = ds.GetRasterBand(2).GetOverview(0)
    cs = ovr.Checksum()
    if cs != 1344:
        gdaltest.post_reason('fail')
        print(cs)
        return 'fail'
    ds.GetRasterBand(1).GetOverview(0).FlushCache()
    ds.GetRasterBand(1).GetOverview(1).FlushCache()
    cs = ovr.Checksum()
    if cs != 1344:
        gdaltest.post_reason('fail')
        print(cs)
        return 'fail'
    ovr = ds.GetRasterBand(1).GetOverview(0)
    cs = ovr.Checksum()
    if cs != 681:
        gdaltest.post_reason('fail')
        print(cs)
        return 'fail'

    ds = gdal.OpenEx('data/test_vr.bag',
                     open_options=[
                         'MODE=RESAMPLED_GRID', 'MINX=90', 'MAXX=120',
                         'MAXY=500112'
                     ])
    gt = ds.GetGeoTransform()
    got = (gt[0], gt[3], ds.RasterXSize)
    if got != (90.0, 500112.0, 6):
        gdaltest.post_reason('fail')
        print(got)
        return 'fail'

    ds = gdal.OpenEx('data/test_vr.bag',
                     open_options=['MODE=RESAMPLED_GRID', 'MINY=500000'])
    gt = ds.GetGeoTransform()
    got = (gt[3] + gt[5] * ds.RasterYSize, ds.RasterYSize)
    if got != (500000.0, 21):
        gdaltest.post_reason('fail')
        print(got)
        return 'fail'

    ds = gdal.OpenEx('data/test_vr.bag',
                     open_options=['MODE=RESAMPLED_GRID', 'RESX=5', 'RESY=6'])
    gt = ds.GetGeoTransform()
    got = (gt[1], gt[5])
    if got != (5.0, -6.0):
        gdaltest.post_reason('fail')
        print(got)
        return 'fail'

    ds = gdal.OpenEx('data/test_vr.bag',
                     open_options=['MODE=RESAMPLED_GRID', 'RES_STRATEGY=MIN'])
    gt = ds.GetGeoTransform()
    got = (gt[1], gt[5])
    if got != (4.983333110809326, -5.316666603088379):
        gdaltest.post_reason('fail')
        print(got)
        return 'fail'

    ds = gdal.OpenEx('data/test_vr.bag',
                     open_options=['MODE=RESAMPLED_GRID', 'RES_STRATEGY=MAX'])
    gt = ds.GetGeoTransform()
    got = (gt[1], gt[5])
    if got != (29.899999618530273, -31.899999618530273):
        gdaltest.post_reason('fail')
        print(got)
        return 'fail'

    ds = gdal.OpenEx('data/test_vr.bag',
                     open_options=['MODE=RESAMPLED_GRID', 'RES_STRATEGY=MEAN'])
    gt = ds.GetGeoTransform()
    got = (gt[1], gt[5])
    if got != (12.209166447321573, -13.025833209355673):
        gdaltest.post_reason('fail')
        print(got)
        return 'fail'

    ds = gdal.OpenEx('data/test_vr.bag',
                     open_options=[
                         'MODE=RESAMPLED_GRID', 'RES_FILTER_MIN=0',
                         'RES_FILTER_MAX=8'
                     ])
    gt = ds.GetGeoTransform()
    got = (gt[1], gt[5])
    if got != (8.0, -8.0):
        gdaltest.post_reason('fail')
        print(got)
        return 'fail'
    got = (ds.GetRasterBand(1).Checksum(), ds.GetRasterBand(2).Checksum())
    if got != (2099, 2747):
        gdaltest.post_reason('fail')
        print(got)
        return 'fail'

    ds = gdal.OpenEx('data/test_vr.bag',
                     open_options=['MODE=RESAMPLED_GRID', 'RES_FILTER_MAX=8'])
    gt = ds.GetGeoTransform()
    got = (gt[1], gt[5])
    if got != (8.0, -8.0):
        gdaltest.post_reason('fail')
        print(got)
        return 'fail'
    got = (ds.GetRasterBand(1).Checksum(), ds.GetRasterBand(2).Checksum())
    if got != (2099, 2747):
        gdaltest.post_reason('fail')
        print(got)
        return 'fail'

    ds = gdal.OpenEx('data/test_vr.bag',
                     open_options=[
                         'MODE=RESAMPLED_GRID', 'RES_FILTER_MIN=8',
                         'RES_FILTER_MAX=16'
                     ])
    gt = ds.GetGeoTransform()
    got = (gt[1], gt[5])
    if got != (16.0, -16.0):
        gdaltest.post_reason('fail')
        print(got)
        return 'fail'
    got = (ds.GetRasterBand(1).Checksum(), ds.GetRasterBand(2).Checksum())
    if got != (796, 864):
        gdaltest.post_reason('fail')
        print(got)
        return 'fail'

    ds = gdal.OpenEx('data/test_vr.bag',
                     open_options=[
                         'MODE=RESAMPLED_GRID', 'RES_FILTER_MIN=16',
                         'RES_FILTER_MAX=32'
                     ])
    gt = ds.GetGeoTransform()
    got = (gt[1], gt[5])
    if got != (32.0, -32.0):
        gdaltest.post_reason('fail')
        print(got)
        return 'fail'
    got = (ds.GetRasterBand(1).Checksum(), ds.GetRasterBand(2).Checksum())
    if got != (207, 207):
        gdaltest.post_reason('fail')
        print(got)
        return 'fail'

    ds = gdal.OpenEx('data/test_vr.bag',
                     open_options=['MODE=RESAMPLED_GRID', 'RES_FILTER_MIN=16'])
    gt = ds.GetGeoTransform()
    got = (gt[1], gt[5])
    if got != (29.899999618530273, -31.899999618530273):
        gdaltest.post_reason('fail')
        print(got)
        return 'fail'
    got = (ds.GetRasterBand(1).Checksum(), ds.GetRasterBand(2).Checksum())
    if got != (165, 205):
        gdaltest.post_reason('fail')
        print(got)
        return 'fail'

    # Too big RES_FILTER_MIN
    with gdaltest.error_handler():
        ds = gdal.OpenEx(
            'data/test_vr.bag',
            open_options=['MODE=RESAMPLED_GRID', 'RES_FILTER_MIN=32'])
    if ds is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    # Too small RES_FILTER_MAX
    with gdaltest.error_handler():
        ds = gdal.OpenEx(
            'data/test_vr.bag',
            open_options=['MODE=RESAMPLED_GRID', 'RES_FILTER_MAX=4'])
    if ds is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    # RES_FILTER_MIN >= RES_FILTER_MAX
    with gdaltest.error_handler():
        ds = gdal.OpenEx('data/test_vr.bag',
                         open_options=[
                             'MODE=RESAMPLED_GRID', 'RES_FILTER_MIN=4',
                             'RES_FILTER_MAX=4'
                         ])
    if ds is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    # Test VALUE_POPULATION
    ds = gdal.OpenEx('data/test_vr.bag',
                     open_options=[
                         'MODE=RESAMPLED_GRID', 'RES_STRATEGY=MEAN',
                         'VALUE_POPULATION=MAX'
                     ])
    m1_max, M1_max, mean1_max, _ = ds.GetRasterBand(1).ComputeStatistics(False)
    m2_max, M2_max, mean2_max, _ = ds.GetRasterBand(2).ComputeStatistics(False)

    ds = gdal.OpenEx('data/test_vr.bag',
                     open_options=[
                         'MODE=RESAMPLED_GRID', 'RES_STRATEGY=MEAN',
                         'VALUE_POPULATION=MEAN'
                     ])
    m1_mean, M1_mean, mean1_mean, _ = ds.GetRasterBand(1).ComputeStatistics(
        False)
    m2_mean, M2_mean, mean2_mean, _ = ds.GetRasterBand(2).ComputeStatistics(
        False)

    ds = gdal.OpenEx('data/test_vr.bag',
                     open_options=[
                         'MODE=RESAMPLED_GRID', 'RES_STRATEGY=MEAN',
                         'VALUE_POPULATION=MIN'
                     ])
    m1_min, M1_min, mean1_min, _ = ds.GetRasterBand(1).ComputeStatistics(False)
    m2_min, M2_min, mean2_min, _ = ds.GetRasterBand(2).ComputeStatistics(False)

    if mean1_min >= mean1_mean or mean1_mean >= mean1_max:
        gdaltest.post_reason('fail')
        print(m1_max, M1_max, mean1_max)
        print(m2_max, M2_max, mean2_max)
        print(m1_mean, M1_mean, mean1_mean)
        print(m2_mean, M2_mean, mean2_mean)
        print(m1_min, M1_min, mean1_min)
        print(m2_min, M2_min, mean2_min)
        return 'fail'

    if m2_min >= m2_max or \
       (m2_mean, M2_mean, mean2_mean) != (m2_max, M2_max, mean2_max):
        gdaltest.post_reason('fail')
        print(m1_max, M1_max, mean1_max)
        print(m2_max, M2_max, mean2_max)
        print(m1_mean, M1_mean, mean1_mean)
        print(m2_mean, M2_mean, mean2_mean)
        print(m1_min, M1_min, mean1_min)
        print(m2_min, M2_min, mean2_min)
        return 'fail'

    return 'success'
Example #32
0
def test_ogr_gpx_1():
    if not gdaltest.have_gpx:
        pytest.skip()

    assert gdaltest.gpx_ds is not None

    lyr = gdaltest.gpx_ds.GetLayerByName('waypoints')

    expect = [2, None]

    with gdaltest.error_handler():
        tr = ogrtest.check_features_against_list(lyr, 'ele', expect)
    assert tr

    lyr.ResetReading()

    expect = ['waypoint name', None]

    tr = ogrtest.check_features_against_list(lyr, 'name', expect)
    assert tr

    lyr.ResetReading()

    expect = ['href', None]

    tr = ogrtest.check_features_against_list(lyr, 'link1_href', expect)
    assert tr

    lyr.ResetReading()

    expect = ['text', None]

    tr = ogrtest.check_features_against_list(lyr, 'link1_text', expect)
    assert tr

    lyr.ResetReading()

    expect = ['type', None]

    tr = ogrtest.check_features_against_list(lyr, 'link1_type', expect)
    assert tr

    lyr.ResetReading()

    expect = ['href2', None]

    tr = ogrtest.check_features_against_list(lyr, 'link2_href', expect)
    assert tr

    lyr.ResetReading()

    expect = ['text2', None]

    tr = ogrtest.check_features_against_list(lyr, 'link2_text', expect)
    assert tr

    lyr.ResetReading()

    expect = ['type2', None]

    tr = ogrtest.check_features_against_list(lyr, 'link2_type', expect)
    assert tr

    lyr.ResetReading()

    expect = ['2007/11/25 17:58:00+01', None]

    tr = ogrtest.check_features_against_list(lyr, 'time', expect)
    assert tr

    lyr.ResetReading()
    feat = lyr.GetNextFeature()
    assert (ogrtest.check_feature_geometry(feat,
                                           'POINT (1 0)',
                                           max_error=0.0001) == 0)

    feat = lyr.GetNextFeature()
    assert (ogrtest.check_feature_geometry(feat,
                                           'POINT (4 3)',
                                           max_error=0.0001) == 0)
Example #33
0
def jpeg_28():

    tmpfilename = '/vsimem/jpeg_28.jpg'

    # Nothing
    src_ds = gdal.GetDriverByName('MEM').Create('', 1, 1)
    ds = gdal.GetDriverByName('JPEG').CreateCopy(tmpfilename, src_ds)
    src_ds = None
    ds = gdal.Open(tmpfilename)
    if ds.GetMetadata():
        gdaltest.post_reason('fail')
        return 'fail'

    # EXIF tags only
    src_ds = gdal.GetDriverByName('MEM').Create('', 1, 1)

    src_ds.SetMetadataItem('EXIF_DateTime', 'dt')  # not enough values ASCII
    src_ds.SetMetadataItem('EXIF_DateTimeOriginal',
                           '01234567890123456789')  # truncated ASCII
    src_ds.SetMetadataItem(
        'EXIF_DateTimeDigitized',
        '0123456789012345678')  # right number of items ASCII
    src_ds.SetMetadataItem('EXIF_Make', 'make')  # variable ASCII

    src_ds.SetMetadataItem('EXIF_ExifVersion', '01234')  # truncated UNDEFINED
    src_ds.SetMetadataItem('EXIF_ComponentsConfiguration',
                           '0x1F')  # not enough values UNDEFINED
    src_ds.SetMetadataItem('EXIF_FlashpixVersion',
                           'ABCD')  # right number of items UNDEFINED
    src_ds.SetMetadataItem('EXIF_SpatialFrequencyResponse',
                           '0xab 0xCD')  # variable UNDEFINED

    src_ds.SetMetadataItem('EXIF_Orientation',
                           '10')  # right number of items SHORT
    src_ds.SetMetadataItem('EXIF_ResolutionUnit', '2 4')  # truncated SHORT
    src_ds.SetMetadataItem('EXIF_TransferFunction',
                           '0 1')  # not enough values SHORT
    src_ds.SetMetadataItem('EXIF_ISOSpeedRatings', '1 2 3')  # variable SHORT

    src_ds.SetMetadataItem('EXIF_StandardOutputSensitivity',
                           '123456789')  # right number of items LONG

    src_ds.SetMetadataItem('EXIF_XResolution',
                           '96')  # right number of items RATIONAL
    src_ds.SetMetadataItem('EXIF_YResolution', '96 0')  # truncated RATIONAL
    src_ds.SetMetadataItem('EXIF_CompressedBitsPerPixel',
                           'nan')  # invalid RATIONAL
    src_ds.SetMetadataItem('EXIF_ApertureValue', '-1')  # invalid RATIONAL

    with gdaltest.error_handler():
        gdal.GetDriverByName('JPEG').CreateCopy(tmpfilename, src_ds)
    src_ds = None
    if gdal.VSIStatL(tmpfilename + '.aux.xml') is not None:
        gdaltest.post_reason('fail')
        return 'fail'
    ds = gdal.Open(tmpfilename)
    got_md = ds.GetMetadata()
    expected_md = {
        'EXIF_DateTimeDigitized': '0123456789012345678',
        'EXIF_DateTimeOriginal': '0123456789012345678',
        'EXIF_Orientation': '10',
        'EXIF_ApertureValue': '(0)',
        'EXIF_YResolution': '(96)',
        'EXIF_XResolution': '(96)',
        'EXIF_TransferFunction':
        '0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0',
        'EXIF_ExifVersion': '0123',
        'EXIF_DateTime': 'dt                 ',
        'EXIF_FlashpixVersion': 'ABCD',
        'EXIF_ComponentsConfiguration': '0x1f 0x00 0x00 0x00',
        'EXIF_Make': 'make',
        'EXIF_StandardOutputSensitivity': '123456789',
        'EXIF_ResolutionUnit': '2',
        'EXIF_CompressedBitsPerPixel': '(0)',
        'EXIF_SpatialFrequencyResponse': '0xab 0xcd',
        'EXIF_ISOSpeedRatings': '1 2 3'
    }
    if got_md != expected_md:
        gdaltest.post_reason('fail')
        print(got_md)
        return 'fail'

    # Test SRATIONAL
    for val in (-1.5, -1, -0.5, 0, 0.5, 1, 1.5):
        src_ds = gdal.GetDriverByName('MEM').Create('', 1, 1)
        src_ds.SetMetadataItem('EXIF_ShutterSpeedValue', str(val))
        gdal.GetDriverByName('JPEG').CreateCopy(tmpfilename, src_ds)
        src_ds = None
        if gdal.VSIStatL(tmpfilename + '.aux.xml') is not None:
            gdaltest.post_reason('fail')
            return 'fail'
        ds = gdal.Open(tmpfilename)
        got_val = ds.GetMetadataItem('EXIF_ShutterSpeedValue')
        got_val = got_val.replace('(', '').replace(')', '')
        if float(got_val) != val:
            gdaltest.post_reason('fail')
            print(val)
            print(ds.GetMetadataItem('EXIF_ShutterSpeedValue'))
            return 'fail'

    # Test RATIONAL
    for val in (0, 0.5, 1, 1.5):
        src_ds = gdal.GetDriverByName('MEM').Create('', 1, 1)
        src_ds.SetMetadataItem('EXIF_ApertureValue', str(val))
        gdal.GetDriverByName('JPEG').CreateCopy(tmpfilename, src_ds)
        src_ds = None
        if gdal.VSIStatL(tmpfilename + '.aux.xml') is not None:
            gdaltest.post_reason('fail')
            return 'fail'
        ds = gdal.Open(tmpfilename)
        got_val = ds.GetMetadataItem('EXIF_ApertureValue')
        got_val = got_val.replace('(', '').replace(')', '')
        if float(got_val) != val:
            gdaltest.post_reason('fail')
            print(val)
            print(ds.GetMetadataItem('EXIF_ApertureValue'))
            return 'fail'

    # GPS tags only
    src_ds = gdal.GetDriverByName('MEM').Create('', 1, 1)
    src_ds.SetMetadataItem('EXIF_GPSLatitudeRef', 'N')
    src_ds.SetMetadataItem('EXIF_GPSLatitude', '49 34 56.5')
    gdal.GetDriverByName('JPEG').CreateCopy(tmpfilename, src_ds)
    src_ds = None
    if gdal.VSIStatL(tmpfilename + '.aux.xml') is not None:
        gdaltest.post_reason('fail')
        return 'fail'
    ds = gdal.Open(tmpfilename)
    got_md = ds.GetMetadata()
    if got_md != {
            'EXIF_GPSLatitudeRef': 'N',
            'EXIF_GPSLatitude': '(49) (34) (56.5)'
    }:
        gdaltest.post_reason('fail')
        print(got_md)
        return 'fail'
    ds = None

    # EXIF and GPS tags
    src_ds = gdal.GetDriverByName('MEM').Create('', 1, 1)
    src_ds.SetMetadataItem('EXIF_ExifVersion', '0231')
    src_ds.SetMetadataItem('EXIF_GPSLatitudeRef', 'N')
    gdal.GetDriverByName('JPEG').CreateCopy(tmpfilename, src_ds)
    src_ds = None
    if gdal.VSIStatL(tmpfilename + '.aux.xml') is not None:
        gdaltest.post_reason('fail')
        return 'fail'
    ds = gdal.Open(tmpfilename)
    got_md = ds.GetMetadata()
    if got_md != {'EXIF_ExifVersion': '0231', 'EXIF_GPSLatitudeRef': 'N'}:
        gdaltest.post_reason('fail')
        print(got_md)
        return 'fail'
    ds = None

    # EXIF and other metadata
    src_ds = gdal.GetDriverByName('MEM').Create('', 1, 1)
    src_ds.SetMetadataItem('EXIF_ExifVersion', '0231')
    src_ds.SetMetadataItem('EXIF_invalid', 'foo')
    src_ds.SetMetadataItem('FOO', 'BAR')
    with gdaltest.error_handler():
        gdal.GetDriverByName('JPEG').CreateCopy(tmpfilename, src_ds)
    src_ds = None
    if gdal.VSIStatL(tmpfilename + '.aux.xml') is None:
        gdaltest.post_reason('fail')
        return 'fail'
    ds = gdal.Open(tmpfilename)
    got_md = ds.GetMetadata()
    if got_md != {
            'EXIF_ExifVersion': '0231',
            'EXIF_invalid': 'foo',
            'FOO': 'BAR'
    }:
        gdaltest.post_reason('fail')
        print(got_md)
        return 'fail'
    ds = None

    # Too much content for EXIF
    src_ds = gdal.GetDriverByName('MEM').Create('', 1, 1)
    src_ds.SetMetadataItem('EXIF_UserComment', 'x' * 65535)
    with gdaltest.error_handler():
        gdal.GetDriverByName('JPEG').CreateCopy(tmpfilename, src_ds)
    src_ds = None
    ds = None

    # EXIF and GPS tags and EXIF overview
    src_ds = gdal.GetDriverByName('MEM').Create('', 1024, 1024)
    src_ds.SetMetadataItem('EXIF_ExifVersion', '0231')
    src_ds.SetMetadataItem('EXIF_GPSLatitudeRef', 'N')
    gdal.GetDriverByName('JPEG').CreateCopy(tmpfilename,
                                            src_ds,
                                            options=[
                                                'EXIF_THUMBNAIL=YES',
                                                'THUMBNAIL_WIDTH=32',
                                                'THUMBNAIL_HEIGHT=32'
                                            ])
    src_ds = None
    if gdal.VSIStatL(tmpfilename + '.aux.xml') is not None:
        gdaltest.post_reason('fail')
        return 'fail'
    ds = gdal.Open(tmpfilename)
    got_md = ds.GetMetadata()
    if got_md != {'EXIF_ExifVersion': '0231', 'EXIF_GPSLatitudeRef': 'N'}:
        gdaltest.post_reason('fail')
        print(got_md)
        return 'fail'
    if ds.GetRasterBand(1).GetOverview(
            ds.GetRasterBand(1).GetOverviewCount() - 1).XSize != 32:
        gdaltest.post_reason('fail')
        return 'fail'
    ds = None

    gdal.Unlink(tmpfilename)

    return 'success'
Example #34
0
def vsis3_extra_1():
    try:
        drv = gdal.GetDriverByName( 'HTTP' )
    except:
        drv = None

    if drv is None:
        return 'skip'

    if gdal.GetConfigOption('AWS_SECRET_ACCESS_KEY') is None:
        print('Missing AWS_SECRET_ACCESS_KEY for running gdaltest_list_extra')
        return 'skip'
    elif gdal.GetConfigOption('AWS_ACCESS_KEY_ID') is None:
        print('Missing AWS_ACCESS_KEY_ID for running gdaltest_list_extra')
        return 'skip'
    elif gdal.GetConfigOption('S3_RESOURCE') is None:
        print('Missing S3_RESOURCE for running gdaltest_list_extra')
        return 'skip'

    f = open_for_read('/vsis3/' + gdal.GetConfigOption('S3_RESOURCE'))
    if f is None:
        gdaltest.post_reason('fail')
        return 'fail'
    ret = gdal.VSIFReadL(1, 1, f)
    gdal.VSIFCloseL(f)

    if len(ret) != 1:
        gdaltest.post_reason('fail')
        print(ret)
        return 'fail'

    # Same with /vsis3_streaming/
    f = open_for_read('/vsis3_streaming/' + gdal.GetConfigOption('S3_RESOURCE'))
    if f is None:
        gdaltest.post_reason('fail')
        return 'fail'
    ret = gdal.VSIFReadL(1, 1, f)
    gdal.VSIFCloseL(f)

    if len(ret) != 1:
        gdaltest.post_reason('fail')
        print(ret)
        return 'fail'

    # Invalid bucket : "The specified bucket does not exist"
    gdal.ErrorReset()
    f = open_for_read('/vsis3/not_existing_bucket/foo')
    with gdaltest.error_handler():
        gdal.VSIFReadL(1, 1, f)
    gdal.VSIFCloseL(f)
    if gdal.VSIGetLastErrorMsg() == '':
        gdaltest.post_reason('fail')
        print(gdal.VSIGetLastErrorMsg())
        return 'fail'

    # Invalid resource
    gdal.ErrorReset()
    f = open_for_read('/vsis3_streaming/' + gdal.GetConfigOption('S3_RESOURCE') + '/invalid_resource.baz')
    if f is not None:
        gdaltest.post_reason('fail')
        print(gdal.VSIGetLastErrorMsg())
        return 'fail'

    return 'success'
Example #35
0
def vsis3_4():

    if gdaltest.webserver_port == 0:
        return 'skip'

    with gdaltest.error_handler():
        f = gdal.VSIFOpenL('/vsis3/s3_fake_bucket3', 'wb')
    if f is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    if gdal.VSIStatL('/vsis3/s3_fake_bucket3/empty_file.bin').size != 3:
        gdaltest.post_reason('fail')
        return 'fail'

    # Empty file
    f = gdal.VSIFOpenL('/vsis3/s3_fake_bucket3/empty_file.bin', 'wb')
    if f is None:
        gdaltest.post_reason('fail')
        return 'fail'
    gdal.ErrorReset()
    gdal.VSIFCloseL(f)
    if gdal.GetLastErrorMsg() != '':
        gdaltest.post_reason('fail')
        return 'fail'

    if gdal.VSIStatL('/vsis3/s3_fake_bucket3/empty_file.bin').size != 0:
        gdaltest.post_reason('fail')
        return 'fail'

    # Invalid seek
    f = gdal.VSIFOpenL('/vsis3/s3_fake_bucket3/empty_file.bin', 'wb')
    if f is None:
        gdaltest.post_reason('fail')
        return 'fail'
    with gdaltest.error_handler():
        ret = gdal.VSIFSeekL(f, 1, 0)
    if ret == 0:
        gdaltest.post_reason('fail')
        return 'fail'
    gdal.VSIFCloseL(f)

    # Invalid read
    f = gdal.VSIFOpenL('/vsis3/s3_fake_bucket3/empty_file.bin', 'wb')
    if f is None:
        gdaltest.post_reason('fail')
        return 'fail'
    with gdaltest.error_handler():
        ret = gdal.VSIFReadL(1, 1, f)
    if len(ret) != 0:
        gdaltest.post_reason('fail')
        return 'fail'
    gdal.VSIFCloseL(f)

    # Error case
    f = gdal.VSIFOpenL('/vsis3/s3_fake_bucket3/empty_file_error.bin', 'wb')
    if f is None:
        gdaltest.post_reason('fail')
        return 'fail'
    gdal.ErrorReset()
    with gdaltest.error_handler():
        gdal.VSIFCloseL(f)
    if gdal.GetLastErrorMsg() == '':
        gdaltest.post_reason('fail')
        return 'fail'

    # Nominal case
    f = gdal.VSIFOpenL('/vsis3/s3_fake_bucket3/another_file.bin', 'wb')
    if f is None:
        gdaltest.post_reason('fail')
        return 'fail'
    if gdal.VSIFSeekL(f, gdal.VSIFTellL(f), 0) != 0:
        gdaltest.post_reason('fail')
        return 'fail'
    if gdal.VSIFSeekL(f, 0, 1) != 0:
        gdaltest.post_reason('fail')
        return 'fail'
    if gdal.VSIFSeekL(f, 0, 2) != 0:
        gdaltest.post_reason('fail')
        return 'fail'
    if gdal.VSIFWriteL('foo', 1, 3, f) != 3:
        gdaltest.post_reason('fail')
        return 'fail'
    if gdal.VSIFWriteL('bar', 1, 3, f) != 3:
        gdaltest.post_reason('fail')
        return 'fail'
    gdal.ErrorReset()
    gdal.VSIFCloseL(f)
    if gdal.GetLastErrorMsg() != '':
        gdaltest.post_reason('fail')
        return 'fail'

    # Redirect case
    f = gdal.VSIFOpenL('/vsis3/s3_fake_bucket3/redirect', 'wb')
    if f is None:
        gdaltest.post_reason('fail')
        return 'fail'
    if gdal.VSIFWriteL('foobar', 1, 6, f) != 6:
        gdaltest.post_reason('fail')
        return 'fail'
    gdal.ErrorReset()
    gdal.VSIFCloseL(f)
    if gdal.GetLastErrorMsg() != '':
        gdaltest.post_reason('fail')
        return 'fail'

    return 'success'
Example #36
0
File: vsiaz.py Project: zyhgis/gdal
def test_vsiaz_extra_1():

    if not gdaltest.built_against_curl():
        pytest.skip()

    az_resource = gdal.GetConfigOption('AZ_RESOURCE')
    if az_resource is None:
        pytest.skip('Missing AZ_RESOURCE')

    if '/' not in az_resource:
        path = '/vsiaz/' + az_resource
        statres = gdal.VSIStatL(path)
        assert statres is not None and stat.S_ISDIR(statres.mode), \
            ('%s is not a valid bucket' % path)

        readdir = gdal.ReadDir(path)
        assert readdir is not None, 'ReadDir() should not return empty list'
        for filename in readdir:
            if filename != '.':
                subpath = path + '/' + filename
                assert gdal.VSIStatL(subpath) is not None, \
                    ('Stat(%s) should not return an error' % subpath)

        unique_id = 'vsiaz_test'
        subpath = path + '/' + unique_id
        ret = gdal.Mkdir(subpath, 0)
        assert ret >= 0, ('Mkdir(%s) should not return an error' % subpath)

        readdir = gdal.ReadDir(path)
        assert unique_id in readdir, \
            ('ReadDir(%s) should contain %s' % (path, unique_id))

        ret = gdal.Mkdir(subpath, 0)
        assert ret != 0, ('Mkdir(%s) repeated should return an error' %
                          subpath)

        ret = gdal.Rmdir(subpath)
        assert ret >= 0, ('Rmdir(%s) should not return an error' % subpath)

        readdir = gdal.ReadDir(path)
        assert unique_id not in readdir, \
            ('ReadDir(%s) should not contain %s' % (path, unique_id))

        ret = gdal.Rmdir(subpath)
        assert ret != 0, ('Rmdir(%s) repeated should return an error' %
                          subpath)

        ret = gdal.Mkdir(subpath, 0)
        assert ret >= 0, ('Mkdir(%s) should not return an error' % subpath)

        f = gdal.VSIFOpenL(subpath + '/test.txt', 'wb')
        assert f is not None
        gdal.VSIFWriteL('hello', 1, 5, f)
        gdal.VSIFCloseL(f)

        ret = gdal.Rmdir(subpath)
        assert ret != 0, \
            ('Rmdir(%s) on non empty directory should return an error' % subpath)

        f = gdal.VSIFOpenL(subpath + '/test.txt', 'rb')
        assert f is not None
        data = gdal.VSIFReadL(1, 5, f).decode('utf-8')
        assert data == 'hello'
        gdal.VSIFCloseL(f)

        assert gdal.Rename(subpath + '/test.txt', subpath + '/test2.txt') == 0

        f = gdal.VSIFOpenL(subpath + '/test2.txt', 'rb')
        assert f is not None
        data = gdal.VSIFReadL(1, 5, f).decode('utf-8')
        assert data == 'hello'
        gdal.VSIFCloseL(f)

        ret = gdal.Unlink(subpath + '/test2.txt')
        assert ret >= 0, \
            ('Unlink(%s) should not return an error' % (subpath + '/test2.txt'))

        ret = gdal.Rmdir(subpath)
        assert ret >= 0, ('Rmdir(%s) should not return an error' % subpath)

        return

    f = open_for_read('/vsiaz/' + az_resource)
    assert f is not None
    ret = gdal.VSIFReadL(1, 1, f)
    gdal.VSIFCloseL(f)

    assert len(ret) == 1

    # Same with /vsiaz_streaming/
    f = open_for_read('/vsiaz_streaming/' + az_resource)
    assert f is not None
    ret = gdal.VSIFReadL(1, 1, f)
    gdal.VSIFCloseL(f)

    assert len(ret) == 1

    if False:  # pylint: disable=using-constant-test
        # we actually try to read at read() time and bSetError = false
        # Invalid bucket : "The specified bucket does not exist"
        gdal.ErrorReset()
        f = open_for_read('/vsiaz/not_existing_bucket/foo')
        with gdaltest.error_handler():
            gdal.VSIFReadL(1, 1, f)
        gdal.VSIFCloseL(f)
        assert gdal.VSIGetLastErrorMsg() != ''

    # Invalid resource
    gdal.ErrorReset()
    f = open_for_read('/vsiaz_streaming/' + az_resource +
                      '/invalid_resource.baz')
    assert f is None, gdal.VSIGetLastErrorMsg()

    # Test GetSignedURL()
    signed_url = gdal.GetSignedURL('/vsiaz/' + az_resource)
    f = open_for_read('/vsicurl_streaming/' + signed_url)
    assert f is not None
    ret = gdal.VSIFReadL(1, 1, f)
    gdal.VSIFCloseL(f)

    assert len(ret) == 1
Example #37
0
def test_ogr_wkt_multipolygon_corrupted():

    with gdaltest.error_handler():
        g = ogr.CreateGeometryFromWkt('MULTIPOLYGON(POLYGON((N')
    assert g is None
Example #38
0
File: rmf.py Project: tingli3/gdal
def rmf_10():

    tst = gdaltest.GDALTest('rmf', 't100.mtw', 1, 6388)

    with gdaltest.error_handler():
        return tst.testOpen()
Example #39
0
def test_cog_creation_options():

    filename = '/vsimem/cog.tif'
    src_ds = gdal.Open('data/byte.tif')
    ds = gdal.GetDriverByName('COG').CreateCopy(
        filename,
        src_ds,
        options=['COMPRESS=DEFLATE', 'LEVEL=1', 'NUM_THREADS=2'])
    assert ds
    ds = None
    ds = gdal.Open(filename)
    assert ds.GetRasterBand(1).Checksum() == 4672
    assert ds.GetMetadataItem('COMPRESSION', 'IMAGE_STRUCTURE') == 'DEFLATE'
    assert ds.GetMetadataItem('PREDICTOR', 'IMAGE_STRUCTURE') is None
    ds = None
    filesize = gdal.VSIStatL(filename).size
    _check_cog(filename)

    gdal.GetDriverByName('COG').CreateCopy(
        filename,
        src_ds,
        options=['COMPRESS=DEFLATE', 'BIGTIFF=YES', 'LEVEL=1'])
    assert gdal.VSIStatL(filename).size != filesize

    gdal.GetDriverByName('COG').CreateCopy(
        filename,
        src_ds,
        options=['COMPRESS=DEFLATE', 'PREDICTOR=YES', 'LEVEL=1'])
    assert gdal.VSIStatL(filename).size != filesize
    ds = gdal.Open(filename)
    assert ds.GetMetadataItem('PREDICTOR', 'IMAGE_STRUCTURE') == '2'
    ds = None

    gdal.GetDriverByName('COG').CreateCopy(
        filename, src_ds, options=['COMPRESS=DEFLATE', 'LEVEL=9'])
    assert gdal.VSIStatL(filename).size < filesize

    colist = gdal.GetDriverByName('COG').GetMetadataItem(
        'DMD_CREATIONOPTIONLIST')
    if '<Value>ZSTD' in colist:

        gdal.GetDriverByName('COG').CreateCopy(filename,
                                               src_ds,
                                               options=['COMPRESS=ZSTD'])
        ds = gdal.Open(filename)
        assert ds.GetMetadataItem('COMPRESSION', 'IMAGE_STRUCTURE') == 'ZSTD'
        ds = None

    if '<Value>LZMA' in colist:

        gdal.GetDriverByName('COG').CreateCopy(filename,
                                               src_ds,
                                               options=['COMPRESS=LZMA'])
        ds = gdal.Open(filename)
        assert ds.GetMetadataItem('COMPRESSION', 'IMAGE_STRUCTURE') == 'LZMA'
        ds = None

    if '<Value>WEBP' in colist:

        with gdaltest.error_handler():
            assert not gdal.GetDriverByName('COG').CreateCopy(
                filename, src_ds, options=['COMPRESS=WEBP'])

    if '<Value>LERC' in colist:

        assert gdal.GetDriverByName('COG').CreateCopy(
            filename, src_ds, options=['COMPRESS=LERC'])
        filesize_no_z_error = gdal.VSIStatL(filename).size
        assert gdal.VSIStatL(filename).size != filesize

        assert gdal.GetDriverByName('COG').CreateCopy(
            filename, src_ds, options=['COMPRESS=LERC', 'MAX_Z_ERROR=10'])
        filesize_with_z_error = gdal.VSIStatL(filename).size
        assert filesize_with_z_error < filesize_no_z_error

        assert gdal.GetDriverByName('COG').CreateCopy(
            filename, src_ds, options=['COMPRESS=LERC_DEFLATE'])
        filesize_lerc_deflate = gdal.VSIStatL(filename).size
        assert filesize_lerc_deflate < filesize_no_z_error

        assert gdal.GetDriverByName('COG').CreateCopy(
            filename, src_ds, options=['COMPRESS=LERC_DEFLATE', 'LEVEL=1'])
        filesize_lerc_deflate_level_1 = gdal.VSIStatL(filename).size
        assert filesize_lerc_deflate_level_1 > filesize_lerc_deflate

        if '<Value>ZSTD' in colist:
            assert gdal.GetDriverByName('COG').CreateCopy(
                filename, src_ds, options=['COMPRESS=LERC_ZSTD'])
            filesize_lerc_zstd = gdal.VSIStatL(filename).size
            assert filesize_lerc_zstd < filesize_no_z_error

            assert gdal.GetDriverByName('COG').CreateCopy(
                filename, src_ds, options=['COMPRESS=LERC_ZSTD', 'LEVEL=1'])
            filesize_lerc_zstd_level_1 = gdal.VSIStatL(filename).size
            assert filesize_lerc_zstd_level_1 > filesize_lerc_zstd

    src_ds = None
    gdal.GetDriverByName('GTiff').Delete(filename)
Example #40
0
def test_vrtderived_9():

    try:
        import numpy
        numpy.ones
    except (ImportError, AttributeError):
        pytest.skip()

    # Missing PixelFunctionType
    with gdaltest.error_handler():
        ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10">
  <VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand">
    <PixelFunctionLanguage>Python</PixelFunctionLanguage>
  </VRTRasterBand>
</VRTDataset>
""")
    assert ds is None

    # Unsupported PixelFunctionLanguage
    with gdaltest.error_handler():
        ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10">
  <VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand">
    <PixelFunctionType>identity</PixelFunctionType>
    <PixelFunctionLanguage>foo</PixelFunctionLanguage>
  </VRTRasterBand>
</VRTDataset>
""")
    assert ds is None

    # PixelFunctionCode can only be used with Python
    with gdaltest.error_handler():
        ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10">
  <VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand">
    <PixelFunctionType>identity</PixelFunctionType>
    <PixelFunctionCode><![CDATA[
def identity(in_ar, out_ar, xoff, yoff, xsize, ysize, raster_xsize, raster_ysize, r, gt, **kwargs):
    syntax_error
]]>
     </PixelFunctionCode>
  </VRTRasterBand>
</VRTDataset>
""")
    assert ds is None

    # BufferRadius can only be used with Python
    with gdaltest.error_handler():
        ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10">
  <VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand">
    <PixelFunctionType>identity</PixelFunctionType>
    <BufferRadius>1</BufferRadius>
  </VRTRasterBand>
</VRTDataset>
""")
    assert ds is None

    # Invalid BufferRadius
    with gdaltest.error_handler():
        ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10">
  <VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand">
    <PixelFunctionType>identity</PixelFunctionType>
    <PixelFunctionLanguage>Python</PixelFunctionLanguage>
    <BufferRadius>-1</BufferRadius>
  </VRTRasterBand>
</VRTDataset>
""")
    assert ds is None

    # Error at Python code compilation (indentation error)
    ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10">
  <VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand">
    <PixelFunctionType>identity</PixelFunctionType>
    <PixelFunctionLanguage>Python</PixelFunctionLanguage>
    <PixelFunctionCode><![CDATA[
def identity(in_ar, out_ar, xoff, yoff, xsize, ysize, raster_xsize, raster_ysize, r, gt, **kwargs):
syntax_error
]]>
    </PixelFunctionCode>
  </VRTRasterBand>
</VRTDataset>
""")
    gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', 'YES')
    with gdaltest.error_handler():
        cs = ds.GetRasterBand(1).Checksum()
    gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', None)
    if cs != 0:
        print(gdal.GetLastErrorMsg())
        pytest.fail('invalid checksum')

    # Error at run time (in global code)
    ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10">
  <VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand">
    <PixelFunctionType>identity</PixelFunctionType>
    <PixelFunctionLanguage>Python</PixelFunctionLanguage>
    <PixelFunctionCode><![CDATA[
runtime_error
def identity(in_ar, out_ar, xoff, yoff, xsize, ysize, raster_xsize, raster_ysize, r, gt, **kwargs):
    pass
]]>
    </PixelFunctionCode>
  </VRTRasterBand>
</VRTDataset>
""")
    gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', 'YES')
    with gdaltest.error_handler():
        cs = ds.GetRasterBand(1).Checksum()
    gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', None)
    if cs != 0:
        print(gdal.GetLastErrorMsg())
        pytest.fail('invalid checksum')

    # Error at run time (in pixel function)
    ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10">
  <VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand">
    <PixelFunctionType>identity</PixelFunctionType>
    <PixelFunctionLanguage>Python</PixelFunctionLanguage>
    <PixelFunctionCode><![CDATA[
def identity(in_ar, out_ar, xoff, yoff, xsize, ysize, raster_xsize, raster_ysize, r, gt, **kwargs):
    runtime_error
]]>
    </PixelFunctionCode>
  </VRTRasterBand>
</VRTDataset>
""")
    gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', 'YES')
    with gdaltest.error_handler():
        cs = ds.GetRasterBand(1).Checksum()
    gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', None)
    if cs != 0:
        print(gdal.GetLastErrorMsg())
        pytest.fail('invalid checksum')

    # User exception
    ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10">
  <VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand">
    <PixelFunctionType>identity</PixelFunctionType>
    <PixelFunctionLanguage>Python</PixelFunctionLanguage>
    <PixelFunctionCode><![CDATA[
def identity(in_ar, out_ar, xoff, yoff, xsize, ysize, raster_xsize, raster_ysize, r, gt, **kwargs):
    raise Exception('my exception')
]]>
    </PixelFunctionCode>
  </VRTRasterBand>
</VRTDataset>
""")
    gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', 'YES')
    with gdaltest.error_handler():
        cs = ds.GetRasterBand(1).Checksum()
    gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', None)
    if cs != 0:
        print(gdal.GetLastErrorMsg())
        pytest.fail('invalid checksum')

    # unknown_function
    ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10">
  <VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand">
    <PixelFunctionType>unknown_function</PixelFunctionType>
    <PixelFunctionLanguage>Python</PixelFunctionLanguage>
    <PixelFunctionCode><![CDATA[
def identity(in_ar, out_ar, xoff, yoff, xsize, ysize, raster_xsize, raster_ysize, r, gt, **kwargs):
    pass
]]>
    </PixelFunctionCode>
  </VRTRasterBand>
</VRTDataset>
""")
    gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', 'YES')
    with gdaltest.error_handler():
        cs = ds.GetRasterBand(1).Checksum()
    gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', None)
    if cs != 0:
        print(gdal.GetLastErrorMsg())
        pytest.fail('invalid checksum')

    # uncallable object
    ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10">
  <VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand">
    <PixelFunctionType>uncallable_object</PixelFunctionType>
    <PixelFunctionLanguage>Python</PixelFunctionLanguage>
    <PixelFunctionCode><![CDATA[
uncallable_object = True
]]>
    </PixelFunctionCode>
  </VRTRasterBand>
</VRTDataset>
""")
    gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', 'YES')
    with gdaltest.error_handler():
        cs = ds.GetRasterBand(1).Checksum()
    gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', None)
    if cs != 0:
        print(gdal.GetLastErrorMsg())
        pytest.fail('invalid checksum')

    # unknown_module
    ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10">
  <VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand">
    <PixelFunctionType>unknown_module.unknown_function</PixelFunctionType>
    <PixelFunctionLanguage>Python</PixelFunctionLanguage>
  </VRTRasterBand>
</VRTDataset>
""")
    with gdaltest.error_handler():
        gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', "YES")
        cs = ds.GetRasterBand(1).Checksum()
        gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', None)
    if cs != 0:
        print(gdal.GetLastErrorMsg())
        pytest.fail('invalid checksum')
Example #41
0
def vsicrypt_2():

    if not gdaltest.has_vsicrypt:
        return 'skip'

    # Missing key
    with gdaltest.error_handler():
        fp = gdal.VSIFOpenL('/vsicrypt//vsimem/file.bin', 'wb+')
    if fp is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    # Invalid file
    with gdaltest.error_handler():
        fp = gdal.VSIFOpenL(
            '/vsicrypt/key=DONT_USE_IN_PROD,file=/not_existing/not_existing',
            'wb')
    if fp is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    # Invalid file
    with gdaltest.error_handler():
        fp = gdal.VSIFOpenL(
            '/vsicrypt/key=DONT_USE_IN_PROD,file=/not_existing/not_existing',
            'rb')
    if fp is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    # Invalid file
    with gdaltest.error_handler():
        fp = gdal.VSIFOpenL(
            '/vsicrypt/key=DONT_USE_IN_PROD,file=/not_existing/not_existing',
            'ab')
    if fp is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    # Invalid access
    with gdaltest.error_handler():
        fp = gdal.VSIFOpenL(
            '/vsicrypt/key=DONT_USE_IN_PROD,file=/not_existing/not_existing',
            'foo')
    if fp is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    # Key to short
    with gdaltest.error_handler():
        fp = gdal.VSIFOpenL('/vsicrypt/key=a,file=/vsimem/file.bin', 'wb+')
    if fp is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    # Invalid signature
    gdal.FileFromMemBuffer('/vsimem/file.bin', 'foo')
    with gdaltest.error_handler():
        fp = gdal.VSIFOpenL(
            '/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin', 'rb')
    if fp is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    # Generate empty file
    fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin',
                        'wb')
    gdal.VSIFCloseL(fp)

    fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin',
                        'rb')
    if fp is None:
        gdaltest.post_reason('fail')
        return 'fail'
    gdal.VSIFCloseL(fp)

    fp = gdal.VSIFOpenL('/vsimem/file.bin', 'rb')
    header = gdal.VSIFReadL(1, 1000, fp)
    gdal.VSIFCloseL(fp)

    if len(header) != 46:
        gdaltest.post_reason('fail')
        print(len(header))
        return 'fail'

    # Test shortening header
    for i in range(46):
        fp = gdal.VSIFOpenL('/vsimem/file.bin', 'wb')
        gdal.VSIFWriteL(header, 1, 46 - 1 - i, fp)
        gdal.VSIFCloseL(fp)

        with gdaltest.error_handler():
            fp = gdal.VSIFOpenL(
                '/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin', 'rb')
        if fp is not None:
            gdaltest.post_reason('fail')
            return 'fail'

    # Test corrupting all bytes of header
    for i in range(46):
        for val in (0, 127, 255):
            fp = gdal.VSIFOpenL('/vsimem/file.bin', 'wb')
            try:
                new_byte = chr(val).encode('latin1')
            except (UnicodeDecodeError, UnicodeEncodeError):
                new_byte = chr(val)
            header_new = header[0:i] + new_byte + header[i + 1:]
            gdal.VSIFWriteL(header_new, 1, 46, fp)
            gdal.VSIFCloseL(fp)

            with gdaltest.error_handler():
                fp = gdal.VSIFOpenL(
                    '/vsicrypt/key=DONT_USE_IN_PROD,file='
                    '/vsimem/file.bin', 'rb')
            if fp is not None:
                gdal.VSIFCloseL(fp)

    gdal.SetConfigOption('VSICRYPT_IV', 'TOO_SHORT')
    with gdaltest.error_handler():
        fp = gdal.VSIFOpenL(
            '/vsicrypt/key=DONT_USE_IN_PROD,file='
            '/vsimem/file.bin', 'wb')
    gdal.SetConfigOption('VSICRYPT_IV', None)
    if fp is not None:
        gdal.VSIFCloseL(fp)

    # Inconsistent initial vector.
    header = struct.pack(
        'B' * 38,
        86,
        83,
        73,
        67,
        82,
        89,
        80,
        84,  # signature
        38,
        0,  # header size
        1,  # major
        0,  # minor
        0,
        2,  # sector size
        0,  # alg
        0,  # mode
        8,  # size of IV (should be 16)
        32,
        13,
        169,
        71,
        154,
        208,
        22,
        32,  # IV
        0,
        0,  # size of free text
        0,  # size of key check
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,  # size of unencrypted file
        0,
        0  # size of extra content
    )
    fp = gdal.VSIFOpenL('/vsimem/file.bin', 'wb')
    gdal.VSIFWriteL(header, 1, len(header), fp)
    gdal.VSIFCloseL(fp)

    with gdaltest.error_handler():
        fp = gdal.VSIFOpenL(
            '/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin', 'rb')
    if fp is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    # Inconsistent initial vector with key check.
    header = struct.pack(
        'B' * 39,
        86,
        83,
        73,
        67,
        82,
        89,
        80,
        84,  # signature
        39,
        0,  # header size
        1,  # major
        0,  # minor
        0,
        2,  # sector size
        0,  # alg
        0,  # mode
        8,  # size of IV (should be 16)
        32,
        13,
        169,
        71,
        154,
        208,
        22,
        32,  # IV
        0,
        0,  # size of free text
        1,  # size of key check
        0,  # key check
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,  # size of unencrypted file
        0,
        0  # size of extra content
    )
    fp = gdal.VSIFOpenL('/vsimem/file.bin', 'wb')
    gdal.VSIFWriteL(header, 1, len(header), fp)
    gdal.VSIFCloseL(fp)

    with gdaltest.error_handler():
        fp = gdal.VSIFOpenL(
            '/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin', 'rb')
    if fp is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    # Test reading with wrong key
    fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin',
                        'wb')
    gdal.VSIFWriteL('hello', 1, 5, fp)
    gdal.VSIFCloseL(fp)

    fp = gdal.VSIFOpenL('/vsicrypt/key=dont_use_in_prod,file=/vsimem/file.bin',
                        'rb')
    content = gdal.VSIFReadL(1, 5, fp).decode('latin1')
    gdal.VSIFCloseL(fp)

    if content == 'hello':
        gdaltest.post_reason('fail')
        return 'fail'

    with gdaltest.error_handler():
        fp = gdal.VSIFOpenL('/vsicrypt/key=short_key,file=/vsimem/file.bin',
                            'ab')
    if fp is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    # Test reading with wrong key with add_key_check
    fp = gdal.VSIFOpenL(
        '/vsicrypt/key=DONT_USE_IN_PROD,add_key_check=yes,file=/vsimem/file.bin',
        'wb')
    gdal.VSIFWriteL('hello', 1, 5, fp)
    gdal.VSIFCloseL(fp)

    with gdaltest.error_handler():
        fp = gdal.VSIFOpenL(
            '/vsicrypt/key=dont_use_in_prod,file=/vsimem/file.bin', 'rb')
    if fp is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    with gdaltest.error_handler():
        fp = gdal.VSIFOpenL('/vsicrypt/key=short_key,file=/vsimem/file.bin',
                            'ab')
    if fp is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    with gdaltest.error_handler():
        fp = gdal.VSIFOpenL(
            '/vsicrypt/key=dont_use_in_prod,file=/vsimem/file.bin', 'ab')
    if fp is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    # Test creating with potentially not build-in alg:
    with gdaltest.error_handler():
        fp = gdal.VSIFOpenL(
            '/vsicrypt/alg=blowfish,key=DONT_USE_IN_PROD,file=/vsimem/file.bin',
            'wb')
    if fp is not None:
        gdal.VSIFCloseL(fp)

    # Invalid sector_size
    with gdaltest.error_handler():
        fp = gdal.VSIFOpenL(
            '/vsicrypt/key=DONT_USE_IN_PROD,sector_size=1,file=/vsimem/file.bin',
            'wb')
    if fp is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    # Sector size (16) should be at least twice larger than the block size (16) in CBC_CTS
    with gdaltest.error_handler():
        fp = gdal.VSIFOpenL(
            '/vsicrypt/key=DONT_USE_IN_PROD,sector_size=16,mode=CBC_CTS,file=/vsimem/file.bin',
            'wb')
    if fp is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    gdal.Unlink('/vsimem/file.bin')

    return 'success'
Example #42
0
def test_ogr_jml_2():

    # Invalid filename
    gdal.PushErrorHandler('CPLQuietErrorHandler')
    ds = ogr.GetDriverByName('JML').CreateDataSource('/foo/ogr_jml.jml')
    gdal.PopErrorHandler()
    ds = None

    # Empty layer
    ds = ogr.GetDriverByName('JML').CreateDataSource('/vsimem/ogr_jml.jml')
    lyr = ds.CreateLayer('foo')
    lyr.ResetReading()
    lyr.GetNextFeature()
    ds = None

    f = gdal.VSIFOpenL('/vsimem/ogr_jml.jml', 'rb')
    data = gdal.VSIFReadL(1, 1000, f).decode('ascii')
    gdal.VSIFCloseL(f)

    assert data == """<?xml version='1.0' encoding='UTF-8'?>
<JCSDataFile xmlns:gml="http://www.opengis.net/gml" xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" >
<JCSGMLInputTemplate>
<CollectionElement>featureCollection</CollectionElement>
<FeatureElement>feature</FeatureElement>
<GeometryElement>geometry</GeometryElement>
<CRSElement>boundedBy</CRSElement>
<ColumnDefinitions>
</ColumnDefinitions>
</JCSGMLInputTemplate>
<featureCollection>
  <gml:boundedBy>
    <gml:Box>
      <gml:coordinates decimal="." cs="," ts=" ">0.00,0.00 -1.00,-1.00</gml:coordinates>
    </gml:Box>
  </gml:boundedBy>
</featureCollection>
</JCSDataFile>
"""

    gdal.Unlink('/vsimem/ogr_jml.jml')

    # Test all data types
    ds = ogr.GetDriverByName('JML').CreateDataSource('/vsimem/ogr_jml.jml')
    srs = osr.SpatialReference()
    srs.ImportFromEPSG(32632)
    lyr = ds.CreateLayer('foo', srs=srs)
    lyr.CreateField(ogr.FieldDefn('str', ogr.OFTString))
    lyr.CreateField(ogr.FieldDefn('int', ogr.OFTInteger))
    lyr.CreateField(ogr.FieldDefn('double', ogr.OFTReal))
    lyr.CreateField(ogr.FieldDefn('date', ogr.OFTDate))
    lyr.CreateField(ogr.FieldDefn('datetime', ogr.OFTDateTime))
    lyr.CreateField(ogr.FieldDefn('datetime2', ogr.OFTDateTime))

    with gdaltest.error_handler():
        lyr.CreateField(ogr.FieldDefn('time_as_str', ogr.OFTTime))

    assert lyr.TestCapability(ogr.OLCCreateField) == 1
    assert lyr.TestCapability(ogr.OLCSequentialWrite) == 1
    assert lyr.TestCapability(ogr.OLCRandomWrite) == 0
    assert lyr.TestCapability(ogr.OLCStringsAsUTF8) == 1

    # empty feature
    f = ogr.Feature(lyr.GetLayerDefn())
    lyr.CreateFeature(f)

    assert lyr.TestCapability(ogr.OLCCreateField) == 0
    assert lyr.CreateField(ogr.FieldDefn('that_wont_work')) != 0

    f = ogr.Feature(lyr.GetLayerDefn())
    f.SetField('str', 'fo<o')
    f.SetField('int', 1)
    f.SetField('double', 1.23)
    f.SetField('date', '2014-10-19')
    f.SetField('datetime', '2014-10-19 12:34:56')
    f.SetField('datetime2', '2014-10-19 12:34:56+02')
    f.SetField('time_as_str', '12:34:56')
    f.SetGeometryDirectly(ogr.CreateGeometryFromWkt('POINT (1 2)'))
    f.SetStyleString('PEN(c:#112233)')
    lyr.CreateFeature(f)

    f = ogr.Feature(lyr.GetLayerDefn())
    f.SetGeometryDirectly(ogr.CreateGeometryFromWkt('POLYGON ((0 0,0 10,10 10,10 0,0 0))'))
    f.SetStyleString('BRUSH(fc:#112233)')
    lyr.CreateFeature(f)

    ds = None

    f = gdal.VSIFOpenL('/vsimem/ogr_jml.jml', 'rb')
    data = gdal.VSIFReadL(1, 10000, f).decode('ascii')
    gdal.VSIFCloseL(f)

    assert data == """<?xml version='1.0' encoding='UTF-8'?>
<JCSDataFile xmlns:gml="http://www.opengis.net/gml" xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" >
<JCSGMLInputTemplate>
<CollectionElement>featureCollection</CollectionElement>
<FeatureElement>feature</FeatureElement>
<GeometryElement>geometry</GeometryElement>
<CRSElement>boundedBy</CRSElement>
<ColumnDefinitions>
     <column>
          <name>str</name>
          <type>STRING</type>
          <valueElement elementName="property" attributeName="name" attributeValue="str"/>
          <valueLocation position="body"/>
     </column>
     <column>
          <name>int</name>
          <type>INTEGER</type>
          <valueElement elementName="property" attributeName="name" attributeValue="int"/>
          <valueLocation position="body"/>
     </column>
     <column>
          <name>double</name>
          <type>DOUBLE</type>
          <valueElement elementName="property" attributeName="name" attributeValue="double"/>
          <valueLocation position="body"/>
     </column>
     <column>
          <name>date</name>
          <type>DATE</type>
          <valueElement elementName="property" attributeName="name" attributeValue="date"/>
          <valueLocation position="body"/>
     </column>
     <column>
          <name>datetime</name>
          <type>DATE</type>
          <valueElement elementName="property" attributeName="name" attributeValue="datetime"/>
          <valueLocation position="body"/>
     </column>
     <column>
          <name>datetime2</name>
          <type>DATE</type>
          <valueElement elementName="property" attributeName="name" attributeValue="datetime2"/>
          <valueLocation position="body"/>
     </column>
     <column>
          <name>time_as_str</name>
          <type>STRING</type>
          <valueElement elementName="property" attributeName="name" attributeValue="time_as_str"/>
          <valueLocation position="body"/>
     </column>
     <column>
          <name>R_G_B</name>
          <type>STRING</type>
          <valueElement elementName="property" attributeName="name" attributeValue="R_G_B"/>
          <valueLocation position="body"/>
     </column>
</ColumnDefinitions>
</JCSGMLInputTemplate>
<featureCollection>
  <gml:boundedBy>
    <gml:Box srsName="http://www.opengis.net/gml/srs/epsg.xml#32632">
      <gml:coordinates decimal="." cs="," ts=" ">0.0000000000,0.0000000000 10.0000000000,10.0000000000                                               </gml:coordinates>
    </gml:Box>
  </gml:boundedBy>
     <feature>
          <geometry>
                <gml:MultiGeometry></gml:MultiGeometry>
          </geometry>
          <property name="str"></property>
          <property name="int"></property>
          <property name="double"></property>
          <property name="date"></property>
          <property name="datetime"></property>
          <property name="datetime2"></property>
          <property name="time_as_str"></property>
          <property name="R_G_B"></property>
     </feature>
     <feature>
          <geometry>
                <gml:Point><gml:coordinates>1,2</gml:coordinates></gml:Point>
          </geometry>
          <property name="str">fo&lt;o</property>
          <property name="int">1</property>
          <property name="double">1.23</property>
          <property name="date">2014/10/19</property>
          <property name="datetime">2014-10-19T12:34:56</property>
          <property name="datetime2">2014-10-19T12:34:56.000+0200</property>
          <property name="time_as_str">12:34:56</property>
          <property name="R_G_B">112233</property>
     </feature>
     <feature>
          <geometry>
                <gml:Polygon><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>0,0 0,10 10,10 10,0 0,0</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon>
          </geometry>
          <property name="str"></property>
          <property name="int"></property>
          <property name="double"></property>
          <property name="date"></property>
          <property name="datetime"></property>
          <property name="datetime2"></property>
          <property name="time_as_str"></property>
          <property name="R_G_B">112233</property>
     </feature>
</featureCollection>
</JCSDataFile>
"""

    gdal.Unlink('/vsimem/ogr_jml.jml')

    # Test with an explicit R_G_B field
    ds = ogr.GetDriverByName('JML').CreateDataSource('/vsimem/ogr_jml.jml')
    lyr = ds.CreateLayer('foo')
    lyr.CreateField(ogr.FieldDefn('R_G_B', ogr.OFTString))
    f = ogr.Feature(lyr.GetLayerDefn())
    f.SetField('R_G_B', '112233')
    f.SetGeometryDirectly(ogr.CreateGeometryFromWkt('POINT (1 2)'))
    lyr.CreateFeature(f)

    # Test that R_G_B is not overridden by feature style
    f.SetField('R_G_B', '445566')
    f.SetGeometryDirectly(ogr.CreateGeometryFromWkt('POINT (1 2)'))
    f.SetStyleString('PEN(c:#778899)')
    lyr.CreateFeature(f)

    ds = None

    f = gdal.VSIFOpenL('/vsimem/ogr_jml.jml', 'rb')
    data = gdal.VSIFReadL(1, 10000, f).decode('ascii')
    gdal.VSIFCloseL(f)

    assert '112233' in data and '445566' in data

    gdal.Unlink('/vsimem/ogr_jml.jml')

    # Test CREATE_R_G_B_FIELD=NO
    ds = ogr.GetDriverByName('JML').CreateDataSource('/vsimem/ogr_jml.jml')
    lyr = ds.CreateLayer('foo', options=['CREATE_R_G_B_FIELD=NO'])
    lyr.CreateField(ogr.FieldDefn('str', ogr.OFTString))
    f = ogr.Feature(lyr.GetLayerDefn())
    lyr.CreateFeature(f)
    ds = None

    f = gdal.VSIFOpenL('/vsimem/ogr_jml.jml', 'rb')
    data = gdal.VSIFReadL(1, 10000, f).decode('ascii')
    gdal.VSIFCloseL(f)

    assert 'R_G_B' not in data

    gdal.Unlink('/vsimem/ogr_jml.jml')

    # Test CREATE_OGR_STYLE_FIELD=YES
    ds = ogr.GetDriverByName('JML').CreateDataSource('/vsimem/ogr_jml.jml')
    lyr = ds.CreateLayer('foo', options=['CREATE_OGR_STYLE_FIELD=YES'])
    lyr.CreateField(ogr.FieldDefn('str', ogr.OFTString))
    f = ogr.Feature(lyr.GetLayerDefn())
    f.SetStyleString('PEN(c:#445566)')
    lyr.CreateFeature(f)
    ds = None

    f = gdal.VSIFOpenL('/vsimem/ogr_jml.jml', 'rb')
    data = gdal.VSIFReadL(1, 10000, f).decode('ascii')
    gdal.VSIFCloseL(f)

    assert 'OGR_STYLE' in data and 'PEN(c:#445566)' in data

    gdal.Unlink('/vsimem/ogr_jml.jml')

    # Test CREATE_OGR_STYLE_FIELD=YES with a R_G_B field
    ds = ogr.GetDriverByName('JML').CreateDataSource('/vsimem/ogr_jml.jml')
    lyr = ds.CreateLayer('foo', options=['CREATE_OGR_STYLE_FIELD=YES'])
    lyr.CreateField(ogr.FieldDefn('R_G_B', ogr.OFTString))
    f = ogr.Feature(lyr.GetLayerDefn())
    f.SetField('R_G_B', '112233')
    f.SetGeometryDirectly(ogr.CreateGeometryFromWkt('POINT (1 2)'))
    f.SetStyleString('PEN(c:#445566)')
    lyr.CreateFeature(f)
    ds = None

    f = gdal.VSIFOpenL('/vsimem/ogr_jml.jml', 'rb')
    data = gdal.VSIFReadL(1, 10000, f).decode('ascii')
    gdal.VSIFCloseL(f)

    assert 'OGR_STYLE' in data and 'PEN(c:#445566)' in data and '112233' in data

    gdal.Unlink('/vsimem/ogr_jml.jml')
Example #43
0
def test_ogr_wkt_multicurve_compoundcurve_corrupted():

    with gdaltest.error_handler():
        g = ogr.CreateGeometryFromWkt('MULTICURVE(COMPOUNDCURVE')
    assert g is None
Example #44
0
File: rmf.py Project: tingli3/gdal
def rmf_12b():

    tst = gdaltest.GDALTest('rmf', 'cucled-2.rsw', 1, 4672)
    with gdaltest.error_handler():
        return tst.testOpen(check_gt=(440720, 60, 0, 3751320, 0, -60))
Example #45
0
File: vsiaz.py Project: zyhgis/gdal
def test_vsiaz_fake_write():

    if gdaltest.webserver_port == 0:
        pytest.skip()

    gdal.VSICurlClearCache()

    # Test creation of BlockBob
    f = gdal.VSIFOpenL('/vsiaz/test_copy/file.bin', 'wb')
    assert f is not None

    handler = webserver.SequentialHandler()

    def method(request):
        h = request.headers
        if 'Authorization' not in h or \
           h['Authorization'] != 'SharedKey myaccount:AigkrY7q66WCrx3JRKBte56k7kxV2cxB/ZyGNubxk5I=' or \
           'Expect' not in h or h['Expect'] != '100-continue' or \
           'Content-Length' not in h or h['Content-Length'] != '40000' or \
           'x-ms-date' not in h or h['x-ms-date'] != 'my_timestamp' or \
           'x-ms-blob-type' not in h or h['x-ms-blob-type'] != 'BlockBlob':
            sys.stderr.write('Bad headers: %s\n' % str(h))
            request.send_response(403)
            return

        request.protocol_version = 'HTTP/1.1'
        request.wfile.write('HTTP/1.1 100 Continue\r\n\r\n'.encode('ascii'))
        content = request.rfile.read(40000).decode('ascii')
        if len(content) != 40000:
            sys.stderr.write('Bad headers: %s\n' % str(request.headers))
            request.send_response(403)
            request.send_header('Content-Length', 0)
            request.end_headers()
            return
        request.send_response(201)
        request.send_header('Content-Length', 0)
        request.end_headers()

    handler.add('PUT',
                '/azure/blob/myaccount/test_copy/file.bin',
                custom_method=method)
    with webserver.install_http_handler(handler):
        ret = gdal.VSIFWriteL('x' * 35000, 1, 35000, f)
        ret += gdal.VSIFWriteL('x' * 5000, 1, 5000, f)
        if ret != 40000:
            gdal.VSIFCloseL(f)
            pytest.fail(ret)
        gdal.VSIFCloseL(f)

    # Simulate illegal read
    f = gdal.VSIFOpenL('/vsiaz/test_copy/file.bin', 'wb')
    assert f is not None
    with gdaltest.error_handler():
        ret = gdal.VSIFReadL(1, 1, f)
    assert not ret
    gdal.VSIFCloseL(f)

    # Simulate illegal seek
    f = gdal.VSIFOpenL('/vsiaz/test_copy/file.bin', 'wb')
    assert f is not None
    with gdaltest.error_handler():
        ret = gdal.VSIFSeekL(f, 1, 0)
    assert ret != 0
    gdal.VSIFCloseL(f)

    # Simulate failure when putting BlockBob
    f = gdal.VSIFOpenL('/vsiaz/test_copy/file.bin', 'wb')
    assert f is not None

    handler = webserver.SequentialHandler()

    def method(request):
        request.protocol_version = 'HTTP/1.1'
        request.send_response(403)
        request.send_header('Content-Length', 0)
        request.end_headers()

    handler.add('PUT',
                '/azure/blob/myaccount/test_copy/file.bin',
                custom_method=method)

    if gdal.VSIFSeekL(f, 0, 0) != 0:
        gdal.VSIFCloseL(f)
        pytest.fail()

    gdal.VSIFWriteL('x' * 35000, 1, 35000, f)

    if gdal.VSIFTellL(f) != 35000:
        gdal.VSIFCloseL(f)
        pytest.fail()

    if gdal.VSIFSeekL(f, 35000, 0) != 0:
        gdal.VSIFCloseL(f)
        pytest.fail()

    if gdal.VSIFSeekL(f, 0, 1) != 0:
        gdal.VSIFCloseL(f)
        pytest.fail()
    if gdal.VSIFSeekL(f, 0, 2) != 0:
        gdal.VSIFCloseL(f)
        pytest.fail()

    if gdal.VSIFEofL(f) != 0:
        gdal.VSIFCloseL(f)
        pytest.fail()

    with webserver.install_http_handler(handler):
        with gdaltest.error_handler():
            ret = gdal.VSIFCloseL(f)
        if ret == 0:
            gdal.VSIFCloseL(f)
            pytest.fail(ret)

    # Simulate creation of BlockBob over an existing blob of incompatible type
    f = gdal.VSIFOpenL('/vsiaz/test_copy/file.bin', 'wb')
    assert f is not None

    handler = webserver.SequentialHandler()
    handler.add('PUT', '/azure/blob/myaccount/test_copy/file.bin', 409)
    handler.add('DELETE', '/azure/blob/myaccount/test_copy/file.bin', 202)
    handler.add('PUT', '/azure/blob/myaccount/test_copy/file.bin', 201)
    with webserver.install_http_handler(handler):
        gdal.VSIFCloseL(f)

    # Test creation of AppendBlob
    gdal.SetConfigOption('VSIAZ_CHUNK_SIZE_BYTES', '10')
    f = gdal.VSIFOpenL('/vsiaz/test_copy/file.bin', 'wb')
    gdal.SetConfigOption('VSIAZ_CHUNK_SIZE_BYTES', None)
    assert f is not None

    handler = webserver.SequentialHandler()

    def method(request):
        h = request.headers
        if 'Authorization' not in h or \
           h['Authorization'] != 'SharedKey myaccount:KimVui3ptY9D5ftLlsI7CNOgK36CNAEzsXqcuHskdEY=' or \
           'Content-Length' not in h or h['Content-Length'] != '0' or \
           'x-ms-date' not in h or h['x-ms-date'] != 'my_timestamp' or \
           'x-ms-blob-type' not in h or h['x-ms-blob-type'] != 'AppendBlob':
            sys.stderr.write('Bad headers: %s\n' % str(h))
            request.send_response(403)
            return

        request.protocol_version = 'HTTP/1.1'
        request.send_response(201)
        request.send_header('Content-Length', 0)
        request.end_headers()

    handler.add('PUT',
                '/azure/blob/myaccount/test_copy/file.bin',
                custom_method=method)

    def method(request):
        h = request.headers
        if 'Content-Length' not in h or h['Content-Length'] != '10' or \
           'x-ms-date' not in h or h['x-ms-date'] != 'my_timestamp' or \
           'x-ms-blob-type' not in h or h['x-ms-blob-type'] != 'AppendBlob':
            sys.stderr.write('Bad headers: %s\n' % str(h))
            request.send_response(403)
            return

        request.protocol_version = 'HTTP/1.1'
        content = request.rfile.read(10).decode('ascii')
        if content != '0123456789':
            sys.stderr.write('Bad headers: %s\n' % str(request.headers))
            request.send_response(403)
            request.send_header('Content-Length', 0)
            request.end_headers()
            return
        request.send_response(201)
        request.send_header('Content-Length', 0)
        request.end_headers()

    handler.add('PUT',
                '/azure/blob/myaccount/test_copy/file.bin?comp=appendblock',
                custom_method=method)

    def method(request):
        h = request.headers
        if 'Content-Length' not in h or h['Content-Length'] != '6' or \
           'x-ms-date' not in h or h['x-ms-date'] != 'my_timestamp' or \
           'x-ms-blob-type' not in h or h['x-ms-blob-type'] != 'AppendBlob':
            sys.stderr.write('Bad headers: %s\n' % str(h))
            request.send_response(403)
            return

        request.protocol_version = 'HTTP/1.1'
        content = request.rfile.read(6).decode('ascii')
        if content != 'abcdef':
            sys.stderr.write('Bad headers: %s\n' % str(request.headers))
            request.send_response(403)
            request.send_header('Content-Length', 0)
            request.end_headers()
            return
        request.send_response(201)
        request.send_header('Content-Length', 0)
        request.end_headers()

    handler.add('PUT',
                '/azure/blob/myaccount/test_copy/file.bin?comp=appendblock',
                custom_method=method)

    with webserver.install_http_handler(handler):
        ret = gdal.VSIFWriteL('0123456789abcdef', 1, 16, f)
        if ret != 16:
            gdal.VSIFCloseL(f)
            pytest.fail(ret)
        gdal.VSIFCloseL(f)

    # Test failed creation of AppendBlob
    gdal.SetConfigOption('VSIAZ_CHUNK_SIZE_BYTES', '10')
    f = gdal.VSIFOpenL('/vsiaz/test_copy/file.bin', 'wb')
    gdal.SetConfigOption('VSIAZ_CHUNK_SIZE_BYTES', None)
    assert f is not None

    handler = webserver.SequentialHandler()

    def method(request):
        request.protocol_version = 'HTTP/1.1'
        request.send_response(403)
        request.send_header('Content-Length', 0)
        request.end_headers()

    handler.add('PUT',
                '/azure/blob/myaccount/test_copy/file.bin',
                custom_method=method)

    with webserver.install_http_handler(handler):
        with gdaltest.error_handler():
            ret = gdal.VSIFWriteL('0123456789abcdef', 1, 16, f)
        if ret != 0:
            gdal.VSIFCloseL(f)
            pytest.fail(ret)
        gdal.VSIFCloseL(f)

    # Test failed writing of a block of an AppendBlob
    gdal.SetConfigOption('VSIAZ_CHUNK_SIZE_BYTES', '10')
    f = gdal.VSIFOpenL('/vsiaz/test_copy/file.bin', 'wb')
    gdal.SetConfigOption('VSIAZ_CHUNK_SIZE_BYTES', None)
    assert f is not None

    handler = webserver.SequentialHandler()
    handler.add('PUT', '/azure/blob/myaccount/test_copy/file.bin', 201)
    handler.add('PUT',
                '/azure/blob/myaccount/test_copy/file.bin?comp=appendblock',
                403)
    with webserver.install_http_handler(handler):
        with gdaltest.error_handler():
            ret = gdal.VSIFWriteL('0123456789abcdef', 1, 16, f)
        if ret != 0:
            gdal.VSIFCloseL(f)
            pytest.fail(ret)
        gdal.VSIFCloseL(f)
Example #46
0
File: rmf.py Project: tingli3/gdal
def rmf_12c():

    tst = gdaltest.GDALTest('rmf', 'invalid-subheader.rsw', 1, 4672)
    with gdaltest.error_handler():
        return tst.testOpen(check_gt=(440720, 60, 0, 3751320, 0, -60))
Example #47
0
def vsis3_2():

    if gdaltest.webserver_port == 0:
        return 'skip'

    gdal.SetConfigOption('AWS_SECRET_ACCESS_KEY', 'AWS_SECRET_ACCESS_KEY')
    gdal.SetConfigOption('AWS_ACCESS_KEY_ID', 'AWS_ACCESS_KEY_ID')
    gdal.SetConfigOption('AWS_TIMESTAMP', '20150101T000000Z')
    gdal.SetConfigOption('AWS_HTTPS', 'NO')
    gdal.SetConfigOption('AWS_VIRTUAL_HOSTING', 'NO')
    gdal.SetConfigOption('AWS_S3_ENDPOINT', '127.0.0.1:%d' % gdaltest.webserver_port)

    f = open_for_read('/vsis3/s3_fake_bucket/resource')
    if f is None:
        gdaltest.post_reason('fail')
        return 'fail'
    data = gdal.VSIFReadL(1, 4, f).decode('ascii')
    gdal.VSIFCloseL(f)

    if data != 'foo':
        gdaltest.post_reason('fail')
        print(data)
        return 'fail'

    f = open_for_read('/vsis3_streaming/s3_fake_bucket/resource')
    if f is None:
        gdaltest.post_reason('fail')
        return 'fail'
    data = gdal.VSIFReadL(1, 4, f).decode('ascii')
    gdal.VSIFCloseL(f)

    if data != 'foo':
        gdaltest.post_reason('fail')
        print(data)
        return 'fail'

    # Test with temporary credentials
    gdal.SetConfigOption('AWS_SESSION_TOKEN', 'AWS_SESSION_TOKEN')
    f = open_for_read('/vsis3/s3_fake_bucket_with_session_token/resource')
    if f is None:
        gdaltest.post_reason('fail')
        return 'fail'
    data = gdal.VSIFReadL(1, 4, f).decode('ascii')
    gdal.VSIFCloseL(f)
    gdal.SetConfigOption('AWS_SESSION_TOKEN', None)

    #old_val = gdal.GetConfigOption('GDAL_DISABLE_READDIR_ON_OPEN')
    #gdal.SetConfigOption('GDAL_DISABLE_READDIR_ON_OPEN', 'EMPTY_DIR')
    stat_res = gdal.VSIStatL('/vsis3/s3_fake_bucket/resource2.bin')
    #gdal.SetConfigOption('GDAL_DISABLE_READDIR_ON_OPEN', old_val)
    if stat_res is None or stat_res.size != 1000000:
        gdaltest.post_reason('fail')
        if stat_res is not None:
            print(stat_res.size)
        else:
            print(stat_res)
        return 'fail'

    stat_res = gdal.VSIStatL('/vsis3_streaming/s3_fake_bucket/resource2.bin')
    if stat_res is None or stat_res.size != 1000000:
        gdaltest.post_reason('fail')
        if stat_res is not None:
            print(stat_res.size)
        else:
            print(stat_res)
        return 'fail'

    # Test region and endpoint 'redirects'
    f = open_for_read('/vsis3/s3_fake_bucket/redirect')
    if f is None:
        gdaltest.post_reason('fail')
        return 'fail'
    data = gdal.VSIFReadL(1, 4, f).decode('ascii')
    gdal.VSIFCloseL(f)

    if data != 'foo':

        if gdaltest.is_travis_branch('trusty'):
            print('Skipped on trusty branch, but should be investigated')
            return 'skip'

        gdaltest.post_reason('fail')
        print(data)
        return 'fail'

    # Test region and endpoint 'redirects'
    f = open_for_read('/vsis3_streaming/s3_fake_bucket/redirect')
    if f is None:
        gdaltest.post_reason('fail')
        return 'fail'
    data = gdal.VSIFReadL(1, 4, f).decode('ascii')
    gdal.VSIFCloseL(f)

    if data != 'foo':
        gdaltest.post_reason('fail')
        print(data)
        return 'fail'

    gdal.ErrorReset()
    with gdaltest.error_handler():
        f = open_for_read('/vsis3_streaming/s3_fake_bucket/non_xml_error')
    if f is not None or gdal.VSIGetLastErrorMsg().find('bla') < 0:
        gdaltest.post_reason('fail')
        print(gdal.VSIGetLastErrorMsg())
        return 'fail'

    gdal.ErrorReset()
    with gdaltest.error_handler():
        f = open_for_read('/vsis3_streaming/s3_fake_bucket/invalid_xml_error')
    if f is not None or gdal.VSIGetLastErrorMsg().find('<oops>') < 0:
        gdaltest.post_reason('fail')
        print(gdal.VSIGetLastErrorMsg())
        return 'fail'

    gdal.ErrorReset()
    with gdaltest.error_handler():
        f = open_for_read('/vsis3_streaming/s3_fake_bucket/no_code_in_error')
    if f is not None or gdal.VSIGetLastErrorMsg().find('<Error/>') < 0:
        gdaltest.post_reason('fail')
        print(gdal.VSIGetLastErrorMsg())
        return 'fail'

    gdal.ErrorReset()
    with gdaltest.error_handler():
        f = open_for_read('/vsis3_streaming/s3_fake_bucket/no_region_in_AuthorizationHeaderMalformed_error')
    if f is not None or gdal.VSIGetLastErrorMsg().find('<Error>') < 0:
        gdaltest.post_reason('fail')
        print(gdal.VSIGetLastErrorMsg())
        return 'fail'

    gdal.ErrorReset()
    with gdaltest.error_handler():
        f = open_for_read('/vsis3_streaming/s3_fake_bucket/no_endpoint_in_PermanentRedirect_error')
    if f is not None or gdal.VSIGetLastErrorMsg().find('<Error>') < 0:
        gdaltest.post_reason('fail')
        print(gdal.VSIGetLastErrorMsg())
        return 'fail'

    gdal.ErrorReset()
    with gdaltest.error_handler():
        f = open_for_read('/vsis3_streaming/s3_fake_bucket/no_message_in_error')
    if f is not None or gdal.VSIGetLastErrorMsg().find('<Error>') < 0:
        gdaltest.post_reason('fail')
        print(gdal.VSIGetLastErrorMsg())
        return 'fail'

    return 'success'
Example #48
0
File: rmf.py Project: tingli3/gdal
def rmf_2():

    tst = gdaltest.GDALTest('rmf', 'byte-lzw.rsw', 1, 40503)
    with gdaltest.error_handler():
        return tst.testOpen()
Example #49
0
def vsis3_6():

    if gdaltest.webserver_port == 0:
        return 'skip'

    gdal.SetConfigOption('VSIS3_CHUNK_SIZE', '1') # 1 MB
    f = gdal.VSIFOpenL('/vsis3/s3_fake_bucket4/large_file.bin', 'wb')
    gdal.SetConfigOption('VSIS3_CHUNK_SIZE', None)
    if f is None:
        gdaltest.post_reason('fail')
        return 'fail'
    size = 1024*1024+1
    ret = gdal.VSIFWriteL(''.join('a' for i in range(size)), 1,size, f)
    if ret != size:
        gdaltest.post_reason('fail')
        return 'fail'
    gdal.ErrorReset()
    gdal.VSIFCloseL(f)
    if gdal.GetLastErrorMsg() != '':
        gdaltest.post_reason('fail')
        return 'fail'

    for filename in [ '/vsis3/s3_fake_bucket4/large_file_initiate_403_error.bin',
                      '/vsis3/s3_fake_bucket4/large_file_initiate_empty_result.bin',
                      '/vsis3/s3_fake_bucket4/large_file_initiate_invalid_xml_result.bin',
                      '/vsis3/s3_fake_bucket4/large_file_initiate_no_uploadId.bin' ]:
        gdal.SetConfigOption('VSIS3_CHUNK_SIZE', '1') # 1 MB
        f = gdal.VSIFOpenL(filename, 'wb')
        gdal.SetConfigOption('VSIS3_CHUNK_SIZE', None)
        if f is None:
            gdaltest.post_reason('fail')
            return 'fail'
        size = 1024*1024+1
        with gdaltest.error_handler():
            ret = gdal.VSIFWriteL(''.join('a' for i in range(size)), 1,size, f)
        if ret != 0:
            gdaltest.post_reason('fail')
            print(ret)
            return 'fail'
        gdal.ErrorReset()
        gdal.VSIFCloseL(f)
        if gdal.GetLastErrorMsg() != '':
            gdaltest.post_reason('fail')
            return 'fail'

    for filename in [ '/vsis3/s3_fake_bucket4/large_file_upload_part_403_error.bin',
                      '/vsis3/s3_fake_bucket4/large_file_upload_part_no_etag.bin']:
        gdal.SetConfigOption('VSIS3_CHUNK_SIZE', '1') # 1 MB
        f = gdal.VSIFOpenL(filename, 'wb')
        gdal.SetConfigOption('VSIS3_CHUNK_SIZE', None)
        if f is None:
            gdaltest.post_reason('fail')
            return 'fail'
        size = 1024*1024+1
        with gdaltest.error_handler():
            ret = gdal.VSIFWriteL(''.join('a' for i in range(size)), 1,size, f)
        if ret != 0:
            gdaltest.post_reason('fail')
            print(ret)
            return 'fail'
        gdal.ErrorReset()
        gdal.VSIFCloseL(f)
        if gdal.GetLastErrorMsg() != '':
            gdaltest.post_reason('fail')
            return 'fail'

    return 'success'
Example #50
0
File: rmf.py Project: tingli3/gdal
def rmf_3():

    tst = gdaltest.GDALTest('rmf', 'float64.mtw', 1, 4672)
    with gdaltest.error_handler():
        return tst.testOpen(check_gt=(440720, 60, 0, 3751320, 0, -60))
Example #51
0
def vsis3_1():
    try:
        drv = gdal.GetDriverByName( 'HTTP' )
    except:
        drv = None

    if drv is None:
        return 'skip'

        # RETODO: Bind to swig, change test

    # Missing AWS_SECRET_ACCESS_KEY
    gdal.ErrorReset()
    with gdaltest.error_handler():
        f = open_for_read('/vsis3/foo/bar')
    if f is not None or gdal.VSIGetLastErrorMsg().find('AWS_SECRET_ACCESS_KEY') < 0:
        gdaltest.post_reason('fail')
        print(gdal.VSIGetLastErrorMsg())
        return 'fail'

    gdal.ErrorReset()
    with gdaltest.error_handler():
        f = open_for_read('/vsis3_streaming/foo/bar')
    if f is not None or gdal.VSIGetLastErrorMsg().find('AWS_SECRET_ACCESS_KEY') < 0:
        gdaltest.post_reason('fail')
        print(gdal.VSIGetLastErrorMsg())
        return 'fail'

    gdal.SetConfigOption('AWS_SECRET_ACCESS_KEY', 'AWS_SECRET_ACCESS_KEY')

    # Missing AWS_ACCESS_KEY_ID
    gdal.ErrorReset()
    with gdaltest.error_handler():
        f = open_for_read('/vsis3/foo/bar')
    if f is not None or gdal.VSIGetLastErrorMsg().find('AWS_ACCESS_KEY_ID') < 0:
        gdaltest.post_reason('fail')
        print(gdal.VSIGetLastErrorMsg())
        return 'fail'

    gdal.SetConfigOption('AWS_ACCESS_KEY_ID', 'AWS_ACCESS_KEY_ID')

    # ERROR 1: The AWS Access Key Id you provided does not exist in our records.
    gdal.ErrorReset()
    with gdaltest.error_handler():
        f = open_for_read('/vsis3/foo/bar.baz')
    if f is not None or gdal.VSIGetLastErrorMsg() == '':
        if f is not None:
            gdal.VSIFCloseL(f)
        if gdal.GetConfigOption('APPVEYOR') is not None:
            return 'success'
        gdaltest.post_reason('fail')
        print(gdal.VSIGetLastErrorMsg())
        return 'fail'

    gdal.ErrorReset()
    with gdaltest.error_handler():
        f = open_for_read('/vsis3_streaming/foo/bar.baz')
    if f is not None or gdal.VSIGetLastErrorMsg() == '':
        gdaltest.post_reason('fail')
        print(gdal.VSIGetLastErrorMsg())
        return 'fail'

    return 'success'
Example #52
0
def test_mbtiles_create():

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

    filename = '/vsimem/mbtiles_create.mbtiles'
    gdaltest.mbtiles_drv.Create(filename, 1, 1, 1)
    with gdaltest.error_handler():
        assert gdal.Open(filename) is None

    # Nominal case
    gdal.Unlink(filename)
    src_ds = gdal.Open('data/mbtiles/byte.mbtiles')
    ds = gdaltest.mbtiles_drv.Create(filename, src_ds.RasterXSize,
                                     src_ds.RasterYSize)
    ds.SetGeoTransform(src_ds.GetGeoTransform())
    ds.SetProjection(src_ds.GetProjectionRef())

    # Cannot modify geotransform once set"
    with gdaltest.error_handler():
        ret = ds.SetGeoTransform(src_ds.GetGeoTransform())
    assert ret != 0
    ds = None

    ds = gdal.Open('data/mbtiles/byte.mbtiles')
    # SetGeoTransform() not supported on read-only dataset"
    with gdaltest.error_handler():
        ret = ds.SetGeoTransform(src_ds.GetGeoTransform())
    assert ret != 0
    # SetProjection() not supported on read-only dataset
    with gdaltest.error_handler():
        ret = ds.SetProjection(src_ds.GetProjectionRef())
    assert ret != 0
    ds = None

    gdal.Unlink(filename)
    ds = gdaltest.mbtiles_drv.Create(filename, src_ds.RasterXSize,
                                     src_ds.RasterYSize)
    # Only EPSG:3857 supported on MBTiles dataset
    with gdaltest.error_handler():
        ret = ds.SetProjection('LOCAL_CS["foo"]')
    assert ret != 0
    ds = None

    gdal.Unlink(filename)
    ds = gdaltest.mbtiles_drv.Create(filename, src_ds.RasterXSize,
                                     src_ds.RasterYSize)
    # Only north-up non rotated geotransform supported
    with gdaltest.error_handler():
        ret = ds.SetGeoTransform([0, 1, 0, 0, 0, 1])
    assert ret != 0
    ds = None

    gdal.Unlink(filename)
    ds = gdaltest.mbtiles_drv.Create(filename, src_ds.RasterXSize,
                                     src_ds.RasterYSize)
    # Could not find an appropriate zoom level that matches raster pixel size
    with gdaltest.error_handler():
        ret = ds.SetGeoTransform([0, 1, 0, 0, 0, -1])
    assert ret != 0
    ds = None

    gdal.Unlink(filename)
Example #53
0
def vsicrypt_3():

    if not gdaltest.has_vsicrypt:
        return 'skip'

    for options in [
            'sector_size=16', 'alg=AES', 'alg=DES_EDE2', 'alg=DES_EDE3',
            'alg=SKIPJACK', 'alg=invalid', 'mode=CBC', 'mode=CFB', 'mode=OFB',
            'mode=CTR', 'mode=CBC_CTS', 'mode=invalid',
            'freetext=my_free_text', 'add_key_check=yes'
    ]:

        gdal.Unlink('/vsimem/file.bin')

        if options == 'alg=invalid' or options == 'mode=invalid':
            with gdaltest.error_handler():
                fp = gdal.VSIFOpenL(
                    '/vsicrypt/key=DONT_USE_IN_PRODDONT_USE_IN_PROD,%s,file=/vsimem/file.bin'
                    % options, 'wb')
        else:
            fp = gdal.VSIFOpenL(
                '/vsicrypt/key=DONT_USE_IN_PRODDONT_USE_IN_PROD,%s,file=/vsimem/file.bin'
                % options, 'wb')
        if fp is None:
            gdaltest.post_reason('fail')
            print(options)
            return 'fail'
        gdal.VSIFWriteL('hello', 1, 5, fp)
        gdal.VSIFCloseL(fp)

        fp = gdal.VSIFOpenL(
            '/vsicrypt/key=DONT_USE_IN_PRODDONT_USE_IN_PROD,file=/vsimem/file.bin',
            'r')
        content = gdal.VSIFReadL(1, 5, fp).decode('latin1')
        gdal.VSIFCloseL(fp)

        if content != 'hello':
            gdaltest.post_reason('fail')
            print(options)
            return 'fail'

    # Some of those algs might be missing
    for options in [
            'alg=Blowfish', 'alg=Camellia', 'alg=CAST256', 'alg=MARS',
            'alg=IDEA', 'alg=RC5', 'alg=RC6', 'alg=Serpent', 'alg=SHACAL2',
            'alg=Twofish', 'alg=XTEA'
    ]:

        gdal.Unlink('/vsimem/file.bin')

        with gdaltest.error_handler():
            fp = gdal.VSIFOpenL(
                '/vsicrypt/key=DONT_USE_IN_PROD,%s,file=/vsimem/file.bin' %
                options, 'wb')
        if fp is not None:
            gdal.VSIFWriteL('hello', 1, 5, fp)
            gdal.VSIFCloseL(fp)

            fp = gdal.VSIFOpenL(
                '/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin', 'rb')
            content = gdal.VSIFReadL(1, 5, fp).decode('latin1')
            gdal.VSIFCloseL(fp)

            if content != 'hello':
                gdaltest.post_reason('fail')
                print(options)
                return 'fail'

    # Test key generation

    # Do NOT set VSICRYPT_CRYPTO_RANDOM=NO in production. This is just to speed up tests !
    gdal.SetConfigOption("VSICRYPT_CRYPTO_RANDOM", "NO")
    fp = gdal.VSIFOpenL(
        '/vsicrypt/key=GENERATE_IT,add_key_check=yes,file=/vsimem/file.bin',
        'wb')
    gdal.SetConfigOption("VSICRYPT_CRYPTO_RANDOM", None)

    # Get the generated random key
    key_b64 = gdal.GetConfigOption('VSICRYPT_KEY_B64')
    if key_b64 is None:
        gdaltest.post_reason('fail')
        return 'fail'

    gdal.VSIFWriteL('hello', 1, 5, fp)
    gdal.VSIFCloseL(fp)

    fp = gdal.VSIFOpenL('/vsicrypt//vsimem/file.bin', 'rb')
    content = gdal.VSIFReadL(1, 5, fp).decode('latin1')
    gdal.VSIFCloseL(fp)

    if content != 'hello':
        gdaltest.post_reason('fail')
        print(options)
        return 'fail'

    gdal.SetConfigOption('VSICRYPT_KEY_B64', None)

    fp = gdal.VSIFOpenL('/vsicrypt/key_b64=%s,file=/vsimem/file.bin' % key_b64,
                        'rb')
    content = gdal.VSIFReadL(1, 5, fp).decode('latin1')
    gdal.VSIFCloseL(fp)

    if content != 'hello':
        gdaltest.post_reason('fail')
        print(options)
        return 'fail'

    with gdaltest.error_handler():
        statRes = gdal.VSIStatL('/vsicrypt//vsimem/file.bin')
    if statRes is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    ret = gdal.Rename('/vsicrypt//vsimem/file.bin',
                      '/vsicrypt//vsimem/subdir_crypt/file.bin')
    if ret != 0:
        gdaltest.post_reason('fail')
        print(ret)
        return 'fail'

    ret = gdal.Rename('/vsicrypt//vsimem/subdir_crypt/file.bin',
                      '/vsimem/subdir_crypt/file2.bin')
    if ret != 0:
        gdaltest.post_reason('fail')
        print(ret)
        return 'fail'

    dir_content = gdal.ReadDir('/vsicrypt//vsimem/subdir_crypt')
    if dir_content != ['file2.bin']:
        gdaltest.post_reason('fail')
        print(dir_content)
        return 'fail'

    gdal.Unlink('/vsimem/subdir_crypt/file2.bin')

    return 'success'
Example #54
0
def bag_vr_open_supergrids():

    if gdaltest.bag_drv is None:
        return 'skip'

    ds = gdal.Open('BAG:"data/test_vr.bag":supergrid:0:0')
    if ds is None:
        gdaltest.post_reason('fail')
        return 'fail'

    got_md = gdal.Info(ds, computeChecksum=True, format='json')
    expected_md = {
        'bands': [{
            'band': 1,
            'block': [2, 1],
            'checksum': 65529,
            'colorInterpretation': 'Undefined',
            'description': 'elevation',
            'metadata': {},
            'noDataValue': 1000000.0,
            'type': 'Float32'
        }, {
            'band': 2,
            'block': [2, 1],
            'checksum': 13,
            'colorInterpretation': 'Undefined',
            'description': 'uncertainty',
            'metadata': {},
            'noDataValue': 1000000.0,
            'type': 'Float32'
        }],
        'coordinateSystem': {
            'wkt':
            'PROJCS["NAD83 / UTM zone 10N",\n    GEOGCS["NAD83",\n        DATUM["North American Datum 1983",\n            SPHEROID["GRS 1980",6378137,298.2572221010041,\n                AUTHORITY["EPSG","7019"]],\n            TOWGS84[0,0,0,0,0,0,0],\n            AUTHORITY["EPSG","6269"]],\n        PRIMEM["Greenwich",0,\n            AUTHORITY["EPSG","8901"]],\n        UNIT["degree",0.0174532925199433,\n            AUTHORITY["EPSG","9122"]],\n        AUTHORITY["EPSG","4269"]],\n    PROJECTION["Transverse_Mercator",\n        AUTHORITY["EPSG","16010"]],\n    PARAMETER["latitude_of_origin",0],\n    PARAMETER["central_meridian",-123],\n    PARAMETER["scale_factor",0.9996],\n    PARAMETER["false_easting",500000],\n    PARAMETER["false_northing",0],\n    UNIT["metre",1,\n        AUTHORITY["EPSG","9001"]],\n    AUTHORITY["EPSG","26910"]]'
        },
        'geoTransform': [
            70.10000038146973, 29.899999618530273, 0.0, 500031.89999961853,
            0.0, -31.899999618530273
        ],
        'metadata': {
            '': {
                'AREA_OR_POINT': 'Point',
                'BAG_DATETIME': '2018-08-08T12:34:56',
                'BagVersion': '1.6.2'
            },
            'IMAGE_STRUCTURE': {
                'INTERLEAVE': 'PIXEL'
            }
        },
        'size': [2, 2]
    }

    for key in expected_md:
        if key not in got_md or got_md[key] != expected_md[key]:
            gdaltest.post_reason('fail')
            print(key)
            import pprint
            pp = pprint.PrettyPrinter()
            pp.pprint(got_md)
            return 'fail'

    with gdaltest.error_handler():
        ds = gdal.Open('BAG:"/vsimem/unexisting.bag":supergrid:0:0')
    if ds is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    with gdaltest.error_handler():
        ds = gdal.Open('BAG:"data/test_vr.bag":supergrid:4:0')
    if ds is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    with gdaltest.error_handler():
        ds = gdal.Open('BAG:"data/test_vr.bag":supergrid:0:6')
    if ds is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    with gdaltest.error_handler():
        ds = gdal.OpenEx('BAG:"data/test_vr.bag":supergrid:0:0',
                         open_options=['MINX=0'])
    if gdal.GetLastErrorMsg() == '':
        gdaltest.post_reason('warning expected')
        return 'fail'

    return 'success'
Example #55
0
def mem_6():

    if gdal.GetConfigOption('SKIP_MEM_INTENSIVE_TEST') is not None:
        return 'skip'

    drv = gdal.GetDriverByName('MEM')

    # Multiplication overflow
    with gdaltest.error_handler():
        ds = drv.Create('', 1, 1, 0x7FFFFFFF, gdal.GDT_Float64)
    if ds is not None:
        gdaltest.post_reason('fail')
        return 'fail'
    ds = None

    # Multiplication overflow
    with gdaltest.error_handler():
        ds = drv.Create('', 0x7FFFFFFF, 0x7FFFFFFF, 16)
    if ds is not None:
        gdaltest.post_reason('fail')
        return 'fail'
    ds = None

    # Multiplication overflow
    with gdaltest.error_handler():
        ds = drv.Create('', 0x7FFFFFFF, 0x7FFFFFFF, 1, gdal.GDT_Float64)
    if ds is not None:
        gdaltest.post_reason('fail')
        return 'fail'
    ds = None

    # Out of memory error
    with gdaltest.error_handler():
        ds = drv.Create('',
                        0x7FFFFFFF,
                        0x7FFFFFFF,
                        1,
                        options=['INTERLEAVE=PIXEL'])
    if ds is not None:
        gdaltest.post_reason('fail')
        return 'fail'
    ds = None

    # Out of memory error
    with gdaltest.error_handler():
        ds = drv.Create('', 0x7FFFFFFF, 0x7FFFFFFF, 1)
    if ds is not None:
        gdaltest.post_reason('fail')
        return 'fail'
    ds = None

    # 32 bit overflow on 32-bit builds, or possible out of memory error
    ds = drv.Create('', 0x7FFFFFFF, 1, 0)
    with gdaltest.error_handler():
        ds.AddBand(gdal.GDT_Float64)

    # Will raise out of memory error in all cases
    ds = drv.Create('', 0x7FFFFFFF, 0x7FFFFFFF, 0)
    with gdaltest.error_handler():
        ret = ds.AddBand(gdal.GDT_Float64)
    if ret == 0:
        gdaltest.post_reason('fail')
        return 'fail'

    return 'success'
Example #56
0
def mem_10():

    # Error case: building overview on a 0 band dataset
    ds = gdal.GetDriverByName('MEM').Create('', 1, 1, 0)
    with gdaltest.error_handler():
        ds.BuildOverviews('NEAR', [2])

    # Requesting overviews when they are not
    ds = gdal.GetDriverByName('MEM').Create('', 1, 1)
    if ds.GetRasterBand(1).GetOverviewCount() != 0:
        gdaltest.post_reason('fail')
        return 'fail'
    if ds.GetRasterBand(1).GetOverview(-1) is not None:
        gdaltest.post_reason('fail')
        return 'fail'
    if ds.GetRasterBand(1).GetOverview(0) is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    # Single band case
    ds = gdal.GetDriverByName('MEM').CreateCopy('', gdal.Open('data/byte.tif'))
    for i in range(2):
        ret = ds.BuildOverviews('NEAR', [2])
        if ret != 0:
            gdaltest.post_reason('fail')
            return 'fail'
        if ds.GetRasterBand(1).GetOverviewCount() != 1:
            gdaltest.post_reason('fail')
            return 'fail'
        cs = ds.GetRasterBand(1).GetOverview(0).Checksum()
        if cs != 1087:
            gdaltest.post_reason('fail')
            print(cs)
            return 'fail'

    ret = ds.BuildOverviews('NEAR', [4])
    if ret != 0:
        gdaltest.post_reason('fail')
        return 'fail'
    if ds.GetRasterBand(1).GetOverviewCount() != 2:
        gdaltest.post_reason('fail')
        return 'fail'
    cs = ds.GetRasterBand(1).GetOverview(0).Checksum()
    if cs != 1087:
        gdaltest.post_reason('fail')
        print(cs)
        return 'fail'
    cs = ds.GetRasterBand(1).GetOverview(1).Checksum()
    if cs != 328:
        gdaltest.post_reason('fail')
        print(cs)
        return 'fail'

    ret = ds.BuildOverviews('NEAR', [2, 4])
    if ret != 0:
        gdaltest.post_reason('fail')
        return 'fail'
    if ds.GetRasterBand(1).GetOverviewCount() != 2:
        gdaltest.post_reason('fail')
        return 'fail'
    cs = ds.GetRasterBand(1).GetOverview(0).Checksum()
    if cs != 1087:
        gdaltest.post_reason('fail')
        print(cs)
        return 'fail'
    cs = ds.GetRasterBand(1).GetOverview(1).Checksum()
    if cs != 328:
        gdaltest.post_reason('fail')
        print(cs)
        return 'fail'

    # Test that average in one or several steps give the same result
    ds.GetRasterBand(1).GetOverview(0).Fill(0)
    ds.GetRasterBand(1).GetOverview(1).Fill(0)

    ret = ds.BuildOverviews('AVERAGE', [2, 4])
    if ret != 0:
        gdaltest.post_reason('fail')
        return 'fail'
    if ds.GetRasterBand(1).GetOverviewCount() != 2:
        gdaltest.post_reason('fail')
        return 'fail'
    cs = ds.GetRasterBand(1).GetOverview(0).Checksum()
    if cs != 1152:
        gdaltest.post_reason('fail')
        print(cs)
        return 'fail'
    cs = ds.GetRasterBand(1).GetOverview(1).Checksum()
    if cs != 240:
        gdaltest.post_reason('fail')
        print(cs)
        return 'fail'

    ds.GetRasterBand(1).GetOverview(0).Fill(0)
    ds.GetRasterBand(1).GetOverview(1).Fill(0)

    ret = ds.BuildOverviews('AVERAGE', [2])
    ret = ds.BuildOverviews('AVERAGE', [4])
    if ret != 0:
        gdaltest.post_reason('fail')
        return 'fail'
    if ds.GetRasterBand(1).GetOverviewCount() != 2:
        gdaltest.post_reason('fail')
        return 'fail'
    cs = ds.GetRasterBand(1).GetOverview(0).Checksum()
    if cs != 1152:
        gdaltest.post_reason('fail')
        print(cs)
        return 'fail'
    cs = ds.GetRasterBand(1).GetOverview(1).Checksum()
    if cs != 240:
        gdaltest.post_reason('fail')
        print(cs)
        return 'fail'

    ds = None

    # Multiple band case
    ds = gdal.GetDriverByName('MEM').CreateCopy('',
                                                gdal.Open('data/rgbsmall.tif'))
    ret = ds.BuildOverviews('NEAR', [2])
    if ret != 0:
        gdaltest.post_reason('fail')
        return 'fail'
    cs = ds.GetRasterBand(1).GetOverview(0).Checksum()
    if cs != 5057:
        gdaltest.post_reason('fail')
        print(cs)
        return 'fail'
    cs = ds.GetRasterBand(2).GetOverview(0).Checksum()
    if cs != 5304:
        gdaltest.post_reason('fail')
        print(cs)
        return 'fail'
    cs = ds.GetRasterBand(3).GetOverview(0).Checksum()
    if cs != 5304:
        gdaltest.post_reason('fail')
        print(cs)
        return 'fail'
    ds = None

    # Clean overviews
    ds = gdal.GetDriverByName('MEM').CreateCopy('', gdal.Open('data/byte.tif'))
    ret = ds.BuildOverviews('NEAR', [2])
    if ret != 0:
        gdaltest.post_reason('fail')
        return 'fail'
    ret = ds.BuildOverviews('NONE', [])
    if ret != 0:
        gdaltest.post_reason('fail')
        return 'fail'
    if ds.GetRasterBand(1).GetOverviewCount() != 0:
        gdaltest.post_reason('fail')
        return 'fail'
    ds = None

    return 'success'
Example #57
0
def bag_vr_list_supergrids():

    if gdaltest.bag_drv is None:
        return 'skip'

    ds = gdal.OpenEx('data/test_vr.bag', open_options=['MODE=LIST_SUPERGRIDS'])
    if ds is None:
        gdaltest.post_reason('fail')
        return 'fail'
    sub_ds = ds.GetSubDatasets()
    if len(sub_ds) != 24:
        gdaltest.post_reason('fail')
        print(len(sub_ds))
        print(sub_ds)
        return 'fail'

    if sub_ds[0][0] != 'BAG:"data/test_vr.bag":supergrid:0:0':
        gdaltest.post_reason('fail')
        print(sub_ds)
        return 'fail'

    with gdaltest.error_handler():
        # Bounding box filter ignored since only part of MINX, MINY, MAXX and
        # MAXY has been specified
        ds = gdal.OpenEx('data/test_vr.bag',
                         open_options=['MODE=LIST_SUPERGRIDS', 'MINX=200'])
    sub_ds = ds.GetSubDatasets()
    if len(sub_ds) != 24:
        gdaltest.post_reason('fail')
        print(len(sub_ds))
        print(sub_ds)
        return 'fail'

    ds = gdal.OpenEx('data/test_vr.bag',
                     open_options=[
                         'MODE=LIST_SUPERGRIDS', 'MINX=100', 'MAXX=220',
                         'MINY=500000', 'MAXY=500100'
                     ])
    sub_ds = ds.GetSubDatasets()
    if len(sub_ds) != 6:
        gdaltest.post_reason('fail')
        print(len(sub_ds))
        print(sub_ds)
        return 'fail'
    if sub_ds[0][0] != 'BAG:"data/test_vr.bag":supergrid:1:1':
        gdaltest.post_reason('fail')
        print(sub_ds)
        return 'fail'

    ds = gdal.OpenEx('data/test_vr.bag',
                     open_options=[
                         'MODE=LIST_SUPERGRIDS', 'RES_FILTER_MIN=5',
                         'RES_FILTER_MAX=10'
                     ])
    sub_ds = ds.GetSubDatasets()
    if len(sub_ds) != 12:
        gdaltest.post_reason('fail')
        print(len(sub_ds))
        print(sub_ds)
        return 'fail'
    if sub_ds[0][0] != 'BAG:"data/test_vr.bag":supergrid:0:3':
        gdaltest.post_reason('fail')
        print(sub_ds)
        return 'fail'

    ds = gdal.OpenEx('data/test_vr.bag',
                     open_options=['SUPERGRIDS_INDICES=(2,1),(3,4)'])
    sub_ds = ds.GetSubDatasets()
    if len(sub_ds) != 2:
        gdaltest.post_reason('fail')
        print(len(sub_ds))
        print(sub_ds)
        return 'fail'
    if sub_ds[0][0] != 'BAG:"data/test_vr.bag":supergrid:2:1' or \
       sub_ds[1][0] != 'BAG:"data/test_vr.bag":supergrid:3:4':
        gdaltest.post_reason('fail')
        print(sub_ds)
        return 'fail'

    # One single tuple: open the subdataset directly
    ds = gdal.OpenEx('data/test_vr.bag',
                     open_options=['SUPERGRIDS_INDICES=(2,1)'])
    ds2 = gdal.Open('BAG:"data/test_vr.bag":supergrid:2:1')
    if gdal.Info(ds) != gdal.Info(ds2):
        gdaltest.post_reason('fail')
        print(sub_ds)
        return 'fail'

    # Test invalid values for SUPERGRIDS_INDICES
    for invalid_val in [
            '', 'x', '(', '(1', '(1,', '(1,)', '(1,2),', '(x,2)', '(2,x)'
    ]:
        with gdaltest.error_handler():
            ds = gdal.OpenEx(
                'data/test_vr.bag',
                open_options=['SUPERGRIDS_INDICES=' + invalid_val])
        if gdal.GetLastErrorMsg() == '':
            gdaltest.post_reason('fail')
            print(invalid_val)
            return 'fail'

    return 'success'
Example #58
0
def test_vsifile_14():

    with gdaltest.error_handler():
        gdal.VSIFOpenL(
            '/vsitar//vsitar//vsitar//vsitar//vsitar//vsitar//vsitar//vsitar/a.tgzb.tgzc.tgzd.tgze.tgzf.tgz.h.tgz.i.tgz',
            'rb')
Example #59
0
def vsicrypt_6():

    try:
        import ctypes
    except ImportError:
        return 'skip'
    import testnonboundtoswig

    testnonboundtoswig.testnonboundtoswig_init()

    if testnonboundtoswig.gdal_handle is None:
        return 'skip'

    testnonboundtoswig.gdal_handle.VSISetCryptKey.argtypes = [
        ctypes.c_char_p, ctypes.c_int
    ]
    testnonboundtoswig.gdal_handle.VSISetCryptKey.restype = None

    # Set a valid key
    testnonboundtoswig.gdal_handle.VSISetCryptKey(
        'DONT_USE_IN_PROD'.encode('ASCII'), 16)

    if not gdaltest.has_vsicrypt:
        return 'skip'

    fp = gdal.VSIFOpenL('/vsicrypt/add_key_check=yes,file=/vsimem/file.bin',
                        'wb+')
    if fp is None:
        gdaltest.post_reason('fail')
        return 'fail'
    gdal.VSIFWriteL('hello', 1, 5, fp)
    gdal.VSIFCloseL(fp)

    fp = gdal.VSIFOpenL('/vsicrypt//vsimem/file.bin', 'rb')
    content = gdal.VSIFReadL(1, 5, fp).decode('latin1')
    gdal.VSIFCloseL(fp)

    if content != 'hello':
        gdaltest.post_reason('fail')
        return 'fail'

    fp = gdal.VSIFOpenL('/vsicrypt//vsimem/file.bin', 'wb+')
    if fp is None:
        gdaltest.post_reason('fail')
        return 'fail'
    gdal.VSIFWriteL('hello', 1, 5, fp)
    gdal.VSIFCloseL(fp)

    fp = gdal.VSIFOpenL('/vsicrypt//vsimem/file.bin', 'rb')
    content = gdal.VSIFReadL(1, 5, fp).decode('latin1')
    gdal.VSIFCloseL(fp)

    if content != 'hello':
        gdaltest.post_reason('fail')
        return 'fail'

    # Set a too short key
    testnonboundtoswig.gdal_handle.VSISetCryptKey('bbc'.encode('ASCII'), 3)
    with gdaltest.error_handler():
        fp = gdal.VSIFOpenL('/vsicrypt//vsimem/file.bin', 'rb')
    if fp is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    with gdaltest.error_handler():
        fp = gdal.VSIFOpenL('/vsicrypt//vsimem/file.bin', 'wb+')
    if fp is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    # Erase key
    testnonboundtoswig.gdal_handle.VSISetCryptKey(None, 0)
    with gdaltest.error_handler():
        fp = gdal.VSIFOpenL('/vsicrypt//vsimem/file.bin', 'wb+')
    if fp is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    gdal.Unlink('/vsimem/file.bin')

    return 'success'
Example #60
0
def test_ogr_wfs3_errors():
    if gdaltest.wfs3_drv is None:
        pytest.skip()

    if gdaltest.webserver_port == 0:
        pytest.skip()

    handler = webserver.SequentialHandler()
    handler.add('GET', '/wfs3/collections', 404)
    with webserver.install_http_handler(handler):
        with gdaltest.error_handler():
            ds = ogr.Open('WFS3:http://localhost:%d/wfs3' % gdaltest.webserver_port)
    assert ds is None

    # No Content-Type
    handler = webserver.SequentialHandler()
    handler.add('GET', '/wfs3/collections', 200, {}, 'foo')
    with webserver.install_http_handler(handler):
        with gdaltest.error_handler():
            ds = ogr.Open('WFS3:http://localhost:%d/wfs3' % gdaltest.webserver_port)
    assert ds is None

    # Unexpected Content-Type
    handler = webserver.SequentialHandler()
    handler.add('GET', '/wfs3/collections', 200,
                {'Content-Type': 'text/html'}, 'foo')
    with webserver.install_http_handler(handler):
        with gdaltest.error_handler():
            ds = ogr.Open('WFS3:http://localhost:%d/wfs3' % gdaltest.webserver_port)
    assert ds is None

    # Invalid JSON
    handler = webserver.SequentialHandler()
    handler.add('GET', '/wfs3/collections', 200,
                {'Content-Type': 'application/json'}, 'foo bar')
    with webserver.install_http_handler(handler):
        with gdaltest.error_handler():
            ds = ogr.Open('WFS3:http://localhost:%d/wfs3' % gdaltest.webserver_port)
    assert ds is None

    # Valid JSON but not collections array
    handler = webserver.SequentialHandler()
    handler.add('GET', '/wfs3/collections', 200,
                {'Content-Type': 'application/json'}, '{}')
    with webserver.install_http_handler(handler):
        with gdaltest.error_handler():
            ds = ogr.Open('WFS3:http://localhost:%d/wfs3' % gdaltest.webserver_port)
    assert ds is None

    # Valid JSON but collections is not an array
    handler = webserver.SequentialHandler()
    handler.add('GET', '/wfs3/collections', 200,
                {'Content-Type': 'application/json'},
                '{ "collections" : null }')
    with webserver.install_http_handler(handler):
        with gdaltest.error_handler():
            ds = ogr.Open('WFS3:http://localhost:%d/wfs3' % gdaltest.webserver_port)
    assert ds is None

    handler = webserver.SequentialHandler()
    handler.add('GET', '/wfs3/collections', 200,
                {'Content-Type': 'application/json'},
                '{ "collections" : [ null, {} ] }')
    with webserver.install_http_handler(handler):
        ds = ogr.Open('WFS3:http://localhost:%d/wfs3' % gdaltest.webserver_port)
    assert ds is not None
    assert ds.GetLayerCount() == 0
    assert ds.GetLayer(-1) is None
    assert ds.GetLayer(0) is None