Exemplo n.º 1
0
def test_http_4():
    # Too unreliable
    if gdaltest.skip_on_travis():
        pytest.skip()

    url = 'ftp://download.osgeo.org/gdal/data/gtiff/utm.tif'
    ds = gdal.Open('/vsicurl/' + url)
    if ds is None:
        skip_if_unreachable(url, try_read=True)
        if sys.platform == 'darwin' and gdal.GetConfigOption('TRAVIS',
                                                             None) is not None:
            pytest.skip("Fails on MacOSX Travis sometimes. Not sure why.")
        pytest.fail()

    filelist = ds.GetFileList()
    assert '/vsicurl/ftp://download.osgeo.org/gdal/data/gtiff/utm.tif' in filelist
Exemplo n.º 2
0
Arquivo: mem.py Projeto: kongdd/gdal
def test_mem_6():

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

    drv = gdal.GetDriverByName('MEM')

    # Multiplication overflow
    with gdaltest.error_handler():
        ds = drv.Create('', 1, 1, 0x7FFFFFFF, gdal.GDT_Float64)
    assert ds is None
    ds = None

    # Multiplication overflow
    with gdaltest.error_handler():
        ds = drv.Create('', 0x7FFFFFFF, 0x7FFFFFFF, 16)
    assert ds is None
    ds = None

    # Multiplication overflow
    with gdaltest.error_handler():
        ds = drv.Create('', 0x7FFFFFFF, 0x7FFFFFFF, 1, gdal.GDT_Float64)
    assert ds is None
    ds = None

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

    # Out of memory error
    with gdaltest.error_handler():
        ds = drv.Create('', 0x7FFFFFFF, 0x7FFFFFFF, 1)
    assert ds is None
    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)
    assert ret != 0
Exemplo n.º 3
0
    def testSetMetadata(self):
        if self.testDriver() == 'fail':
            return 'skip'

        src_ds = gdal.Open('data/' + self.filename)
        xsize = src_ds.RasterXSize
        ysize = src_ds.RasterYSize

        new_filename = 'tmp/' + self.filename + '.tst'
        new_ds = self.driver.Create(new_filename,
                                    xsize,
                                    ysize,
                                    1,
                                    src_ds.GetRasterBand(self.band).DataType,
                                    options=self.options)
        if new_ds is None:
            post_reason('Failed to create test file using Create method.')
            return 'fail'

        dict = {}
        dict['TEST_KEY'] = 'TestValue'
        new_ds.SetMetadata(dict)
        # FIXME
        #if new_ds.SetMetadata( dict ) is not gdal.CE_None:
        #print new_ds.SetMetadata( dict )
        #post_reason( 'Failed to set metadata item.' )
        #return 'fail'

        src_ds = None
        new_ds = None

        new_ds = gdal.Open(new_filename)
        if new_ds is None:
            post_reason('Failed to open dataset: ' + new_filename)
            return 'fail'

        md_dict = new_ds.GetMetadata()
        if md_dict['TEST_KEY'] != 'TestValue':
            post_reason('Did not get expected metadata item.')
            return 'fail'

        new_ds = None

        if gdal.GetConfigOption('CPL_DEBUG', 'OFF') != 'ON':
            self.driver.Delete(new_filename)

        return 'success'
Exemplo n.º 4
0
    def create_cog(img, factors=[2, 4, 8, 16, 32], compression='NONE'):
        # Define a new configuration, save the previous configuration,
        # and then apply the new one.
        new_config = {
            'GDAL_CACHEMAX': '8192',
            'COMPRESS_OVERVIEW': 'NONE'  # Compression will be made in the next step
        }
        prev_config = {
            key: gdal.GetConfigOption(key) for key in new_config.keys()}
        for key, val in new_config.items():
            gdal.SetConfigOption(key, val)

        InputImage = str(img)
        Image = gdal.Open(InputImage, 1)  # 0 = read-only, 1 = read-write.
        Image.BuildOverviews("NEAREST", factors)
        del Image

        # Restore previous configuration.
        for key, val in prev_config.items():
            gdal.SetConfigOption(key, val)


        # Run gdal_translate to properly tile and compress gtiff file after overviews have been added
        # Note: Should not be necessary, but the current version of gdal destroys tiles when adding overviews
        img_tmp = Path(img.parent, 'temp' + img.suffix)
        img.rename(img_tmp)

        ds = gdal.Open(str(img_tmp))  # img is a Path object but gdal requires a string, therefore str(img)
        # NOTE: ZSTD and WEBP compression is not working here
        # (https://lists.osgeo.org/pipermail/gdal-dev/2018-November/049289.html)
        if compression == 'NONE':
            creation_options = ['TILED=YES', 'COPY_SRC_OVERVIEWS=YES', 'BLOCKXSIZE=512', 'BLOCKYSIZE=512',
                                'NUM_THREADS=ALL_CPUS', 'BIGTIFF=IF_SAFER']
        elif compression == 'DEFLATE':
            creation_options = ['COMPRESS=DEFLATE', 'TILED=YES', 'COPY_SRC_OVERVIEWS=YES',
                                'BLOCKXSIZE=512', 'BLOCKYSIZE=512', 'NUM_THREADS=ALL_CPUS', 'BIGTIFF=IF_SAFER']
        elif compression == 'JPEG':
            creation_options = ['COMPRESS=JPEG', 'PHOTOMETRIC=YCBCR', 'JPEG_QUALITY=90', 'TILED=YES',
                                'COPY_SRC_OVERVIEWS=YES', 'BLOCKXSIZE=512', 'BLOCKYSIZE=512', 'NUM_THREADS=ALL_CPUS',
                                'BIGTIFF=IF_SAFER']
        config_options = ['GDAL_CACHEMAX=8192']
        translate_options = gdal.TranslateOptions(creationOptions=creation_options)
        ds = gdal.Translate(str(img), ds, options=translate_options, config_options=config_options)
        ds = None

        os.remove(img_tmp)
Exemplo n.º 5
0
def sieve_8():

    gdal.FileFromMemBuffer('/vsimem/sieve_8.asc',
"""ncols        7
nrows        7
xllcorner    440720.000000000000
yllcorner    3750120.000000000000
cellsize     60.000000000000
 0 0 0 0 0 0 0
 0 5 5 0 0 0 0
 0 5 2 3 4 0 0
 0 0 8 1 5 0 0
 0 0 7 6 5 9 0
 0 0 0 0 9 9 0
 0 0 0 0 0 0 0
 """)


    drv = gdal.GetDriverByName( 'GTiff' )
    src_ds = gdal.Open('/vsimem/sieve_8.asc')
    src_band = src_ds.GetRasterBand(1)

    dst_ds = drv.Create('/vsimem/sieve_8.tif', 7, 7, 1, gdal.GDT_Byte )
    dst_band = dst_ds.GetRasterBand(1)

    gdal.SieveFilter( src_band, src_band.GetMaskBand(), dst_band, 4, 4 )

    # All non 0 should be mapped to 0
    cs_expected = 0
    cs = dst_band.Checksum()

    dst_band = None
    dst_ds = None

    gdal.Unlink('/vsimem/sieve_8.asc')

    if cs == cs_expected \
       or gdal.GetConfigOption( 'CPL_DEBUG', 'OFF' ) != 'ON':
        drv.Delete( '/vsimem/sieve_8.tif' )

    if cs != cs_expected:
        print('Got: ', cs)
        gdaltest.post_reason( 'got wrong checksum' )
        return 'fail'
    else:
        return 'success'
Exemplo n.º 6
0
def virtualmem_3():

    if gdal.GetConfigOption('SKIP_VIRTUALMEM'):
        return 'skip'
    try:
        from osgeo import gdalnumeric
    except ImportError:
        return 'skip'

    if not sys.platform.startswith('linux'):
        return 'skip'

    for tmpfile in ['tmp/virtualmem_3.img', '/vsimem/virtualmem_3.img']:
        ds = gdal.GetDriverByName('EHdr').Create(tmpfile, 400, 300, 2)
        ar1 = ds.GetRasterBand(1).GetVirtualMemAutoArray(gdal.GF_Write)
        ar2 = ds.GetRasterBand(2).GetVirtualMemAutoArray(gdal.GF_Write)
        for y in range(ds.RasterYSize):
            ar1[y].fill(127)
            ar2[y].fill(255)
        # We need to destroy the array before dataset destruction
        ar1 = None
        ar2 = None
        ds = None

        ds = gdal.Open(tmpfile)
        ar1 = ds.GetRasterBand(1).GetVirtualMemAutoArray(gdal.GF_Read)
        ar2 = ds.GetRasterBand(2).GetVirtualMemAutoArray(gdal.GF_Read)
        ar_127 = gdalnumeric.empty(ds.RasterXSize)
        ar_127.fill(127)
        ar_255 = gdalnumeric.empty(ds.RasterXSize)
        ar_255.fill(255)
        for y in range(ds.RasterYSize):
            if not gdalnumeric.array_equal(ar1[y], ar_127):
                gdaltest.post_reason('fail')
                return 'fail'
            if not gdalnumeric.array_equal(ar2[y], ar_255):
                gdaltest.post_reason('fail')
                return 'fail'
        # We need to destroy the array before dataset destruction
        ar1 = None
        ar2 = None
        ds = None

        gdal.GetDriverByName('EHdr').Delete(tmpfile)

    return 'success'
Exemplo n.º 7
0
def test_gdalwarp_lib_cleanup():

    # We don't clean up when run in debug mode.
    if gdal.GetConfigOption('CPL_DEBUG', 'OFF') == 'ON':
        return 'success'

    for i in range(2):
        try:
            os.remove('tmp/testgdalwarp' + str(i + 1) + '.tif')
        except:
            pass
    try:
        os.remove('tmp/testgdalwarp_gcp.tif')
    except:
        pass

    return 'success'
Exemplo n.º 8
0
 def wrapper(*args, **kwargs):
     gdal_version_too_low = int(gdal.VersionInfo()) < 2000000
     # if it was already off prior to entering this function it means
     # that something has turned this option off. This means that we don't
     # have to set it to OFF ourselves, but moreover, we don't want to
     # mess with the setting and turn it back on (this is somewhat
     # similar to a re-entrant lock).
     initial_option_value = gdal.GetConfigOption("OGR_SQLITE_SYNCHRONOUS")
     already_off = initial_option_value == "OFF"
     if gdal_version_too_low and not already_off:
         gdal.SetConfigOption("OGR_SQLITE_SYNCHRONOUS", "OFF")
         retval = func(*args, **kwargs)
         gdal.SetConfigOption("OGR_SQLITE_SYNCHRONOUS", initial_option_value)
     else:
         # business as usual
         retval = func(*args, **kwargs)
     return retval
def test_ili_to_postgis():
    # ogr genconfig --format PostgreSQL tests/data/np/NP_Example.xtf,tests/data/np/NP_73_CH_de_ili2.imd --model tests/data/np/NP_73_CH_de_ili2.imd --srs=EPSG:21781 >tests/data/np/NP_73_CH_de_ili2-pg.cfg
    option = gdal.GetConfigOption('OGR_STROKE_CURVE')
    gdal.SetConfigOption('OGR_STROKE_CURVE', 'True')
    trans = OgrConfig(
        config="tests/data/np/NP_73_CH_de_ili2-pg.cfg",
        ds="tests/data/np/NP_Example.xtf,tests/data/np/NP_73_CH_de_ili2.imd")
    __, dstfile = tempfile.mkstemp(suffix='.sql')
    trans.transform(dstfile, "PGDump")
    print(dstfile)
    sql = codecs.open(dstfile, encoding='utf-8').read()
    assert """CREATE TABLE "public"."n0_grundnutzung_zonenflaeche" (""" in sql
    assert """SELECT AddGeometryColumn('public','n0_grundnutzung_zonenflaeche','geometrie',21781,'POLYGON',2);""" in sql
    assert """INSERT INTO "public"."n0_grundnutzung_zonenflaeche" ("geometrie" ,""" in sql
    assert '''01030000201555000001000000550000007D3F355ECA312541FA7E6ABCDD2E0E41355EBAC9923125411904560EC72D0E41EE7C3FB5923125412DB29DEF9D2C0E4154E3A55B80312541713D0AD7472C0E41A4703DCA7731254183C0CAA11F2C0E414C37898171312541F4FDD478F92B0E414260E5506E3125413D0AD7A3022C0E413F355EFA68312541BC749318122C0E4121B072A8633125416DE7FBA9212C0E4154E3A55B5E31254193180456312C0E41D9CEF7135931254177BE9F1A412C0E414260E5D053312541D122DBF9502C0E418D976E924E3125415C8FC2F5602C0E41448B6CE74A3125411D5A643B6C2C0E419A99995949312541E9263108712C0E418941602544312541A8C64B37812C0E41CBA145F63E31254123DBF97E912C0E415EBA49CC393125415A643BDFA12C0E41448B6CA73431254108AC1C5AB22C0E417B14AE872F3125412DB29DEFC22C0E4104560E6D2A3125410E2DB29DD32C0E41DF4F8D5725312541AC1C5A64E42C0E415839B4C8073125411904560E472D0E41D122DBF9F030254117D9CEF7952D0E410AD7A3B0D83025411F85EB51EE2D0E41E5D0221BAC302541022B87169C2E0E41273108ECA7302541BE9F1A2FAD2E0E41AE47E1FAA6302541D34D6210B12E0E41E9263188A23025417F6ABC74C32E0E41C3F5281C9E3025412DB29DEFD52E0E415C8FC2B599302541DD240681E82E0E41AE47E1FA97302541E5D022DBEF2E0E41B6F3FD54953025414A0C022BFB2E0E413F355EFA90302541FED478E90D2F0E41F853E3A58C302541B4C876BE202F0E41DF4F8D57883025416DE7FBA9332F0E41F853E3E574302541560E2DB2912F0E416F1283C08930254125068195DD2F0E41F4FDD4F8963025411283C0CA0C300E411F85EB519E3025416210583928300E41CDCCCCCCB83025417F6ABC7488300E41333333B3DC302541759318040F310E41B4C8763EE6302541D122DBF9F5300E411283C0CA2A312541EC51B81E42300E41C520B0B230312541C976BE9F32300E413D0AD7633A312541D122DBF988300E41378941E0BD3125414260E5D092320E4114AE4721C2312541931804567C320E41D34D6210E5312541CBA145B6C3310E419EEFA786E6312541BC749318BC310E41986E1203E8312541F6285C8FB4310E41C1CAA185E931254177BE9F1AAD310E418716D90EEB3125413F355EBAA5310E41EC51B89EEC3125410AD7A3709E310E4110583934EE3125411D5A643B97310E41643BDFCFEF31254177BE9F1A90310E41E7FBA971F13125418D976E1289310E419A999919F3312541EC51B81E82310E410C022BC7F4312541068195437B310E41AE47E17AF63125416891ED7C74310E4110583934F83125414260E5D06D310E41C520B0F2F93125416210583967310E41A8C64BB7FB312541FA7E6ABC60310E414C378981FD312541931804565A310E41986E1283FD312541643BDF4F5A310E414260E550FF312541E926310854310E418941602501322541B6F3FDD44D310E4191ED7CFF0232254185EB51B847310E41EC51B8DE0432254185EB51B841310E41986E12C3063225418716D9CE3B310E41273108AC08322541BA490C0236310E4108AC1C9A0A322541AAF1D24D30310E413BDF4F8D0C322541105839B42A310E4152B81E850E322541EE7C3F3525310E414C378981103225414260E5D01F310E41BA490C8212322541C74B37891A310E417B14AE8714322541C3F5285C15310E41B072689116322541F0A7C64B10310E415A643B9F18322541931804560B310E41E7FBA9B11A3225416891ED7C06310E415C8FC2B51A322541C520B07206310E417B14AEC72F322541A8C64B37DA300E41759318C43832254196438B6CCB300E41F0A7C60B2D3225417593180497300E417D3F355ECA312541FA7E6ABCDD2E0E41''' in sql
    os.remove(dstfile)
    gdal.SetConfigOption('OGR_STROKE_CURVE', option)
Exemplo n.º 10
0
def test_http_ssl_verifystatus():

    if gdal.GetDriverByName('HTTP') is None:
        pytest.skip()

    with gdaltest.config_option('GDAL_HTTP_SSL_VERIFYSTATUS', 'YES'):
        with gdaltest.error_handler():
            # For now this URL doesn't support OCSP stapling...
            gdal.OpenEx('https://google.com', allowed_drivers=['HTTP'])
    last_err = gdal.GetLastErrorMsg()
    if 'No OCSP response received' not in last_err and 'libcurl too old' not in last_err:

        # The test actually works on Travis Mac
        if sys.platform == 'darwin' and gdal.GetConfigOption('TRAVIS', None) is not None:
            pytest.skip()

        pytest.fail(last_err)
Exemplo n.º 11
0
def ogr_mssqlspatial_1():

    gdaltest.mssqlspatial_ds = None

    if ogr.GetDriverByName('MSSQLSpatial') is None:
        return 'skip'

    gdaltest.mssqlspatial_dsname = gdal.GetConfigOption(
        'OGR_MSSQL_CONNECTION_STRING',
        # localhost doesn't work under chroot
        'MSSQL:server=127.0.0.1;database=TestDB;driver=ODBC Driver 17 for SQL Server;UID=SA;PWD=DummyPassw0rd'
    )
    gdaltest.mssqlspatial_ds = ogr.Open(gdaltest.mssqlspatial_dsname, update=1)
    if gdaltest.mssqlspatial_ds is not None:
        return 'success'

    return 'skip'
Exemplo n.º 12
0
def vsifile_7():

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

    # Test extending file beyond reasonable limits in write mode
    fp = gdal.VSIFOpenL('/vsimem/vsifile_7.bin', 'wb')
    if gdal.VSIFSeekL(fp, 0x7FFFFFFFFFFFFFFF, 0) != 0:
        gdaltest.post_reason('fail')
        return 'fail'
    if gdal.VSIStatL('/vsimem/vsifile_7.bin').size != 0:
        gdaltest.post_reason('fail')
        return 'fail'
    gdal.PushErrorHandler()
    ret = gdal.VSIFWriteL('a', 1, 1, fp)
    gdal.PopErrorHandler()
    if ret != 0:
        gdaltest.post_reason('fail')
        return 'fail'
    if gdal.VSIStatL('/vsimem/vsifile_7.bin').size != 0:
        gdaltest.post_reason('fail')
        return 'fail'
    gdal.VSIFCloseL(fp)

    # Test seeking  beyond file size in read-only mode
    fp = gdal.VSIFOpenL('/vsimem/vsifile_7.bin', 'rb')
    if gdal.VSIFSeekL(fp, 0x7FFFFFFFFFFFFFFF, 0) != 0:
        gdaltest.post_reason('fail')
        return 'fail'
    if gdal.VSIFEofL(fp) != 0:
        gdaltest.post_reason('fail')
        return 'fail'
    if gdal.VSIFTellL(fp) != 0x7FFFFFFFFFFFFFFF:
        gdaltest.post_reason('fail')
        return 'fail'
    if gdal.VSIFReadL(1, 1, fp):
        gdaltest.post_reason('fail')
        return 'fail'
    if gdal.VSIFEofL(fp) != 1:
        gdaltest.post_reason('fail')
        return 'fail'
    gdal.VSIFCloseL(fp)

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

    return 'success'
Exemplo n.º 13
0
def test_cog_small_world_to_web_mercator():

    tab = [ 0 ]
    def my_cbk(pct, _, arg):
        assert pct >= tab[0]
        tab[0] = pct
        return 1

    directory = '/vsimem/test_cog_small_world_to_web_mercator'
    gdal.Mkdir(directory, 0o755)
    filename = directory + '/cog.tif'
    src_ds = gdal.Open('../gdrivers/data/small_world.tif')
    ds = gdal.GetDriverByName('COG').CreateCopy(filename, src_ds,
        options = ['TILING_SCHEME=GoogleMapsCompatible', 'COMPRESS=JPEG'],
        callback = my_cbk,
        callback_data = tab)
    assert tab[0] == 1.0
    assert ds
    assert len(gdal.ReadDir(directory)) == 1 # check that the temp file has gone away

    ds = None
    ds = gdal.Open(filename)
    assert ds.RasterCount == 3
    assert ds.RasterXSize == 256
    assert ds.RasterYSize == 256
    assert ds.GetRasterBand(1).GetMaskFlags() == gdal.GMF_PER_DATASET
    assert ds.GetRasterBand(1).GetBlockSize() == [256, 256]
    gt = ds.GetGeoTransform()
    expected_gt = [-20037508.342789248, 156543.033928041, 0.0,
                   20037508.342789248, 0.0, -156543.033928041]
    for i in range(6):
        if gt[i] != pytest.approx(expected_gt[i], abs=1e-10 * abs(expected_gt[i])):
            assert False, gt
    got_cs = [ds.GetRasterBand(i+1).Checksum() for i in range(3)]
    if sys.platform == 'darwin' and gdal.GetConfigOption('TRAVIS', None) is not None:
        assert got_cs != [0, 0, 0]
    else:
        assert got_cs == [26293, 23439, 14955]
    assert ds.GetRasterBand(1).GetMaskBand().Checksum() == 17849
    assert ds.GetRasterBand(1).GetOverviewCount() == 0
    ds = None
    _check_cog(filename)

    src_ds = None
    gdal.GetDriverByName('GTiff').Delete(filename)
    gdal.Unlink(directory)
Exemplo n.º 14
0
def vsis3_init():

    gdaltest.aws_vars = {}
    for var in ('AWS_SECRET_ACCESS_KEY', 'AWS_ACCESS_KEY_ID', 'AWS_TIMESTAMP',
                'AWS_HTTPS', 'AWS_VIRTUAL_HOSTING', 'AWS_S3_ENDPOINT',
                'AWS_REQUEST_PAYER', 'AWS_DEFAULT_REGION',
                'AWS_DEFAULT_PROFILE'):
        gdaltest.aws_vars[var] = gdal.GetConfigOption(var)
        if gdaltest.aws_vars[var] is not None:
            gdal.SetConfigOption(var, "")

    # To avoid user AWS credentials in ~/.aws/credentials and ~/.aws/config
    # to mess up our tests
    gdal.SetConfigOption('CPL_AWS_CREDENTIALS_FILE', '')
    gdal.SetConfigOption('AWS_CONFIG_FILE', '')
    gdal.SetConfigOption('CPL_AWS_EC2_CREDENTIALS_URL', '')

    return 'success'
Exemplo n.º 15
0
Arquivo: vsigs.py Projeto: plkms/gdal
def vsigs_init():

    gdaltest.gs_vars = {}
    for var in ('GS_SECRET_ACCESS_KEY', 'GS_ACCESS_KEY_ID', 'CPL_GS_TIMESTAMP',
                'CPL_GS_ENDPOINT', 'GDAL_HTTP_HEADER_FILE'):
        gdaltest.gs_vars[var] = gdal.GetConfigOption(var)
        if gdaltest.gs_vars[var] is not None:
            gdal.SetConfigOption(var, "")

    # To avoid user credentials in ~/.boto
    # to mess up our tests
    gdal.SetConfigOption('CPL_GS_CREDENTIALS_FILE', '')
    gdal.SetConfigOption('GS_OAUTH2_REFRESH_TOKEN', '')
    gdal.SetConfigOption('GS_OAUTH2_CLIENT_EMAIL', '')
    gdal.SetConfigOption('GS_OAUTH2_CLIENT_SECRET', '')
    gdal.SetConfigOption('GS_OAUTH2_CLIENT_ID', '')

    return 'success'
Exemplo n.º 16
0
def startup_and_cleanup():

    # Unset all env vars that could influence the tests
    az_vars = {}
    for var, reset_val in (
                ('AZURE_STORAGE_CONNECTION_STRING', None),
                ('AZURE_STORAGE_ACCOUNT', None),
                ('AZURE_STORAGE_ACCESS_KEY', None),
                ('AZURE_STORAGE_SAS_TOKEN', None),
                ('AZURE_NO_SIGN_REQUEST', None),
                ('AZURE_CONFIG_DIR', ''),
                ('AZURE_STORAGE_ACCESS_TOKEN', '')):
        az_vars[var] = gdal.GetConfigOption(var)
        gdal.SetConfigOption(var, reset_val)

    with gdaltest.config_option('CPL_AZURE_VM_API_ROOT_URL', 'disabled'):
        assert gdal.GetSignedURL('/vsiadls/foo/bar') is None

    gdaltest.webserver_process = None
    gdaltest.webserver_port = 0

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

    (gdaltest.webserver_process, gdaltest.webserver_port) = webserver.launch(handler=webserver.DispatcherHttpHandler)
    if gdaltest.webserver_port == 0:
        pytest.skip()

    gdal.SetConfigOption('AZURE_STORAGE_CONNECTION_STRING',
                         'DefaultEndpointsProtocol=http;AccountName=myaccount;AccountKey=MY_ACCOUNT_KEY;BlobEndpoint=http://127.0.0.1:%d/azure/blob/myaccount' % gdaltest.webserver_port)
    gdal.SetConfigOption('AZURE_STORAGE_ACCOUNT', '')
    gdal.SetConfigOption('AZURE_STORAGE_ACCESS_KEY', '')
    gdal.SetConfigOption('CPL_AZURE_TIMESTAMP', 'my_timestamp')

    yield

    # Clearcache needed to close all connections, since the Python server
    # can only handle one connection at a time
    gdal.VSICurlClearCache()

    webserver.server_stop(gdaltest.webserver_process, gdaltest.webserver_port)

    for var in az_vars:
        gdal.SetConfigOption(var, az_vars[var])
Exemplo n.º 17
0
def test_gdalwarp_cleanup():

    # We don't clean up when run in debug mode.
    if gdal.GetConfigOption('CPL_DEBUG', 'OFF') == 'ON':
        return 'success'

    for i in range(37):
        try:
            os.remove('tmp/testgdalwarp' + str(i + 1) + '.tif')
        except:
            pass
        try:
            os.remove('tmp/testgdalwarp' + str(i + 1) + '.vrt')
        except:
            pass
        try:
            os.remove('tmp/testgdalwarp' + str(i + 1) + '.tif.aux.xml')
        except:
            pass
    try:
        os.remove('tmp/testgdalwarp_gcp.tif')
    except:
        pass
    try:
        os.remove('tmp/testgdalwarp24src.tif')
    except:
        pass
    try:
        os.remove('tmp/testgdalwarp24dst.tif')
    except:
        pass
    try:
        os.remove('tmp/testgdalwarp30_1.tif')
    except:
        pass
    try:
        os.remove('tmp/testgdalwarp30_2.tif')
    except:
        pass
    try:
        os.remove('tmp/testgdalwarp33_mask.tif')
    except:
        pass
    return 'success'
Exemplo n.º 18
0
def test_gdal_ls_py_7():

    # Super slow on AppVeyor since a few weeks (Apr 2016)
    if gdal.GetConfigOption('APPVEYOR') is not None:
        print('Slow on AppVeyor')
        return 'skip'

    try:
        drv = gdal.GetDriverByName('HTTP')
    except:
        drv = None

    if drv is None:
        return 'skip'

    if int(gdal.VersionInfo('VERSION_NUM')) < 1900:
        gdaltest.post_reason('would stall for a long time')
        return 'skip'

    f = gdal.VSIFOpenL(
        '/vsicurl/http://svn.osgeo.org/gdal/trunk/autotest/ogr/data/poly.zip',
        'rb')
    if f is None:
        return 'skip'
    d = gdal.VSIFReadL(1, 1, f)
    gdal.VSIFCloseL(f)
    if len(d) == 0:
        return 'skip'

    ret, ret_str = run_gdal_ls([
        '', '-R', '-Rzip', 'http://svn.osgeo.org/gdal/trunk/autotest/ogr/data/'
    ])

    if ret != 'success':
        return ret

    if ret_str.find(
            '/vsizip//vsicurl/http://svn.osgeo.org/gdal/trunk/autotest/ogr/data/poly.zip/poly.PRJ'
    ) == -1:
        print(ret_str)
        return 'fail'

    return 'success'
Exemplo n.º 19
0
def tiff_12bitjpeg():

    old_accum = gdal.GetConfigOption('CPL_ACCUM_ERROR_MSG', 'OFF')
    gdal.SetConfigOption('CPL_ACCUM_ERROR_MSG', 'ON')
    gdal.ErrorReset()
    gdal.PushErrorHandler('CPLQuietErrorHandler')

    try:
        os.unlink('data/mandrilmini_12bitjpeg.tif.aux.xml')
    except:
        pass

    try:
        ds = gdal.Open('data/mandrilmini_12bitjpeg.tif')
        ds.GetRasterBand(1).ReadRaster(0, 0, 1, 1)
    except:
        ds = None

    gdal.PopErrorHandler()
    gdal.SetConfigOption('CPL_ACCUM_ERROR_MSG', old_accum)

    if gdal.GetLastErrorMsg().find('Unsupported JPEG data precision 12') != -1:
        sys.stdout.write('(12bit jpeg not available) ... ')
        return 'skip'
    elif ds is None:
        gdaltest.post_reason(
            'failed to open 12bit jpeg file with unexpected error')
        return 'fail'

    try:
        stats = ds.GetRasterBand(1).GetStatistics(0, 1)
    except:
        pass

    if stats[2] < 2150 or stats[2] > 2180 or str(stats[2]) == 'nan':
        gdaltest.post_reason('did not get expected mean for band1.')
        print(stats)
        return 'fail'
    ds = None

    os.unlink('data/mandrilmini_12bitjpeg.tif.aux.xml')

    return 'success'
Exemplo n.º 20
0
def test_mbtiles_3():

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

    if gdal.GetDriverByName('HTTP') is None:
        pytest.skip()

    if sys.platform == 'darwin' and gdal.GetConfigOption('TRAVIS',
                                                         None) is not None:
        pytest.skip("Hangs on MacOSX Travis sometimes. Not sure why.")

    # Check that we have SQLite VFS support
    gdal.PushErrorHandler('CPLQuietErrorHandler')
    ds = ogr.GetDriverByName('SQLite').CreateDataSource('/vsimem/mbtiles_3.db')
    gdal.PopErrorHandler()
    if ds is None:
        pytest.skip()
    ds = None
    gdal.Unlink('/vsimem/mbtiles_3.db')

    ds = gdal.Open(
        '/vsicurl/http://a.tiles.mapbox.com/v3/mapbox.geography-class.mbtiles')
    if ds is None:
        # Just skip. The service isn't perfectly reliable sometimes
        pytest.skip()

    # long=2,lat=49 in WGS 84 --> x=222638,y=6274861 in Google Mercator
    locationInfo = ds.GetRasterBand(1).GetMetadataItem(
        'GeoPixel_222638_6274861', 'LocationInfo')
    if locationInfo is None or locationInfo.find("France") == -1:
        print(locationInfo)
        if gdaltest.skip_on_travis():
            pytest.skip()
        pytest.fail('did not get expected LocationInfo')

    locationInfo2 = ds.GetRasterBand(1).GetOverview(5).GetMetadataItem(
        'GeoPixel_222638_6274861', 'LocationInfo')
    if locationInfo2 != locationInfo:
        print(locationInfo2)
        if gdaltest.skip_on_travis():
            pytest.skip()
        pytest.fail('did not get expected LocationInfo on overview')
Exemplo n.º 21
0
def test_ogr_carto_test_ogrsf():
    if gdal.GetConfigOption('SKIP_SLOW') is not None:
        pytest.skip()

    ogrtest.carto_test_server = 'https://gdalautotest2.carto.com'

    if gdaltest.gdalurlopen(ogrtest.carto_test_server) is None:
        pytest.skip('cannot open %s' % ogrtest.carto_test_server)

    import test_cli_utilities
    if test_cli_utilities.get_test_ogrsf_path() is None:
        pytest.skip()

    ret = gdaltest.runexternal(
        test_cli_utilities.get_test_ogrsf_path() +
        ' --config CARTO_HTTPS NO --config CARTO_PAGE_SIZE 300 -ro "CARTO:gdalautotest2 tables=tm_world_borders_simpl_0_3"'
    )

    assert ret.find('INFO') != -1 and ret.find('ERROR') == -1
Exemplo n.º 22
0
def startup_and_cleanup():

    # Unset all env vars that could influence the tests
    az_vars = {}
    for var in ('AZURE_STORAGE_CONNECTION_STRING', 'AZURE_STORAGE_ACCOUNT',
                'AZURE_STORAGE_ACCESS_KEY', 'AZURE_SAS',
                'AZURE_NO_SIGN_REQUEST'):
        az_vars[var] = gdal.GetConfigOption(var)
        if az_vars[var] is not None:
            gdal.SetConfigOption(var, "")

    assert gdal.GetSignedURL('/vsiadls/foo/bar') is None

    gdaltest.webserver_process = None
    gdaltest.webserver_port = 0

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

    (gdaltest.webserver_process, gdaltest.webserver_port) = webserver.launch(
        handler=webserver.DispatcherHttpHandler)
    if gdaltest.webserver_port == 0:
        pytest.skip()

    gdal.SetConfigOption(
        'AZURE_STORAGE_CONNECTION_STRING',
        'DefaultEndpointsProtocol=http;AccountName=myaccount;AccountKey=MY_ACCOUNT_KEY;EndpointSuffix=127.0.0.1:%d'
        % gdaltest.webserver_port)
    gdal.SetConfigOption('AZURE_STORAGE_ACCOUNT', '')
    gdal.SetConfigOption('AZURE_STORAGE_ACCESS_KEY', '')
    gdal.SetConfigOption('CPL_AZURE_TIMESTAMP', 'my_timestamp')

    yield

    # Clearcache needed to close all connections, since the Python server
    # can only handle one connection at a time
    gdal.VSICurlClearCache()

    webserver.server_stop(gdaltest.webserver_process, gdaltest.webserver_port)

    for var in az_vars:
        gdal.SetConfigOption(var, az_vars[var])
Exemplo n.º 23
0
    def testSetNoDataValue(self):
        if self.testDriver() == 'fail':
            return 'skip'

        src_ds = gdal.Open('data/' + self.filename)
        xsize = src_ds.RasterXSize
        ysize = src_ds.RasterYSize

        new_filename = 'tmp/' + self.filename + '.tst'
        new_ds = self.driver.Create(new_filename,
                                    xsize,
                                    ysize,
                                    1,
                                    src_ds.GetRasterBand(self.band).DataType,
                                    options=self.options)
        if new_ds is None:
            post_reason('Failed to create test file using Create method.')
            return 'fail'

        nodata = 11
        if new_ds.GetRasterBand(1).SetNoDataValue(nodata) is not gdal.CE_None:
            post_reason('Failed to set NoData value.')
            return 'fail'

        src_ds = None
        new_ds = None

        new_ds = gdal.Open(new_filename)
        if new_ds is None:
            post_reason('Failed to open dataset: ' + new_filename)
            return 'fail'

        if nodata != new_ds.GetRasterBand(1).GetNoDataValue():
            post_reason('Did not get expected NoData value.')
            return 'fail'

        new_ds = None

        if gdal.GetConfigOption('CPL_DEBUG', 'OFF') != 'ON':
            self.driver.Delete(new_filename)

        return 'success'
Exemplo n.º 24
0
def gpkg_init():

    gdaltest.db2_drv = None

    try:
        gdaltest.db2_drv = gdal.GetDriverByName('DB2ODBC')
        if gdaltest.db2_drv is None:
            return 'skip'
    except:
        return 'skip'

    try:
        gdaltest.png_dr = gdal.GetDriverByName('PNG')
    except:
        gdaltest.png_dr = None

    try:
        gdaltest.jpeg_dr = gdal.GetDriverByName('JPEG')
    except:
        gdaltest.jpeg_dr = None

    try:
        gdaltest.webp_dr = gdal.GetDriverByName('WEBP')
    except:
        gdaltest.webp_dr = None
    gdaltest.webp_supports_rgba = False
    if gdaltest.webp_dr is not None and gdal.GetConfigOption(
            "GPKG_SIMUL_WEBP_3BAND") is None:
        md = gdaltest.webp_dr.GetMetadata()
        if md['DMD_CREATIONOPTIONLIST'].find('LOSSLESS') >= 0:
            gdaltest.webp_supports_rgba = True

    if 'DB2_TEST_SERVER' in os.environ:
        gdaltest.db2_test_server = "DB2ODBC:" + os.environ['DB2_TEST_SERVER']
    else:
        gdaltest.post_reason('Environment variable DB2_TEST_SERVER not found')
        gdaltest.db2_drv = None
        return 'skip'

    print("\ntest server: " + gdaltest.db2_test_server + "\n")

    return 'success'
Exemplo n.º 25
0
def http_test_ssl_verifystatus():

    if gdal.GetDriverByName('HTTP') is None:
        return 'skip'

    with gdaltest.config_option('GDAL_HTTP_SSL_VERIFYSTATUS', 'YES'):
        with gdaltest.error_handler():
            # For now this URL doesn't support OCSP stapling...
            gdal.OpenEx('https://google.com', allowed_drivers=['HTTP'])
    last_err = gdal.GetLastErrorMsg()
    if last_err.find('No OCSP response received') < 0 and last_err.find('libcurl too old') < 0:

        # The test actually works on Travis Mac
        if sys.platform == 'darwin' and gdal.GetConfigOption('TRAVIS', None) is not None:
            return 'skip'

        print(last_err)
        return 'fail'

    return 'success'
Exemplo n.º 26
0
def startup_and_cleanup():

    az_vars = {}
    for var, reset_val in (
                ('AZURE_STORAGE_CONNECTION_STRING', None),
                ('AZURE_STORAGE_ACCOUNT', None),
                ('AZURE_STORAGE_ACCESS_KEY', None),
                ('AZURE_STORAGE_SAS_TOKEN', None),
                ('AZURE_NO_SIGN_REQUEST', None),
                ('AZURE_CONFIG_DIR', ''),
                ('AZURE_STORAGE_ACCESS_TOKEN', '')):
        az_vars[var] = gdal.GetConfigOption(var)
        gdal.SetConfigOption(var, reset_val)

    assert gdal.GetSignedURL('/vsiaz/foo/bar') is None

    yield

    for var in az_vars:
        gdal.SetConfigOption(var, az_vars[var])
Exemplo n.º 27
0
def test_mbtiles_10():

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

    if gdal.GetDriverByName('PNG') is None:
        pytest.skip()

    old_val_GPKG_FORCE_TEMPDB_COMPACTION = gdal.GetConfigOption('GPKG_FORCE_TEMPDB_COMPACTION')
    gdal.SetConfigOption('GPKG_FORCE_TEMPDB_COMPACTION', 'YES')
    with gdaltest.SetCacheMax(0):
        gdal.Translate('/vsimem/mbtiles_10.mbtiles', '../gcore/data/byte.tif', options='-of MBTILES -outsize 512 512')
    gdal.SetConfigOption('GPKG_FORCE_TEMPDB_COMPACTION', old_val_GPKG_FORCE_TEMPDB_COMPACTION)

    ds = gdal.Open('/vsimem/mbtiles_10.mbtiles')
    cs = ds.GetRasterBand(1).Checksum()
    assert cs in (29925, 30092) # 30092 on Mac
    ds = None

    gdal.Unlink('/vsimem/mbtiles_10.mbtiles')
Exemplo n.º 28
0
def http_4():

    # Too unreliable
    if gdaltest.skip_on_travis():
        return 'skip'

    try:
        drv = gdal.GetDriverByName('HTTP')
    except:
        drv = None

    if drv is None:
        return 'skip'

    ds = gdal.Open('/vsicurl/ftp://download.osgeo.org/gdal/data/gtiff/utm.tif')
    if ds is None:
        conn = gdaltest.gdalurlopen(
            'ftp://download.osgeo.org/gdal/data/gtiff/utm.tif', timeout=4)
        if conn is None:
            print('cannot open URL')
            return 'skip'
        try:
            conn.read()
        except:
            print('cannot read')
            return 'skip'
        conn.close()
        if sys.platform == 'darwin' and gdal.GetConfigOption('TRAVIS',
                                                             None) is not None:
            print("Fails on MacOSX Travis sometimes. Not sure why.")
            return 'skip'
        gdaltest.post_reason('fail')
        return 'fail'

    filelist = ds.GetFileList()
    if '/vsicurl/ftp://download.osgeo.org/gdal/data/gtiff/utm.tif' not in filelist:
        print(filelist)
        gdaltest.post_reason('fail')
        return 'fail'

    return 'success'
Exemplo n.º 29
0
def test_vsigs_init():

    gdaltest.gs_vars = {}
    for var in ('GS_SECRET_ACCESS_KEY', 'GS_ACCESS_KEY_ID',
                'GOOGLE_APPLICATION_CREDENTIALS', 'CPL_GS_TIMESTAMP',
                'CPL_GS_ENDPOINT', 'GDAL_HTTP_HEADER_FILE'):
        gdaltest.gs_vars[var] = gdal.GetConfigOption(var)
        if gdaltest.gs_vars[var] is not None:
            gdal.SetConfigOption(var, "")

    # To avoid user credentials in ~/.boto
    # to mess up our tests
    gdal.SetConfigOption('CPL_GS_CREDENTIALS_FILE', '')
    gdal.SetConfigOption('GS_OAUTH2_REFRESH_TOKEN', '')
    gdal.SetConfigOption('GS_OAUTH2_CLIENT_EMAIL', '')
    gdal.SetConfigOption('GS_OAUTH2_CLIENT_SECRET', '')
    gdal.SetConfigOption('GS_OAUTH2_CLIENT_ID', '')
    gdal.SetConfigOption('GOOGLE_APPLICATION_CREDENTIALS', '')

    with gdaltest.config_option('CPL_GCE_SKIP', 'YES'):
        assert gdal.GetSignedURL('/vsigs/foo/bar') is None
Exemplo n.º 30
0
def test_gdal_sieve_1():

    try:
        x = gdal.SieveFilter
    except:
        return 'skip'

    script_path = test_py_scripts.get_py_script('gdal_sieve')
    if script_path is None:
        return 'skip'

    drv = gdal.GetDriverByName('GTiff')
    dst_ds = drv.Create('tmp/sieve_1.tif', 5, 7, 1, gdal.GDT_Byte)
    dst_ds = None

    test_py_scripts.run_py_script(
        script_path, 'gdal_sieve',
        '-nomask -st 2 -4 ../alg/data/sieve_src.grd tmp/sieve_1.tif')

    dst_ds = gdal.Open('tmp/sieve_1.tif')
    dst_band = dst_ds.GetRasterBand(1)

    cs_expected = 364
    cs = dst_band.Checksum()

    dst_band = None
    dst_ds = None

    if cs == cs_expected \
       or gdal.GetConfigOption( 'CPL_DEBUG', 'OFF' ) != 'ON':
        # Reload because of side effects of run_py_script()
        drv = gdal.GetDriverByName('GTiff')
        drv.Delete('tmp/sieve_1.tif')

    if cs != cs_expected:
        print('Got: ', cs)
        gdaltest.post_reason('got wrong checksum')
        return 'fail'
    else:
        return 'success'