示例#1
0
def test_gdal_grid_clipsrc():
    if gdal_grid is None:
        pytest.skip()

    if not ogrtest.have_geos():
        pytest.skip()

    #################
    outfiles.append('tmp/grid_clipsrc.tif')
    try:
        os.remove(outfiles[-1])
    except OSError:
        pass

    open('tmp/clip.csv', 'wt').write(
        'id,WKT\n1,"POLYGON((440750 3751340,440750 3750100,441900 3750100,441900 3751340,440750 3751340))"\n')

    # Create a GDAL dataset from the values of "grid.csv".
    # Grid nodes are located exactly in raster nodes.
    gdaltest.runexternal_out_and_err(gdal_grid + ' -clipsrc tmp/clip.csv -txe 440720.0 441920.0 -tye 3751320.0 3750120.0 -outsize 20 20 -ot Byte -l grid -a nearest:radius1=0.0:radius2=0.0:angle=0.0:nodata=0.0 data/grid.vrt ' + outfiles[-1])

    os.unlink('tmp/clip.csv')

    # We should get the same values as in "gcore/data/byte.tif"
    ds = gdal.Open(outfiles[-1])
    cs = ds.GetRasterBand(1).Checksum()
    assert not (cs == 0 or cs == 4672), 'bad checksum'
    ds = None
示例#2
0
def test_gdalwarp_31():
    if test_cli_utilities.get_gdalwarp_path() is None:
        return 'skip'

    gdaltest.runexternal(test_cli_utilities.get_gdalwarp_path() + " ../gcore/data/byte.tif tmp/testgdalwarp31.tif")

    ds = gdal.Open('tmp/testgdalwarp31.tif')
    cs1 = ds.GetRasterBand(1).Checksum()
    ds = None

    (out, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdalwarp_path() + " ../gcore/data/byte.tif tmp/testgdalwarp31.tif -t_srs EPSG:4326")

    ds = gdal.Open('tmp/testgdalwarp31.tif')
    cs2 = ds.GetRasterBand(1).Checksum()
    ds = None

    (out, err2) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdalwarp_path() + " ../gcore/data/byte.tif tmp/testgdalwarp31.tif -t_srs EPSG:4326 -overwrite")

    ds = gdal.Open('tmp/testgdalwarp31.tif')
    cs3 = ds.GetRasterBand(1).Checksum()
    ds = None

    if cs1 != 4672 or cs2 != 4672 or cs3 != 4727 or err == '' or err2 != '':
        print(cs1,cs2,cs3)
        print(err)
        print(err2)
        return 'fail'

    return 'success'
示例#3
0
def test_gdal_translate_35():
    if test_cli_utilities.get_gdal_translate_path() is None:
        return 'skip'

    (_, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdal_translate_path())
    if err.find('No source dataset specified') < 0:
        gdaltest.post_reason('fail')
        print(err)
        return 'fail'

    (_, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdal_translate_path() + ' ../gcore/data/byte.tif')
    if err.find('No target dataset specified') < 0:
        gdaltest.post_reason('fail')
        print(err)
        return 'fail'

    (_, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdal_translate_path() + ' /non_existing_path/non_existing.tif /vsimem/out.tif')
    if err.find('does not exist in the file system') < 0 and err.find('No such file or directory') < 0:
        gdaltest.post_reason('fail')
        print(err)
        return 'fail'

    (_, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdal_translate_path() + ' ../gcore/data/byte.tif /non_existing_path/non_existing.tif')
    if err.find('Attempt to create new tiff file') < 0:
        gdaltest.post_reason('fail')
        print(err)
        return 'fail'

    return 'success'
示例#4
0
def test_gdal_translate_35():
    if test_cli_utilities.get_gdal_translate_path() is None:
        return "skip"

    (out, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdal_translate_path())
    if err.find("No source dataset specified") < 0:
        gdaltest.post_reason("fail")
        print(err)
        return "fail"

    (out, err) = gdaltest.runexternal_out_and_err(
        test_cli_utilities.get_gdal_translate_path() + " ../gcore/data/byte.tif"
    )
    if err.find("No target dataset specified") < 0:
        gdaltest.post_reason("fail")
        print(err)
        return "fail"

    (out, err) = gdaltest.runexternal_out_and_err(
        test_cli_utilities.get_gdal_translate_path() + " /non_existing_path/non_existing.tif /vsimem/out.tif"
    )
    if err.find("does not exist in the file system") < 0:
        gdaltest.post_reason("fail")
        print(err)
        return "fail"

    (out, err) = gdaltest.runexternal_out_and_err(
        test_cli_utilities.get_gdal_translate_path() + " ../gcore/data/byte.tif /non_existing_path/non_existing.tif"
    )
    if err.find("Attempt to create new tiff file") < 0:
        gdaltest.post_reason("fail")
        print(err)
        return "fail"

    return "success"
示例#5
0
def test_gdal_translate_32():
    if test_cli_utilities.get_gdal_translate_path() is None:
        return 'skip'

    gdaltest.runexternal(test_cli_utilities.get_gdal_translate_path() + ' ../gcore/data/byte_rpc.tif tmp/test_gdal_translate_32.tif -srcwin 1 2 13 14 -outsize 150% 300%')
    ds = gdal.Open('tmp/test_gdal_translate_32.tif')
    md = ds.GetMetadata('RPC')
    if abs(float(md['LINE_OFF']) - 47496) > 1e-5 or \
       abs(float(md['LINE_SCALE']) - 47502) > 1e-5 or \
       abs(float(md['SAMP_OFF']) - 19676.6923076923) > 1e-5 or \
       abs(float(md['SAMP_SCALE']) - 19678.1538461538) > 1e-5:
        gdaltest.post_reason('fail')
        print(md)
        return 'fail'

    gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdal_translate_path() + ' ../gcore/data/byte_rpc.tif tmp/test_gdal_translate_32.tif -srcwin -10 -5 20 20')
    ds = gdal.Open('tmp/test_gdal_translate_32.tif')
    md = ds.GetMetadata('RPC')
    if abs(float(md['LINE_OFF']) - (15834 - -5)) > 1e-5 or \
       abs(float(md['LINE_SCALE']) - 15834) > 1e-5 or \
       abs(float(md['SAMP_OFF']) - (13464 - -10)) > 1e-5 or \
       abs(float(md['SAMP_SCALE']) - 13464) > 1e-5:
        gdaltest.post_reason('fail')
        print(md)
        return 'fail'

    return 'success'
示例#6
0
def test_ogrinfo_17():
    if test_cli_utilities.get_ogrinfo_path() is None:
        return 'skip'

    (out, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_ogrinfo_path() + ' --optfile', check_memleak = False )
    if err.find('--optfile option given without filename') < 0:
        gdaltest.post_reason('fail')
        print(err)
        return 'fail'

    (out, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_ogrinfo_path() + ' --optfile /foo/bar', check_memleak = False )
    if err.find('Unable to open optfile') < 0:
        gdaltest.post_reason('fail')
        print(err)
        return 'fail'

    f = open('tmp/optfile.txt', 'wt')
    f.write('--config foo\n')
    f.close()
    (out, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_ogrinfo_path() + ' --optfile tmp/optfile.txt', check_memleak = False )
    os.unlink('tmp/optfile.txt')
    if err.find('--config option given without a key and value argument') < 0:
        gdaltest.post_reason('fail')
        print(err)
        return 'fail'

    return 'success'
示例#7
0
def test_gdal_translate_29():
    if test_cli_utilities.get_gdal_translate_path() is None:
        pytest.skip()

    (_, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdal_translate_path() + ' ../gcore/data/byte.tif tmp/test_gdal_translate_29.tif -outsize 50% 50% -r cubic')
    assert (err is None or err == ''), 'got error/warning'

    ds = gdal.Open('tmp/test_gdal_translate_29.tif')
    assert ds is not None

    cs = ds.GetRasterBand(1).Checksum()
    assert cs == 1059, 'Bad checksum'

    ds = None

    (_, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdal_translate_path() + ' ../gcore/data/byte.tif tmp/test_gdal_translate_29.vrt -outsize 50% 50% -r cubic -of VRT')
    assert (err is None or err == ''), 'got error/warning'
    (_, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdal_translate_path() + ' tmp/test_gdal_translate_29.vrt tmp/test_gdal_translate_29.tif')
    assert (err is None or err == ''), 'got error/warning'

    ds = gdal.Open('tmp/test_gdal_translate_29.tif')
    assert ds is not None

    cs = ds.GetRasterBand(1).Checksum()
    assert cs == 1059, 'Bad checksum'

    ds = None
示例#8
0
def test_gdalinfo_18():
    if test_cli_utilities.get_gdalinfo_path() is None:
        pytest.skip()

    (_, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdalinfo_path() + ' --optfile', check_memleak=False)
    assert '--optfile option given without filename' in err

    (_, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdalinfo_path() + ' --optfile /foo/bar', check_memleak=False)
    assert 'Unable to open optfile' in err
示例#9
0
def test_gdalinfo_21():
    if test_cli_utilities.get_gdalinfo_path() is None:
        pytest.skip()

    (_, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdalinfo_path() + ' --format', check_memleak=False)
    assert '--format option given without a format code' in err

    (_, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdalinfo_path() + ' --format foo_bar', check_memleak=False)
    assert '--format option given with format' in err
示例#10
0
def test_gnmmanage_2():
    if test_cli_utilities.get_gnmmanage_path() is None:
        pytest.skip()

    (_, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gnmmanage_path() + ' import ../gnm/data/pipes.shp tmp/test_gnm')
    assert (err is None or err == ''), 'got error/warning'

    (_, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gnmmanage_path() + ' import ../gnm/data/wells.shp tmp/test_gnm')
    assert (err is None or err == ''), 'got error/warning'
示例#11
0
def test_gdal_translate_29():
    if test_cli_utilities.get_gdal_translate_path() is None:
        return "skip"

    (out, err) = gdaltest.runexternal_out_and_err(
        test_cli_utilities.get_gdal_translate_path()
        + " ../gcore/data/byte.tif tmp/test_gdal_translate_29.tif -outsize 50% 50% -r cubic"
    )
    if not (err is None or err == ""):
        gdaltest.post_reason("got error/warning")
        print(err)
        return "fail"

    ds = gdal.Open("tmp/test_gdal_translate_29.tif")
    if ds is None:
        return "fail"

    cs = ds.GetRasterBand(1).Checksum()
    if cs != 1059:
        gdaltest.post_reason("Bad checksum")
        print(cs)
        return "fail"

    ds = None

    (out, err) = gdaltest.runexternal_out_and_err(
        test_cli_utilities.get_gdal_translate_path()
        + " ../gcore/data/byte.tif tmp/test_gdal_translate_29.vrt -outsize 50% 50% -r cubic -of VRT"
    )
    if not (err is None or err == ""):
        gdaltest.post_reason("got error/warning")
        print(err)
        return "fail"
    (out, err) = gdaltest.runexternal_out_and_err(
        test_cli_utilities.get_gdal_translate_path() + " tmp/test_gdal_translate_29.vrt tmp/test_gdal_translate_29.tif"
    )
    if not (err is None or err == ""):
        gdaltest.post_reason("got error/warning")
        print(err)
        return "fail"

    ds = gdal.Open("tmp/test_gdal_translate_29.tif")
    if ds is None:
        return "fail"

    cs = ds.GetRasterBand(1).Checksum()
    if cs != 1059:
        gdaltest.post_reason("Bad checksum")
        print(cs)
        return "fail"

    ds = None

    return "success"
示例#12
0
def test_ogrlineref_5():
    if not ogrtest.have_geos() or test_cli_utilities.get_ogrlineref_path() is None:
        pytest.skip()

    if os.path.exists('tmp/parts.kml'):
        ogr.GetDriverByName('KML').DeleteDataSource('tmp/parts.kml')

    gdaltest.runexternal_out_and_err(test_cli_utilities.get_ogrlineref_path() + ' -create -f "KML" -l data/path.shp -p data/mstones.shp -pm pos -o tmp/parts.kml -s 222')
    if os.path.exists('tmp/parts.kml'):
        return

    pytest.fail()
示例#13
0
def netcdf_test_4dfile( ofile ):

    # test result file has 8 bands and 0 subdasets (instead of 0 bands and 8 subdatasets)
    ds = gdal.Open( ofile )
    if ds is None:
        gdaltest.post_reason( 'open of copy failed' )
        return 'fail'
    md = ds.GetMetadata( 'SUBDATASETS' )
    subds_count = 0
    if not md is None:
        subds_count = len(md) / 2
    if ds.RasterCount != 8 or subds_count != 0:
        gdaltest.post_reason( 'copy has %d bands (expected 8) and has %d subdatasets'\
                                  ' (expected 0)' % (ds.RasterCount, subds_count ) )
        return 'fail'
    ds is None

    # get file header with ncdump (if available)
    try:
        (ret, err) = gdaltest.runexternal_out_and_err('ncdump -h')
    except:
        print('NOTICE: ncdump not found')
        return 'success'
    if err == None or not 'netcdf library version' in err:
        print('NOTICE: ncdump not found')
        return 'success'
    (ret, err) = gdaltest.runexternal_out_and_err( 'ncdump -h '+ ofile )
    if ret == '' or err != '':
        gdaltest.post_reason( 'ncdump failed' )
        return 'fail'

    # simple dimension tests using ncdump output
    err = ""
    if not 'int t(time, levelist, lat, lon) ;' in ret:
        err = err + 'variable (t) has wrong dimensions or is missing\n'
    if not 'levelist = 2 ;' in ret:
        err = err + 'levelist dimension is missing or incorrect\n'
    if not 'int levelist(levelist) ;' in ret:
        err = err + 'levelist variable is missing or incorrect\n'
    if not 'time = 4 ;' in ret:
        err = err + 'time dimension is missing or incorrect\n'
    if not 'double time(time) ;' in ret:
        err = err + 'time variable is missing or incorrect\n'
    # uncomment this to get full header in output
    #if err != '':
    #    err = err + ret
    if err != '':
        gdaltest.post_reason( err )
        return 'fail'
         
    return 'success'
示例#14
0
def test_gdaltindex_6():
    if test_cli_utilities.get_gdaltindex_path() is None:
        pytest.skip()

    for option in ['', '-lyr_name tileindex']:
        gdal.PushErrorHandler('CPLQuietErrorHandler')
        ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('tmp/test_gdaltindex_6.mif')
        gdal.PopErrorHandler()
        gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdaltindex_path() + ' -f "MapInfo File" %s tmp/test_gdaltindex_6.mif tmp/gdaltindex1.tif' % option)
        ds = ogr.Open('tmp/test_gdaltindex_6.mif')
        lyr = ds.GetLayer(0)
        assert lyr.GetFeatureCount() == 1, \
            ('got %d features, expecting 1' % lyr.GetFeatureCount())
        ds = None
示例#15
0
def test_vrtderived_7():

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

    ret, err = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdalinfo_path() + ' -checksum data/n43_hillshade.vrt --config GDAL_VRT_ENABLE_PYTHON YES')
    if gdal.GetConfigOption('CPL_DEBUG') is not None:
        print(err)
    # Either we cannot find a Python library, either it works
    if 'Checksum=0' in ret:
        print('Did not manage to find a Python library')
    elif 'Checksum=50577' not in ret:
        print(err)
        pytest.fail(ret)

    ret, err = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdalinfo_path() + ' -checksum data/n43_hillshade.vrt --config GDAL_VRT_ENABLE_PYTHON YES --config VRT_ENABLE_PYTHON_PATH NO')
    if gdal.GetConfigOption('CPL_DEBUG') is not None:
        print(err)
# Either we cannot find a Python library, either it works
    if 'Checksum=0' in ret:
        print('Did not manage to find a Python library')
    elif 'Checksum=50577' not in ret:
        print(err)
        pytest.fail(ret)

    ret, err = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdalinfo_path() + ' -checksum data/n43_hillshade.vrt --config GDAL_VRT_ENABLE_PYTHON YES --config VRT_ENABLE_PYTHON_SYMLINK NO')
    if gdal.GetConfigOption('CPL_DEBUG') is not None:
        print(err)
# Either we cannot find a Python library, either it works
    if 'Checksum=0' in ret:
        print('Did not manage to find a Python library')
    elif 'Checksum=50577' not in ret:
        print(err)
        pytest.fail(ret)

    # Invalid shared object name
    ret, err = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdalinfo_path() + ' -checksum data/n43_hillshade.vrt --config GDAL_VRT_ENABLE_PYTHON YES --config PYTHONSO foo')
    if gdal.GetConfigOption('CPL_DEBUG') is not None:
        print(err)
    assert 'Checksum=0' in ret, err

    # Valid shared object name, but without Python symbols
    libgdal_so = gdaltest.find_lib('gdal')
    if libgdal_so is not None:
        ret, err = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdalinfo_path() + ' -checksum data/n43_hillshade.vrt --config GDAL_VRT_ENABLE_PYTHON YES --config PYTHONSO "%s"' % libgdal_so)
        if gdal.GetConfigOption('CPL_DEBUG') is not None:
            print(err)
        assert 'Checksum=0' in ret, err
示例#16
0
def test_gdal_translate_35():
    if test_cli_utilities.get_gdal_translate_path() is None:
        pytest.skip()

    (_, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdal_translate_path())
    assert 'No source dataset specified' in err

    (_, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdal_translate_path() + ' ../gcore/data/byte.tif')
    assert 'No target dataset specified' in err

    (_, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdal_translate_path() + ' /non_existing_path/non_existing.tif /vsimem/out.tif')
    assert 'does not exist in the file system' in err or 'No such file or directory' in err

    (_, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdal_translate_path() + ' ../gcore/data/byte.tif /non_existing_path/non_existing.tif')
    assert 'Attempt to create new tiff file' in err
示例#17
0
def test_gdaltindex_3():
    if test_cli_utilities.get_gdaltindex_path() is None:
        return 'skip'

    drv = gdal.GetDriverByName('GTiff')
    wkt = 'GEOGCS[\"WGS 72\",DATUM[\"WGS_1972\"]]'

    ds = drv.Create('tmp/gdaltindex5.tif', 10, 10, 1)
    ds.SetProjection(wkt)
    ds.SetGeoTransform([47, 0.1, 0, 2, 0, -0.1])
    ds = None

    (_, ret_stderr) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdaltindex_path() + ' -skip_different_projection tmp/tileindex.shp tmp/gdaltindex5.tif')

    if ret_stderr.find('Warning : tmp/gdaltindex5.tif is not using the same projection system as other files in the tileindex.') == -1 or \
       ret_stderr.find('Use -t_srs option to set target projection system (not supported by MapServer).') == -1:
        print(ret_stderr)
        gdaltest.post_reason('got unexpected error message \n[%s]' % (ret_stderr))
        return 'fail'

    ds = ogr.Open('tmp/tileindex.shp')
    if ds.GetLayer(0).GetFeatureCount() != 4:
        return 'fail'
    ds.Destroy()

    return 'success'
示例#18
0
def test_gdal_translate_18():
    if test_cli_utilities.get_gdal_translate_path() is None:
        return "skip"

    gdaltest.runexternal(
        test_cli_utilities.get_gdal_translate_path() + " ../gcore/data/8bit_pal.bmp -of VRT tmp/test18_1.vrt"
    )
    gdaltest.runexternal(
        test_cli_utilities.get_gdal_translate_path() + " tmp/test18_1.vrt -expand rgb -of VRT tmp/test18_2.vrt"
    )
    (ret_stdout, ret_stderr) = gdaltest.runexternal_out_and_err(
        test_cli_utilities.get_gdal_translate_path() + " tmp/test18_2.vrt tmp/test18_2.tif"
    )

    # Check that all datasets are closed
    if ret_stderr.find("Open GDAL Datasets") != -1:
        return "fail"

    ds = gdal.Open("tmp/test18_2.tif")
    if ds is None:
        return "fail"

    if ds.GetRasterBand(1).Checksum() != 4672:
        gdaltest.post_reason("Bad checksum")
        return "fail"

    ds = None

    return "success"
示例#19
0
def test_ogrinfo_17():
    if test_cli_utilities.get_ogrinfo_path() is None:
        pytest.skip()

    (_, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_ogrinfo_path() + ' --optfile', check_memleak=False)
    assert '--optfile option given without filename' in err

    (_, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_ogrinfo_path() + ' --optfile /foo/bar', check_memleak=False)
    assert 'Unable to open optfile' in err

    f = open('tmp/optfile.txt', 'wt')
    f.write('--config foo\n')
    f.close()
    (_, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_ogrinfo_path() + ' --optfile tmp/optfile.txt', check_memleak=False)
    os.unlink('tmp/optfile.txt')
    assert '--config option given without a key and value argument' in err
示例#20
0
def test_ogrinfo_1():
    if test_cli_utilities.get_ogrinfo_path() is None:
        pytest.skip()

    (ret, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_ogrinfo_path() + ' ../ogr/data/poly.shp')
    assert (err is None or err == ''), 'got error/warning'
    assert ret.find('ESRI Shapefile') != -1
示例#21
0
def test_nearblack_8():
    if test_cli_utilities.get_nearblack_path() is None:
        return 'skip'

    src_ds = gdal.Open('../gdrivers/data/rgbsmall.tif')
    gdal.GetDriverByName('GTiff').CreateCopy('tmp/nearblack8.tif', src_ds)
    src_ds = None

    (_, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_nearblack_path() + ' tmp/nearblack8.tif -nb 0')
    if not (err is None or err == ''):
        gdaltest.post_reason('got error/warning')
        print(err)
        return 'fail'

    ds = gdal.Open('tmp/nearblack8.tif')
    if ds is None:
        return 'fail'

    if ds.GetRasterBand(1).Checksum() != 21106:
        print(ds.GetRasterBand(1).Checksum())
        gdaltest.post_reason('Bad checksum band 1')
        return 'fail'

    if ds.GetRasterBand(2).Checksum() != 20736:
        print(ds.GetRasterBand(2).Checksum())
        gdaltest.post_reason('Bad checksum band 2')
        return 'fail'

    if ds.GetRasterBand(3).Checksum() != 21309:
        print(ds.GetRasterBand(3).Checksum())
        gdaltest.post_reason('Bad checksum band 3')
        return 'fail'

    return 'success'
示例#22
0
def test_gdalbuildvrt_12():
    if test_cli_utilities.get_gdalbuildvrt_path() is None:
        return "skip"

    (out, err) = gdaltest.runexternal_out_and_err(
        test_cli_utilities.get_gdalbuildvrt_path() + " -tap tmp/gdalbuildvrt12.vrt ../gcore/data/byte.tif",
        check_memleak=False,
    )
    if err.find("-tap option cannot be used without using -tr") == -1:
        gdaltest.post_reason("expected error")
        return "fail"

    gdaltest.runexternal(
        test_cli_utilities.get_gdalbuildvrt_path() + " -tr 100 50 -tap tmp/gdalbuildvrt12.vrt ../gcore/data/byte.tif"
    )

    ds = gdal.Open("tmp/gdalbuildvrt12.vrt")

    gt = ds.GetGeoTransform()
    expected_gt = [440700.0, 100.0, 0.0, 3751350.0, 0.0, -50.0]
    for i in range(6):
        if abs(gt[i] - expected_gt[i] > 1e-5):
            gdaltest.post_reason("Expected : %s\nGot : %s" % (expected_gt, gt))
            return "fail"

    if ds.RasterXSize != 13 or ds.RasterYSize != 25:
        gdaltest.post_reason("Wrong raster dimensions : %d x %d" % (ds.RasterXSize, ds.RasterYSize))
        return "fail"

    return "success"
示例#23
0
def test_gdaldem_hillshade_compressed_tiled_output():
    if test_cli_utilities.get_gdaldem_path() is None:
        return 'skip'

    (out, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdaldem_path() + ' hillshade -s 111120 -z 30 ../gdrivers/data/n43.dt0 tmp/n43_hillshade_compressed_tiled.tif -co TILED=YES -co COMPRESS=DEFLATE --config GDAL_CACHEMAX 0')
    if not (err is None or err == '') :
        gdaltest.post_reason('got error/warning')
        print(err)
        return 'fail'

    ds = gdal.Open('tmp/n43_hillshade_compressed_tiled.tif')
    if ds is None:
        return 'fail'

    cs = ds.GetRasterBand(1).Checksum()
    if cs != 45587:
        gdaltest.post_reason('Bad checksum')
        print(cs)
        return 'fail'

    ds = None

    stat_uncompressed = os.stat('tmp/n43_hillshade.tif')
    stat_compressed = os.stat('tmp/n43_hillshade_compressed_tiled.tif')
    if stat_uncompressed.st_size < stat_compressed.st_size:
        gdaltest.post_reason('failure: compressed size greater than uncompressed one')
        return 'fail'

    return 'success'
示例#24
0
def test_gdalbuildvrt_16():
    if test_cli_utilities.get_gdalbuildvrt_path() is None:
        return 'skip'

    (out, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdalbuildvrt_path() + ' /non_existing_dir/non_existing_subdir/out.vrt ../gcore/data/byte.tif')

    if 'TRAVIS_BRANCH' in os.environ:
        val = os.environ['TRAVIS_BRANCH']
    else:
        val = ''
    if val.find('mingw') < 0:
        if err.find('ERROR ret code = 1') < 0:
            gdaltest.post_reason('fail')
            print(out)
            print(err)
            return 'fail'
    else:
        # We don't get the error code on Travis mingw
        if err.find('ERROR') < 0:
            gdaltest.post_reason('fail')
            print(out)
            print(err)
            return 'fail'

    return 'success'
示例#25
0
def test_gdal_translate_33():
    if test_cli_utilities.get_gdal_translate_path() is None:
        return 'skip'

    gdaltest.runexternal(test_cli_utilities.get_gdal_translate_path() + ' -outsize 100 0 ../gdrivers/data/small_world.tif tmp/test_gdal_translate_33.tif')

    ds = gdal.Open('tmp/test_gdal_translate_33.tif')
    if ds.RasterYSize != 50:
        gdaltest.post_reason('fail')
        print(ds.RasterYSize)
        return 'fail'
    ds = None

    gdaltest.runexternal(test_cli_utilities.get_gdal_translate_path() + ' -outsize 0 100 ../gdrivers/data/small_world.tif tmp/test_gdal_translate_33.tif')

    ds = gdal.Open('tmp/test_gdal_translate_33.tif')
    if ds.RasterXSize != 200:
        gdaltest.post_reason('fail')
        print(ds.RasterYSize)
        return 'fail'
    ds = None

    os.unlink('tmp/test_gdal_translate_33.tif')

    (_, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdal_translate_path() + ' -outsize 0 0 ../gdrivers/data/small_world.tif tmp/test_gdal_translate_33.tif')
    if err.find('-outsize 0 0 invalid') < 0:
        gdaltest.post_reason('fail')
        return 'fail'

    return 'success'
示例#26
0
def test_ogrlineref_1():
    if ogrtest.have_geos() is 0 or test_cli_utilities.get_ogrlineref_path() is None:
        return 'skip'

    try:
        os.stat('tmp/parts.shp')
        ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('tmp/parts.shp')
    except:
        pass

    (ret, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_ogrlineref_path() + ' -create -l data/path.shp -p data/mstones.shp -pm pos -o tmp/parts.shp -s 1000')
    if not (err is None or err == '') :
        gdaltest.post_reason('got error/warning')
        print(err)
        return 'fail'

    ds = ogr.Open('tmp/parts.shp')
    if ds is None or ds.GetLayer(0).GetFeatureCount() != 9:
        return 'fail'

    ds.Destroy()
    
#    ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('tmp/parts.shp')

    return 'success'
示例#27
0
def test_gdalinfo_1():
    if test_cli_utilities.get_gdalinfo_path() is None:
        pytest.skip()

    (ret, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdalinfo_path() + ' ../gcore/data/byte.tif')
    assert (err is None or err == ''), 'got error/warning'
    assert ret.find('Driver: GTiff/GeoTIFF') != -1
示例#28
0
文件: vrtmask.py 项目: ksshannon/gdal
def vrtmask_9():
    import test_cli_utilities
    if test_cli_utilities.get_gdal_translate_path() is None:
        return 'skip'

    src_ds = gdal.GetDriverByName('GTiff').Create('tmp/vrtmask_9_src.tif', 10, 10, 4)
    del src_ds

    (_, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdal_translate_path() + ' tmp/vrtmask_9_src.tif tmp/vrtmask_9_dst.tif -b 1 -b 2 -b 3')

    ds = gdal.Open('tmp/vrtmask_9_dst.tif')
    flags = ds.GetRasterBand(1).GetMaskFlags()
    ds = None

    os.remove('tmp/vrtmask_9_src.tif')
    os.remove('tmp/vrtmask_9_dst.tif')
    if err != '':
        gdaltest.post_reason('unexpected output on standard err')
        print(err)
        return 'fail'

    if flags != gdal.GMF_ALL_VALID:
        print(flags)
        return 'fail'

    return 'success'
示例#29
0
def test_gdalwarp_32():
    if test_cli_utilities.get_gdalwarp_path() is None:
        return 'skip'

    (out, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdalwarp_path() + ' -tap ../gcore/data/byte.tif tmp/testgdalwarp32.tif',
                                                  check_memleak = False)
    if err.find('-tap option cannot be used without using -tr') == -1:
        gdaltest.post_reason('expected error')
        return 'fail'

    gdaltest.runexternal(test_cli_utilities.get_gdalwarp_path() + ' -tr 100 50 -tap ../gcore/data/byte.tif tmp/testgdalwarp32.tif')

    ds = gdal.Open('tmp/testgdalwarp32.tif')
    if ds is None:
        return 'fail'

    expected_gt = (440700.0, 100.0, 0.0, 3751350.0, 0.0, -50.0)
    got_gt = ds.GetGeoTransform()
    if not gdaltest.geotransform_equals(expected_gt, got_gt, 1e-9) :
        gdaltest.post_reason('Bad geotransform')
        print(got_gt)
        return 'fail'

    if ds.RasterXSize != 13 or ds.RasterYSize != 25:
        gdaltest.post_reason('Wrong raster dimensions : %d x %d' % (ds.RasterXSize, ds.RasterYSize) )
        return 'fail'

    ds = None

    return 'success'
示例#30
0
def test_gdalbuildvrt_12():
    if test_cli_utilities.get_gdalbuildvrt_path() is None:
        return 'skip'

    (out, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdalbuildvrt_path() + ' -tap tmp/gdalbuildvrt12.vrt ../gcore/data/byte.tif',
                                                  check_memleak = False)
    if err.find('-tap option cannot be used without using -tr') == -1:
        gdaltest.post_reason('expected error')
        return 'fail'

    gdaltest.runexternal(test_cli_utilities.get_gdalbuildvrt_path() + ' -tr 100 50 -tap tmp/gdalbuildvrt12.vrt ../gcore/data/byte.tif')

    ds = gdal.Open('tmp/gdalbuildvrt12.vrt')

    gt = ds.GetGeoTransform()
    expected_gt = [ 440700.0, 100.0, 0.0, 3751350.0, 0.0, -50.0 ]
    for i in range(6):
        if abs(gt[i] - expected_gt[i] > 1e-5):
            gdaltest.post_reason('Expected : %s\nGot : %s' % (expected_gt, gt) )
            return 'fail'

    if ds.RasterXSize != 13 or ds.RasterYSize != 25:
        gdaltest.post_reason('Wrong raster dimensions : %d x %d' % (ds.RasterXSize, ds.RasterYSize) )
        return 'fail'

    return 'success'
示例#31
0
def test_ogrtindex_1(srs=None):
    if test_cli_utilities.get_ogrtindex_path() is None:
        pytest.skip()

    shape_drv = ogr.GetDriverByName('ESRI Shapefile')

    for basename in ['tileindex', 'point1', 'point2', 'point3', 'point4']:
        for extension in ['shp', 'dbf', 'shx', 'prj']:
            try:
                os.remove('tmp/%s.%s' % (basename, extension))
            except OSError:
                pass

    shape_ds = shape_drv.CreateDataSource('tmp')

    shape_lyr = shape_ds.CreateLayer('point1', srs=srs)
    dst_feat = ogr.Feature(feature_def=shape_lyr.GetLayerDefn())
    dst_feat.SetGeometry(ogr.CreateGeometryFromWkt('POINT(49 2)'))
    shape_lyr.CreateFeature(dst_feat)
    dst_feat.Destroy()

    shape_lyr = shape_ds.CreateLayer('point2', srs=srs)
    dst_feat = ogr.Feature(feature_def=shape_lyr.GetLayerDefn())
    dst_feat.SetGeometry(ogr.CreateGeometryFromWkt('POINT(49 3)'))
    shape_lyr.CreateFeature(dst_feat)
    dst_feat.Destroy()

    shape_lyr = shape_ds.CreateLayer('point3', srs=srs)
    dst_feat = ogr.Feature(feature_def=shape_lyr.GetLayerDefn())
    dst_feat.SetGeometry(ogr.CreateGeometryFromWkt('POINT(48 2)'))
    shape_lyr.CreateFeature(dst_feat)
    dst_feat.Destroy()

    shape_lyr = shape_ds.CreateLayer('point4', srs=srs)
    dst_feat = ogr.Feature(feature_def=shape_lyr.GetLayerDefn())
    dst_feat.SetGeometry(ogr.CreateGeometryFromWkt('POINT(48 3)'))
    shape_lyr.CreateFeature(dst_feat)
    dst_feat.Destroy()

    shape_ds.Destroy()

    (_, err) = gdaltest.runexternal_out_and_err(
        test_cli_utilities.get_ogrtindex_path() +
        ' -skip_different_projection tmp/tileindex.shp tmp/point1.shp tmp/point2.shp tmp/point3.shp tmp/point4.shp'
    )
    assert (err is None or err == ''), 'got error/warning'

    ds = ogr.Open('tmp/tileindex.shp')
    assert ds.GetLayer(
        0).GetFeatureCount() == 4, 'did not get expected feature count'

    if srs is not None:
        assert ds.GetLayer(0).GetSpatialRef() is not None and ds.GetLayer(0).GetSpatialRef().IsSame(srs), \
            'did not get expected spatial ref'
    else:
        assert ds.GetLayer(
            0).GetSpatialRef() is None, 'did not get expected spatial ref'

    expected_wkts = [
        'POLYGON ((49 2,49 2,49 2,49 2,49 2))',
        'POLYGON ((49 3,49 3,49 3,49 3,49 3))',
        'POLYGON ((48 2,48 2,48 2,48 2,48 2))',
        'POLYGON ((48 3,48 3,48 3,48 3,48 3))'
    ]
    i = 0
    feat = ds.GetLayer(0).GetNextFeature()
    while feat is not None:
        assert feat.GetGeometryRef().ExportToWkt() == expected_wkts[i], \
            ('i=%d, wkt=%s' % (i, feat.GetGeometryRef().ExportToWkt()))
        i = i + 1
        feat = ds.GetLayer(0).GetNextFeature()
    ds.Destroy()
示例#32
0
def test_gdal_viewshed_missing_source():

    _, err = gdaltest.runexternal_out_and_err(
        test_cli_utilities.get_gdal_viewshed_path())
    assert 'Missing source filename' in err
示例#33
0
def test_gdal_viewshed_missing_destination():

    _, err = gdaltest.runexternal_out_and_err(
        test_cli_utilities.get_gdal_viewshed_path() + ' /dev/null')
    assert 'Missing destination filename' in err
示例#34
0
def test_gdal_viewshed_missing_oy():

    _, err = gdaltest.runexternal_out_and_err(
        test_cli_utilities.get_gdal_viewshed_path() +
        ' -ox 0 /dev/null /dev/null')
    assert 'Missing -oy' in err
示例#35
0
def test_gdal_grid_1():
    if gdal_grid is None:
        return 'skip'

    shape_drv = ogr.GetDriverByName('ESRI Shapefile')
    outfiles.append('tmp/n43.tif')

    try:
        os.remove('tmp/n43.shp')
    except:
        pass
    try:
        os.remove('tmp/n43.dbf')
    except:
        pass
    try:
        os.remove('tmp/n43.shx')
    except:
        pass
    try:
        os.remove('tmp/n43.qix')
    except:
        pass

    # Create an OGR grid from the values of n43.dt0
    ds = gdal.Open('../gdrivers/data/n43.dt0')
    geotransform = ds.GetGeoTransform()

    shape_drv = ogr.GetDriverByName('ESRI Shapefile')
    shape_ds = shape_drv.CreateDataSource( 'tmp' )
    shape_lyr = shape_ds.CreateLayer( 'n43' )

    data = ds.ReadRaster(0, 0, 121, 121)
    array_val = struct.unpack('h' * 121*121, data)
    for j in range(121):
        for i in range(121):
            wkt = 'POINT(%f %f %s)' % ( geotransform[0] + (i + .5) * geotransform[1],
                                        geotransform[3] + (j + .5) * geotransform[5],
                                        array_val[j * 121 + i] )
            dst_feat = ogr.Feature( feature_def = shape_lyr.GetLayerDefn() )
            dst_feat.SetGeometry(ogr.CreateGeometryFromWkt(wkt))
            shape_lyr.CreateFeature( dst_feat )

    dst_feat.Destroy()

    shape_ds.ExecuteSQL('CREATE SPATIAL INDEX ON n43')

    shape_ds.Destroy()

    # Create a GDAL dataset from the previous generated OGR grid
    (out, err) = gdaltest.runexternal_out_and_err(gdal_grid + ' -txe -80.0041667 -78.9958333 -tye 42.9958333 44.0041667 -outsize 121 121 -ot Int16 -l n43 -a nearest:radius1=0.0:radius2=0.0:angle=0.0 -co TILED=YES -co BLOCKXSIZE=256 -co BLOCKYSIZE=256 tmp/n43.shp ' + outfiles[-1])
    if not (err is None or err == '') :
        gdaltest.post_reason('got error/warning')
        print(err)
        return 'fail'

    # We should get the same values as in n43.td0
    ds2 = gdal.Open(outfiles[-1])
    if ds.GetRasterBand(1).Checksum() != ds2.GetRasterBand(1).Checksum():
        print('bad checksum : got %d, expected %d' % (ds.GetRasterBand(1).Checksum() , ds2.GetRasterBand(1).Checksum()))
        return 'fail'
    if ds2.GetRasterBand(1).GetNoDataValue() is not None:
        print('did not expect nodata value')
        return 'fail'

    ds = None
    ds2 = None

    return 'success'
示例#36
0
def netcdf_cf_setup():

    # global vars
    gdaltest.netcdf_cf_method = None
    gdaltest.netcdf_cf_files = None
    gdaltest.netcdf_cf_check_error = ''

    # if netcdf is not supported, skip detection
    if gdaltest.netcdf_drv is None:
        pytest.skip()

    # skip if on windows
    if os.name != 'posix':
        pytest.skip('NOTICE: will skip CF checks because OS is not posix!')

    # try local method
    cdms2_installed = False
    try:
        imp.find_module('cdms2')
        cdms2_installed = True
    except ImportError:
        print('NOTICE: cdms2 not installed!')
        print(
            '        see installation notes at http://pypi.python.org/pypi/cfchecker'
        )
    if cdms2_installed:
        xml_dir = './data/netcdf_cf_xml'
        tmp_dir = './tmp/cache'
        files = dict()
        files['a'] = xml_dir + '/area-type-table.xml'
        files['s'] = tmp_dir + '/cf-standard-name-table-v18.xml'
        # either find udunits path in UDUNITS_PATH, or based on location of udunits app, or copy all .xml files to data
        # opt_u = '/home/soft/share/udunits/udunits2.xml'
        files['u'] = xml_dir + '/udunits2.xml'
        # look for xml files
        if not (os.path.exists(files['a']) and os.path.exists(files['s'])
                and os.path.exists(files['u'])):
            print(
                'NOTICE: cdms2 installed, but necessary xml files are not found!'
            )
            print('        the following files must exist:')
            print(
                '        ' + xml_dir +
                '/area-type-table.xml from http://cf-pcmdi.llnl.gov/documents/cf-standard-names/area-type-table/1/area-type-table.xml'
            )
            print(
                '        ' + tmp_dir +
                '/cf-standard-name-table-v18.xml - http://cf-pcmdi.llnl.gov/documents/cf-standard-names/standard-name-table/18/cf-standard-name-table.xml'
            )
            print('        ' + xml_dir +
                  '/udunits2*.xml from a UDUNITS2 install')
            # try to get cf-standard-name-table
            if not os.path.exists(files['s']):
                # print '        downloading cf-standard-name-table.xml (v18) from http://cf-pcmdi.llnl.gov ...'
                if not gdaltest.download_file(
                        'http://cf-pcmdi.llnl.gov/documents/cf-standard-names/standard-name-table/18/cf-standard-name-table.xml',
                        'cf-standard-name-table-v18.xml'):
                    print(
                        '        Failed to download, please get it and try again.'
                    )

        if os.path.exists(files['a']) and os.path.exists(
                files['s']) and os.path.exists(files['u']):
            gdaltest.netcdf_cf_method = 'local'
            gdaltest.netcdf_cf_files = files
            print(
                'NOTICE: netcdf CF compliance checks: using local checker script'
            )
            return

    # skip http method if GDAL_DOWNLOAD_TEST_DATA and GDAL_RUN_SLOW_TESTS are not defined
    if not gdaltest.download_test_data():
        print('NOTICE: skipping netcdf CF compliance checks')
        print(
            'to enable remote http checker script, define GDAL_DOWNLOAD_TEST_DATA=YES'
        )
        return

    if not gdaltest.run_slow_tests():
        print('NOTICE: skipping netcdf CF compliance checks')
        return

    # http method with curl, should use python module but easier for now
    success = False
    try:
        gdaltest.runexternal_out_and_err('curl')
    except OSError:
        print('no curl executable')
    else:
        # make sure script is responding
        handle = gdaltest.gdalurlopen(
            "http://puma.nerc.ac.uk/cgi-bin/cf-checker.pl")
        if handle is not None:
            success = True
        else:
            print('script not responding')
    if success:
        gdaltest.netcdf_cf_method = 'http'
        print('NOTICE: netcdf CF compliance checks: using remote HTTP '
              'checker script, consider installing cdms2 locally')
        return

    if gdaltest.netcdf_cf_method is None:
        print('NOTICE: skipping netcdf CF compliance checks')
示例#37
0
def test_gdal_viewshed_invalid_output_driver():

    _, err = gdaltest.runexternal_out_and_err(
        test_cli_utilities.get_gdal_viewshed_path() +
        ' -ox -79.5 -oy 43.5 -of FOOBAR ../gdrivers/data/n43.dt0 tmp/tmp.tif')
    assert 'Cannot get driver' in err
示例#38
0
def test_gdal_viewshed_invalid_output_filename():

    _, err = gdaltest.runexternal_out_and_err(
        test_cli_utilities.get_gdal_viewshed_path() +
        ' -ox -79.5 -oy 43.5 ../gdrivers/data/n43.dt0 i/do_not/exist.tif')
    assert 'Cannot create dataset' in err
示例#39
0
def netcdf_setup():

    gdaltest.netcdf_drv_version = None
    gdaltest.netcdf_drv_has_nc2 = False
    gdaltest.netcdf_drv_has_nc4 = False
    gdaltest.netcdf_drv = gdal.GetDriverByName( 'NETCDF' )
    gdaltest.netcdf_drv_silent = False;

    if gdaltest.netcdf_drv is None:
        print('NOTICE: netcdf not supported, skipping checks')
        return 'skip'

    #ugly hack to get netcdf version with 'ncdump', available in netcdf v3 avnd v4
    try:
        (ret, err) = gdaltest.runexternal_out_and_err('ncdump -h')
#        (ret, err) = gdaltest.runexternal_out_and_err('LD_LIBRARY_PATH=/home/src/netcdf-test/usr/lib:$LD_LIBRARY_PATH /home/src/netcdf-test/usr/bin/ncdump -h')
    except:
        #nothing is supported as ncdump not found
        print('NOTICE: netcdf version not found')
        return 'success'
    
    i = err.find('netcdf library version ')
    #version not found
    if i == -1:
        print('NOTICE: netcdf version not found')
        return 'success'

    #netcdf library version "3.6.3" of Dec 22 2009 06:10:17 $
    #netcdf library version 4.1.1 of Mar  4 2011 12:52:19 $
    v = err[ i+23 : ]
    v = v[ 0 : v.find(' ') ]
    v = v.strip('"');

    gdaltest.netcdf_drv_version = v

    #for version 3, assume nc2 supported and nc4 unsupported
    if v[0] == '3':
        gdaltest.netcdf_drv_has_nc2 = True
        gdaltest.netcdf_drv_has_nc4 = False

    # for version 4, use nc-config to test
    elif v[0] == '4':
    
        #check if netcdf library has nc2 (64-bit) support
        #this should be done at configure time by gdal like in cdo
        try:
            ret = gdaltest.runexternal('nc-config --has-nc2')
        except:
            gdaltest.netcdf_drv_has_nc2 = False
        else:
            #should test this on windows
            if ret.rstrip() == 'yes':
                gdaltest.netcdf_drv_has_nc2 = True
                    
        #check if netcdf library has nc4 support
        #this should be done at configure time by gdal like in cdo
        try:
            ret = gdaltest.runexternal('nc-config --has-nc4')
        except:
            gdaltest.netcdf_drv_has_nc4 = False
        else:
            #should test this on windows
            if ret.rstrip() == 'yes':
                gdaltest.netcdf_drv_has_nc4 = True

    print('NOTICE: using netcdf version ' + gdaltest.netcdf_drv_version+'  has_nc2: '+str(gdaltest.netcdf_drv_has_nc2)+'  has_nc4: '+str(gdaltest.netcdf_drv_has_nc4))
 
    return 'success'
示例#40
0
def netcdf_cfproj_testcopy(projTuples, origTiff, interFormats, inPath, outPath,
                           resFilename):
    """Test a Geotiff file can be converted to NetCDF, and projection in
    CF-1 conventions can be successfully maintained. Save results to file.

    :arg: projTuples - list of tuples
    :arg: interFormats - dict of intermediate format overrides
    :arg: outPath - path to save output
    :arg: resFilename - results filename to write to.
    """

    silent = True
    gdaltest.netcdf_drv_silent = True
    bWriteGdalTags = "YES"
    # silent = False
    gdaltest.netcdf_drv_silent = False
    #    bWriteGdalTags="NO"

    result = 'success'

    # Test if ncdump is available
    try:
        (_, err) = gdaltest.runexternal_out_and_err('ncdump -h')
    except OSError:
        # nothing is supported as ncdump not found
        pytest.skip('NOTICE: netcdf version not found')

    i = err.find('netcdf library version ')
    # version not found
    if i == -1:
        pytest.skip('NOTICE: netcdf version not found')

    if not os.path.exists(outPath):
        os.makedirs(outPath)
    resFile = open(os.path.join(outPath, resFilename), "w")

    if not os.path.exists(outPath):
        os.makedirs(outPath)

    heading = "Testing GDAL translation results to NetCDF\n"
    resFile.write(heading)
    resFile.write(len(heading) * "=" + "\n")

    #    now = datetime.datetime.now()
    #    resFile.write("*Date/time:* %s\n" % (now.strftime("%Y-%m-%d %H:%M")))
    resFile.write("\n")

    resPerProj = {}

    dsTiff = gdal.Open(os.path.join(inPath, origTiff), gdal.GA_ReadOnly)
    s_srs_wkt = dsTiff.GetProjection()

    # objects to hold the various tests
    i_t = 0
    tst_res = {}

    for proj in projTuples:
        try:
            intFmt = interFormats[proj[0]]
        except KeyError:
            intFmt = netcdf_cfproj_def_int_format

        intExt = netcdf_cfproj_format_fnames[intFmt]

        # Our little results data structures
        if not silent:
            print("")
            print("Testing %s (%s) translation:" % (proj[0], proj[1]))

        if not silent:
            print("About to create raster in chosen SRS")
        # projVrt = os.path.join(outPath, "%s_%s.vrt" % \
        #    (origTiff.rstrip('.tif'), proj[0] ))
        projRaster = os.path.join(
            outPath, "%s_%s.%s" % (origTiff.rstrip('.tif'), proj[0], intExt))
        srs = osr.SpatialReference()
        srs.SetFromUserInput(proj[2])
        t_srs_wkt = srs.ExportToWkt()
        if not silent:
            print("going to warp file " + origTiff + "\n" + s_srs_wkt +
                  "\ninto file " + projRaster + "\n" + t_srs_wkt)
        dswarp = gdal.AutoCreateWarpedVRT(dsTiff, s_srs_wkt, t_srs_wkt,
                                          gdal.GRA_NearestNeighbour, 0)
        drv_inter = gdal.GetDriverByName(intFmt)
        drv_netcdf = gdal.GetDriverByName("netcdf")
        dsw = drv_inter.CreateCopy(projRaster, dswarp, 0)
        if not silent:
            print("Warped %s to %s" % (proj[0], projRaster))

        projNc = os.path.join(outPath,
                              "%s_%s.nc" % (origTiff.rstrip('.tif'), proj[0]))
        # Force GDAL tags to be written to make testing easier, with preserved datum etc
        # ncCoOpts = "-co WRITE_GDAL_TAGS=yes"
        if not silent:
            print("About to translate to NetCDF")
        dst = drv_netcdf.CreateCopy(projNc, dsw, 0,
                                    ['WRITE_GDAL_TAGS=' + bWriteGdalTags])
        # For drivers like HFA, line below ESSENTIAL so that all info is
        # saved to new raster file - which we'll reopen later and want
        # to be fully updated.
        dsw = None
        del dst
        if not silent:
            print("Translated to %s" % (projNc))

        transWorked, resDetails = netcdf_cfproj_test_cf(proj, projNc)
        resPerProj[proj[0]] = resDetails

        resFile.write("%s (%s): " % (proj[0], proj[1]))
        if transWorked:
            resFile.write("OK\n")
        else:
            resFile.write("BAD\n")
            if 'missingProjName' in resPerProj[proj[0]]:
                resFile.write("\tMissing proj name '%s'\n" %
                              (resPerProj[proj[0]]['missingProjName']))
            for attrib in resPerProj[proj[0]]['missingAttrs']:
                resFile.write("\tMissing attrib '%s'\n" % (attrib))
            for cVarStdName in resPerProj[proj[0]]['missingCoordVarStdNames']:
                resFile.write("\tMissing coord var with std name '%s'\n" %
                              (cVarStdName))
            if 'cfcheck_error' in resPerProj[proj[0]]:
                resFile.write("\tFailed cf check: %s\n" %
                              (resPerProj[proj[0]]['cfcheck_error']))

        # test file copy
        # We now copy to a new file, just to be safe
        projNc2 = projNc.rstrip('.nc') + '2.nc'
        projRaster2 = os.path.join(
            outPath, "%s_%s2.%s" % (origTiff.rstrip('.tif'), proj[0], intExt))

        tst_res[i_t + 1] = netcdf_test_copy(projRaster, 1, None, projNc2, [],
                                            'NETCDF')
        tst_res[i_t + 2] = netcdf_test_copy(projNc2, 1, None, projRaster2, [],
                                            intFmt)

        if tst_res[i_t + 1] == 'fail' or tst_res[i_t + 2] == 'fail':
            result = 'fail'

        i_t = i_t + 2

    resFile.close()

    if not silent:
        print("\n" + "*" * 80)
        print("Saved results to file %s" %
              (os.path.join(outPath, resFilename)))

    # result = 'success'
    resFile = open(os.path.join(outPath, resFilename), "r")
    resStr = resFile.read()
    if resStr.find('BAD') != -1:
        print(
            '\nCF projection tests failed, here is the output (stored in file %s)\n'
            % (os.path.join(outPath, resFilename)))
        print(resStr)
        result = 'fail'

    return result
示例#41
0
文件: misc.py 项目: xwhsky/gdal
def misc_12():

    if int(gdal.VersionInfo('VERSION_NUM')) < 1900:
        gdaltest.post_reason('would crash')
        return 'skip'

    import test_cli_utilities
    gdal_translate_path = test_cli_utilities.get_gdal_translate_path()

    for i in range(gdal.GetDriverCount()):
        drv = gdal.GetDriver(i)
        md = drv.GetMetadata()
        if ('DCAP_CREATECOPY' in md or 'DCAP_CREATE' in md) and 'DCAP_RASTER' in md:

            nbands = 1
            if drv.ShortName == 'WEBP' or drv.ShortName == 'ADRG':
                nbands = 3

            datatype = gdal.GDT_Byte
            if drv.ShortName == 'BT' or drv.ShortName == 'BLX':
                datatype = gdal.GDT_Int16
            elif drv.ShortName == 'GTX' or drv.ShortName == 'NTv2' or drv.ShortName == 'Leveller' :
                datatype = gdal.GDT_Float32

            size = 1201
            if drv.ShortName == 'BLX':
                size = 128

            src_ds = gdal.GetDriverByName('GTiff').Create('/vsimem/misc_12_src.tif', size, size, nbands, datatype)
            set_gt = (2,1.0/size,0,49,0,-1.0/size)
            src_ds.SetGeoTransform(set_gt)
            src_ds.SetProjection('GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["degree",0.01745329251994328]]')

            # Test to detect crashes
            gdal.PushErrorHandler('CPLQuietErrorHandler')
            ds = drv.CreateCopy('/nonexistingpath' + get_filename(drv, ''), src_ds)
            gdal.PopErrorHandler()
            if ds is None and gdal.GetLastErrorMsg() == '':
                gdaltest.post_reason('failure')
                print('CreateCopy() into non existing dir fails without error message for driver %s' % drv.ShortName)
                return 'fail'
            ds = None

            if gdal_translate_path is not None:
                # Test to detect memleaks
                ds = gdal.GetDriverByName('VRT').CreateCopy('tmp/misc_12.vrt', src_ds)
                (out, err) = gdaltest.runexternal_out_and_err(gdal_translate_path + ' -of ' + drv.ShortName + ' tmp/misc_12.vrt /nonexistingpath/' + get_filename(drv, ''), check_memleak = False)
                del ds
                gdal.Unlink('tmp/misc_12.vrt')

                # If DEBUG_VSIMALLOC_STATS is defined, this is an easy way
                # to catch some memory leaks
                if out.find('VSIMalloc + VSICalloc - VSIFree') != -1 and \
                out.find('VSIMalloc + VSICalloc - VSIFree : 0') == -1:
                    if drv.ShortName == 'Rasterlite' and out.find('VSIMalloc + VSICalloc - VSIFree : 1') != -1:
                        pass
                    else:
                        print('memleak detected for driver %s' % drv.ShortName)

            src_ds = None

            gdal.Unlink('/vsimem/misc_12_src.tif')

    return 'success'
示例#42
0
def test_ogrtindex_3():

    if test_cli_utilities.get_ogrtindex_path() is None:
        pytest.skip()

    shape_drv = ogr.GetDriverByName('ESRI Shapefile')

    for basename in ['tileindex', 'point1', 'point2', 'point3', 'point4']:
        for extension in ['shp', 'dbf', 'shx', 'prj']:
            try:
                os.remove('tmp/%s.%s' % (basename, extension))
            except OSError:
                pass

    shape_ds = shape_drv.CreateDataSource('tmp')

    srs_4326 = osr.SpatialReference()
    srs_4326.ImportFromEPSG(4326)
    wkt_epsg_4326 = srs_4326.ExportToWkt()

    srs_32631 = osr.SpatialReference()
    srs_32631.ImportFromEPSG(32631)
    wkt_epsg_32631 = srs_32631.ExportToWkt()

    shape_lyr = shape_ds.CreateLayer('point1', srs=srs_4326)
    dst_feat = ogr.Feature(feature_def=shape_lyr.GetLayerDefn())
    dst_feat.SetGeometry(ogr.CreateGeometryFromWkt('POINT(2 49)'))
    shape_lyr.CreateFeature(dst_feat)

    shape_lyr = shape_ds.CreateLayer('point2', srs=srs_32631)
    dst_feat = ogr.Feature(feature_def=shape_lyr.GetLayerDefn())
    dst_feat.SetGeometry(
        ogr.CreateGeometryFromWkt('POINT(500000 5538630.70286887)'))
    shape_lyr.CreateFeature(dst_feat)
    shape_ds = None

    for (src_srs_format, expected_srss) in [
        ('', ['EPSG:4326', 'EPSG:32631']),
        ('-src_srs_format AUTO', ['EPSG:4326', 'EPSG:32631']),
        ('-src_srs_format EPSG', ['EPSG:4326', 'EPSG:32631']),
        ('-src_srs_format PROJ', [
            '+proj=longlat +datum=WGS84 +no_defs',
            '+proj=utm +zone=31 +datum=WGS84 +units=m +no_defs'
        ]), ('-src_srs_format WKT', [wkt_epsg_4326, wkt_epsg_32631])
    ]:

        if os.path.exists('tmp/tileindex.shp'):
            shape_drv.DeleteDataSource('tmp/tileindex.shp')
        if os.path.exists('tmp/tileindex.db'):
            os.unlink('tmp/tileindex.db')

        output_filename = 'tmp/tileindex.shp'
        output_format = ''
        if src_srs_format == '-src_srs_format WKT':
            if ogr.GetDriverByName('SQLite') is None:
                continue
            output_filename = 'tmp/tileindex.db'
            output_format = ' -f SQLite'

        (_, err) = gdaltest.runexternal_out_and_err(
            test_cli_utilities.get_ogrtindex_path() +
            ' -src_srs_name src_srs -t_srs EPSG:4326 ' + output_filename +
            ' tmp/point1.shp tmp/point2.shp ' + src_srs_format + output_format)

        assert src_srs_format == '-src_srs_format WKT' or (err is None or err == ''), \
            'got error/warning'

        ds = ogr.Open(output_filename)
        assert ds.GetLayer(0).GetFeatureCount() == 2, \
            'did not get expected feature count'

        assert ds.GetLayer(0).GetSpatialRef().GetAuthorityCode(None) == '4326', \
            'did not get expected spatial ref'

        expected_wkts = [
            'POLYGON ((2 49,2 49,2 49,2 49,2 49))',
            'POLYGON ((3 50,3 50,3 50,3 50,3 50))'
        ]
        i = 0
        feat = ds.GetLayer(0).GetNextFeature()
        while feat is not None:
            if feat.GetField('src_srs') != expected_srss[i]:
                feat.DumpReadable()
                pytest.fail(i, src_srs_format)
            assert ogrtest.check_feature_geometry(feat, expected_wkts[i]) == 0, \
                ('i=%d, wkt=%s' % (i, feat.GetGeometryRef().ExportToWkt()))
            i = i + 1
            feat = ds.GetLayer(0).GetNextFeature()
        ds = None

    if os.path.exists('tmp/tileindex.shp'):
        shape_drv.DeleteDataSource('tmp/tileindex.shp')
    if os.path.exists('tmp/tileindex.db'):
        os.unlink('tmp/tileindex.db')
示例#43
0
def test_vrtderived_7():

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

    ret, err = gdaltest.runexternal_out_and_err(
        test_cli_utilities.get_gdalinfo_path() +
        ' -checksum data/vrt/n43_hillshade.vrt --config GDAL_VRT_ENABLE_PYTHON YES'
    )
    if gdal.GetConfigOption('CPL_DEBUG') is not None:
        print(err)
    # Either we cannot find a Python library, either it works
    if 'Checksum=0' in ret:
        print('Did not manage to find a Python library')
    elif 'Checksum=50577' not in ret:
        print(err)
        pytest.fail(ret)

    ret, err = gdaltest.runexternal_out_and_err(
        test_cli_utilities.get_gdalinfo_path() +
        ' -checksum data/vrt/n43_hillshade.vrt --config GDAL_VRT_ENABLE_PYTHON YES --config VRT_ENABLE_PYTHON_PATH NO'
    )
    if gdal.GetConfigOption('CPL_DEBUG') is not None:
        print(err)
# Either we cannot find a Python library, either it works
    if 'Checksum=0' in ret:
        print('Did not manage to find a Python library')
    elif 'Checksum=50577' not in ret:
        print(err)
        pytest.fail(ret)

    ret, err = gdaltest.runexternal_out_and_err(
        test_cli_utilities.get_gdalinfo_path() +
        ' -checksum data/vrt/n43_hillshade.vrt --config GDAL_VRT_ENABLE_PYTHON YES --config VRT_ENABLE_PYTHON_SYMLINK NO'
    )
    if gdal.GetConfigOption('CPL_DEBUG') is not None:
        print(err)
# Either we cannot find a Python library, either it works
    if 'Checksum=0' in ret:
        print('Did not manage to find a Python library')
    elif 'Checksum=50577' not in ret:
        print(err)
        pytest.fail(ret)

    # Invalid shared object name
    ret, err = gdaltest.runexternal_out_and_err(
        test_cli_utilities.get_gdalinfo_path() +
        ' -checksum data/vrt/n43_hillshade.vrt --config GDAL_VRT_ENABLE_PYTHON YES --config PYTHONSO foo'
    )
    if gdal.GetConfigOption('CPL_DEBUG') is not None:
        print(err)
    assert 'Checksum=0' in ret, err

    # Valid shared object name, but without Python symbols
    libgdal_so = gdaltest.find_lib('gdal')
    if libgdal_so is not None:
        ret, err = gdaltest.runexternal_out_and_err(
            test_cli_utilities.get_gdalinfo_path() +
            ' -checksum data/vrt/n43_hillshade.vrt --config GDAL_VRT_ENABLE_PYTHON YES --config PYTHONSO "%s"'
            % libgdal_so)
        if gdal.GetConfigOption('CPL_DEBUG') is not None:
            print(err)
        assert 'Checksum=0' in ret, err
示例#44
0
def test_gdal_viewshed_invalid_band():

    _, err = gdaltest.runexternal_out_and_err(
        test_cli_utilities.get_gdal_viewshed_path() +
        ' -ox 0 -oy 0 -b 2 ../gdrivers/data/n43.dt0 tmp/tmp.tif')
    assert 'Illegal band' in err
示例#45
0
def test_gdal_grid_12():
    if gdal_grid is None:
        return 'skip'

    #################
    # Test generic implementation (no AVX, no SSE)
    outfiles.append('tmp/grid_invdistnn_generic.tif')
    try:
        os.remove(outfiles[-1])
    except:
        pass

    # Create a GDAL dataset from the values of "grid.csv".
    (out, err) = gdaltest.runexternal_out_and_err(gdal_grid + ' -txe 440721.0 441920.0 -tye 3751321.0 3750120.0 -outsize 20 20 -ot Float64 -l grid -a invdistnn:power=2.0:radius=1.0:max_points=12:min_points=0:nodata=0.0 data/grid.vrt ' + outfiles[-1])

    # We should get the same values as in "ref_data/gdal_invdistnn.tif"
    ds = gdal.Open(outfiles[-1])
    ds_ref = gdal.Open('ref_data/grid_invdistnn.tif')
    maxdiff = gdaltest.compare_ds(ds, ds_ref, verbose = 0)
    if maxdiff > 0.00001:
        gdaltest.compare_ds(ds, ds_ref, verbose = 1)
        gdaltest.post_reason('Image too different from the reference')
        return 'fail'
    ds_ref = None
    ds = None

    #################
    outfiles.append('tmp/grid_invdistnn_250_8minp.tif')
    try:
        os.remove(outfiles[-1])
    except:
        pass

    # Create a GDAL dataset from the values of "grid.csv".
    # Circular window, shifted, test min points and NODATA setting.
    gdaltest.runexternal(gdal_grid + ' -txe 440721.0 441920.0 -tye 3751321.0 3750120.0 -outsize 20 20 -ot Float64 -l grid -a invdistnn:power=2.0:radius=250.0:max_points=12:min_points=8:nodata=0.0 data/grid.vrt ' + outfiles[-1])

    # We should get the same values as in "ref_data/grid_invdistnn_250_8minp.tif"
    ds = gdal.Open(outfiles[-1])
    ds_ref = gdal.Open('ref_data/grid_invdistnn_250_8minp.tif')
    maxdiff = gdaltest.compare_ds(ds, ds_ref, verbose = 0)
    if maxdiff > 0.00001:
        gdaltest.compare_ds(ds, ds_ref, verbose = 1)
        gdaltest.post_reason('Image too different from the reference')
        return 'fail'
    ds_ref = None
    ds = None

    #################
    # Test generic implementation with max_points and radius specified
    outfiles.append('tmp/grid_invdistnn_250_10maxp_3pow.tif')
    try:
        os.remove(outfiles[-1])
    except:
        pass

    # Create a GDAL dataset from the values of "grid.csv".
    gdaltest.runexternal(gdal_grid + ' -txe 440721.0 441920.0 -tye 3751321.0 3750120.0 -outsize 20 20 -ot Float64 -l grid -a invdistnn:power=3.0:radius=250.0:max_points=10:min_points=0:nodata=0.0 data/grid.vrt ' + outfiles[-1])

    # We should get the same values as in "ref_data/gdal_invdistnn_250_10maxp_3pow.tif"
    ds = gdal.Open(outfiles[-1])
    ds_ref = gdal.Open('ref_data/grid_invdistnn_250_10maxp_3pow.tif')
    maxdiff = gdaltest.compare_ds(ds, ds_ref, verbose = 0)
    if maxdiff > 0.00001:
        gdaltest.compare_ds(ds, ds_ref, verbose = 1)
        gdaltest.post_reason('Image too different from the reference')
        return 'fail'
    ds_ref = None
    ds = None

    return 'success'
示例#46
0
def misc_12():

    if int(gdal.VersionInfo('VERSION_NUM')) < 1900:
        gdaltest.post_reason('would crash')
        return 'skip'

    for i in range(gdal.GetDriverCount()):
        drv = gdal.GetDriver(i)
        #if drv.ShortName == 'ECW' or drv.ShortName == 'JP2ECW':
        #    continue
        md = drv.GetMetadata()
        if 'DCAP_CREATECOPY' in md or 'DCAP_CREATE' in md:

            ext = ''
            if drv.ShortName == 'GTX':
                ext = '.gtx'
            elif drv.ShortName == 'RST':
                ext = '.rst'
            elif drv.ShortName == 'SAGA':
                ext = '.sdat'
            elif drv.ShortName == 'ECW':
                ext = '.ecw'
            elif drv.ShortName == 'KMLSUPEROVERLAY':
                ext = '.kmz'
            elif drv.ShortName == 'ADRG':
                ext = '/ABCDEF01.GEN'
            elif drv.ShortName == 'SRTMHGT':
                ext = '/N48E002.HGT'

            nbands = 1
            if drv.ShortName == 'WEBP' or drv.ShortName == 'ADRG':
                nbands = 3

            datatype = gdal.GDT_Byte
            if drv.ShortName == 'BT' or drv.ShortName == 'BLX':
                datatype = gdal.GDT_Int16
            elif drv.ShortName == 'GTX' or drv.ShortName == 'NTv2' or drv.ShortName == 'Leveller':
                datatype = gdal.GDT_Float32

            size = 1201
            if drv.ShortName == 'BLX':
                size = 128

            src_ds = gdal.GetDriverByName('GTiff').Create(
                '/vsimem/misc_12_src.tif', size, size, nbands, datatype)
            set_gt = (2, 1.0 / size, 0, 49, 0, -1.0 / size)
            src_ds.SetGeoTransform(set_gt)
            src_ds.SetProjection(
                'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["degree",0.01745329251994328]]'
            )

            # Test to detect crashes
            gdal.PushErrorHandler('CPLQuietErrorHandler')
            ds = drv.CreateCopy('/nonexistingpath/nonexistingfile' + ext,
                                src_ds)
            gdal.PopErrorHandler()
            ds = None

            # Test to detect memleaks
            ds = gdal.GetDriverByName('VRT').CreateCopy(
                'tmp/misc_12.vrt', src_ds)
            import test_cli_utilities
            (out, err) = gdaltest.runexternal_out_and_err(
                test_cli_utilities.get_gdal_translate_path() + ' -of ' +
                drv.ShortName +
                ' tmp/misc_12.vrt /nonexistingpath/nonexistingfile' + ext,
                check_memleak=False)
            ds = None
            gdal.Unlink('tmp/misc_12.vrt')

            # If DEBUG_VSIMALLOC_STATS is defined, this is an easy way
            # to catch some memory leaks
            if out.find('VSIMalloc + VSICalloc - VSIFree') != -1 and \
               out.find('VSIMalloc + VSICalloc - VSIFree : 0') == -1:
                if drv.ShortName == 'Rasterlite' and out.find(
                        'VSIMalloc + VSICalloc - VSIFree : 1') != -1:
                    pass
                else:
                    print('memleak detected for driver %s' % drv.ShortName)

            src_ds = None

            gdal.Unlink('/vsimem/misc_12_src.tif')

    return 'success'
示例#47
0
def test_gdal_contour_1():
    if test_cli_utilities.get_gdal_contour_path() is None:
        return 'skip'

    try:
        os.remove('tmp/contour.shp')
    except:
        pass
    try:
        os.remove('tmp/contour.dbf')
    except:
        pass
    try:
        os.remove('tmp/contour.shx')
    except:
        pass

    drv = gdal.GetDriverByName('GTiff')
    wkt = 'GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9108\"]],AUTHORITY[\"EPSG\",\"4326\"]]'

    size = 160
    precision = 1. / size

    ds = drv.Create('tmp/gdal_contour.tif', size, size, 1)
    ds.SetProjection( wkt )
    ds.SetGeoTransform( [ 1, precision, 0, 50, 0, -precision ] )

    raw_data = array.array('h',[10 for i in range(int(size/2))]).tostring()
    for i in range(int(size/2)):
        ds.WriteRaster( int(size/4), i+int(size/4), int(size/2), 1, raw_data,
                        buf_type = gdal.GDT_Int16,
                        band_list = [1] )

    raw_data = array.array('h',[20 for i in range(int(size/2))]).tostring()
    for i in range(int(size/4)):
        ds.WriteRaster( int(size/4)+int(size/8), i+int(size/4)+int(size/8), int(size/4), 1, raw_data,
                        buf_type = gdal.GDT_Int16,
                        band_list = [1] )

    raw_data = array.array('h',[25 for i in range(int(size/4))]).tostring()
    for i in range(int(size/8)):
        ds.WriteRaster( int(size/4)+int(size/8)+int(size/16), i+int(size/4)+int(size/8)+int(size/16), int(size/8), 1, raw_data,
                        buf_type = gdal.GDT_Int16,
                        band_list = [1] )

    ds = None

    (out, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdal_contour_path() + ' -a elev -i 10 tmp/gdal_contour.tif tmp/contour.shp')
    if not (err is None or err == '') :
        gdaltest.post_reason('got error/warning')
        print(err)
        return 'fail'

    ds = ogr.Open('tmp/contour.shp')

    expected_envelopes = [ [ 1.25, 1.75, 49.25, 49.75 ],
                           [ 1.25+0.125, 1.75-0.125, 49.25+0.125, 49.75-0.125 ] ]
    expected_height = [ 10, 20 ]

    lyr = ds.ExecuteSQL("select * from contour order by elev asc")

    if lyr.GetSpatialRef().ExportToWkt().find('GCS_WGS_1984') == -1:
        print('Did not get expected spatial ref')
        return 'fail'

    if lyr.GetFeatureCount() != len(expected_envelopes):
        print('Got %d features. Expected %d' % (lyr.GetFeatureCount(), len(expected_envelopes)))
        return 'fail'

    i = 0
    feat = lyr.GetNextFeature()
    while feat is not None:
        envelope = feat.GetGeometryRef().GetEnvelope()
        if feat.GetField('elev') != expected_height[i]:
            print('Got %f. Expected %f' % (feat.GetField('elev'), expected_height[i]))
            return 'fail'
        for j in range(4):
            if abs(expected_envelopes[i][j] - envelope[j]) > precision/2*1.001:
                print('i=%d, wkt=%s' % (i, feat.GetGeometryRef().ExportToWkt()))
                print(feat.GetGeometryRef().GetEnvelope())
                print(expected_envelopes[i])
                print('%f, %f' % (expected_envelopes[i][j] - envelope[j], precision / 2))
                return 'fail'
        i = i + 1
        feat = lyr.GetNextFeature()

    ds.ReleaseResultSet(lyr)
    ds.Destroy()

    return 'success'
示例#48
0
def test_gdal_rasterize_1():

    if test_cli_utilities.get_gdal_rasterize_path() is None:
        return 'skip'

    # Setup working spatial reference
    # sr_wkt = 'LOCAL_CS["arbitrary"]'
    # sr = osr.SpatialReference( sr_wkt )
    sr = osr.SpatialReference()
    sr.ImportFromEPSG(32631)
    sr_wkt = sr.ExportToWkt()

    # Create a raster to rasterize into.

    target_ds = gdal.GetDriverByName('GTiff').Create('tmp/rast1.tif', 100, 100,
                                                     3, gdal.GDT_Byte)
    target_ds.SetGeoTransform((1000, 1, 0, 1100, 0, -1))
    target_ds.SetProjection(sr_wkt)

    # Close TIF file
    target_ds = None

    # Create a layer to rasterize from.

    rast_ogr_ds = \
        ogr.GetDriverByName('MapInfo File').CreateDataSource('tmp/rast1.tab')
    rast_lyr = rast_ogr_ds.CreateLayer('rast1', srs=sr)

    rast_lyr.GetLayerDefn()
    field_defn = ogr.FieldDefn('foo')
    rast_lyr.CreateField(field_defn)

    # Add a polygon.

    wkt_geom = 'POLYGON((1020 1030,1020 1045,1050 1045,1050 1030,1020 1030))'

    feat = ogr.Feature(rast_lyr.GetLayerDefn())
    feat.SetGeometryDirectly(ogr.Geometry(wkt=wkt_geom))

    rast_lyr.CreateFeature(feat)

    # Add feature without geometry to test fix for #3310
    feat = ogr.Feature(rast_lyr.GetLayerDefn())
    rast_lyr.CreateFeature(feat)

    # Add a linestring.

    wkt_geom = 'LINESTRING(1000 1000, 1100 1050)'

    feat = ogr.Feature(rast_lyr.GetLayerDefn())
    feat.SetGeometryDirectly(ogr.Geometry(wkt=wkt_geom))

    rast_lyr.CreateFeature(feat)

    # Close file
    rast_ogr_ds.Destroy()

    # Run the algorithm.
    (_, err) = gdaltest.runexternal_out_and_err(
        test_cli_utilities.get_gdal_rasterize_path() +
        ' -b 3 -b 2 -b 1 -burn 200 -burn 220 -burn 240 -l rast1 tmp/rast1.tab tmp/rast1.tif'
    )
    if not (err is None or err == ''):
        gdaltest.post_reason('got error/warning')
        print(err)
        return 'fail'

    # Check results.

    target_ds = gdal.Open('tmp/rast1.tif')
    expected = 6452
    checksum = target_ds.GetRasterBand(2).Checksum()
    if checksum != expected:
        print(checksum)
        gdaltest.post_reason('Did not get expected image checksum')

        return 'fail'

    target_ds = None

    return 'success'
示例#49
0
def test_gdal_grid_3():
    if gdal_grid is None:
        return 'skip'

    #################
    # Test generic implementation (no AVX, no SSE)
    outfiles.append('tmp/grid_invdist_generic.tif')
    try:
        os.remove(outfiles[-1])
    except:
        pass

    # Create a GDAL dataset from the values of "grid.csv".
    print('Step 1: Disabling AVX/SSE optimized versions...')
    (out, err) = gdaltest.runexternal_out_and_err(gdal_grid + ' --debug on --config GDAL_USE_AVX NO --config GDAL_USE_SSE NO -txe 440720.0 441920.0 -tye 3751320.0 3750120.0 -outsize 20 20 -ot Float64 -l grid -a invdist:power=2.0:smoothing=0.0:radius1=0.0:radius2=0.0:angle=0.0:max_points=0:min_points=0:nodata=0.0 data/grid.vrt ' + outfiles[-1])
    pos = err.find(' threads')
    if pos >= 0:
        pos_blank = err[0:pos-1].rfind(' ')
        if pos_blank >= 0:
            print('Step 1: %s threads used' % err[pos_blank+1:pos])

    # We should get the same values as in "ref_data/gdal_invdist.tif"
    ds = gdal.Open(outfiles[-1])
    ds_ref = gdal.Open('ref_data/grid_invdist.tif')
    maxdiff = gdaltest.compare_ds(ds, ds_ref, verbose = 0)
    if maxdiff > 1:
        gdaltest.compare_ds(ds, ds_ref, verbose = 1)
        gdaltest.post_reason('Image too different from the reference')
        return 'fail'
    ds_ref = None
    ds = None

    #################
    # Potentially test optimized SSE implementation

    outfiles.append('tmp/grid_invdist_sse.tif')
    try:
        os.remove(outfiles[-1])
    except:
        pass

    # Create a GDAL dataset from the values of "grid.csv".
    print('Step 2: Trying SSE optimized version...')
    (out, err) = gdaltest.runexternal_out_and_err(gdal_grid + ' --debug on --config GDAL_USE_AVX NO -txe 440720.0 441920.0 -tye 3751320.0 3750120.0 -outsize 20 20 -ot Float64 -l grid -a invdist:power=2.0:smoothing=0.0:radius1=0.0:radius2=0.0:angle=0.0:max_points=0:min_points=0:nodata=0.0 data/grid.vrt ' + outfiles[-1])
    if err.find('SSE') >= 0:
        print('...SSE optimized version used')
    else:
        print('...SSE optimized version NOT used')

    # We should get the same values as in "ref_data/gdal_invdist.tif"
    ds = gdal.Open(outfiles[-1])
    ds_ref = gdal.Open('ref_data/grid_invdist.tif')
    maxdiff = gdaltest.compare_ds(ds, ds_ref, verbose = 0)
    if maxdiff > 1:
        gdaltest.compare_ds(ds, ds_ref, verbose = 1)
        gdaltest.post_reason('Image too different from the reference')
        return 'fail'
    ds_ref = None
    ds = None

    #################
    # Potentially test optimized AVX implementation

    outfiles.append('tmp/grid_invdist_avx.tif')
    try:
        os.remove(outfiles[-1])
    except:
        pass

    # Create a GDAL dataset from the values of "grid.csv".
    print('Step 3: Trying AVX optimized version...')
    (out, err) = gdaltest.runexternal_out_and_err(gdal_grid + ' --debug on -txe 440720.0 441920.0 -tye 3751320.0 3750120.0 -outsize 20 20 -ot Float64 -l grid -a invdist:power=2.0:smoothing=0.0:radius1=0.0:radius2=0.0:angle=0.0:max_points=0:min_points=0:nodata=0.0 data/grid.vrt ' + outfiles[-1])
    if err.find('AVX') >= 0:
        print('...AVX optimized version used')
    else:
        print('...AVX optimized version NOT used')

    # We should get the same values as in "ref_data/gdal_invdist.tif"
    ds = gdal.Open(outfiles[-1])
    ds_ref = gdal.Open('ref_data/grid_invdist.tif')
    maxdiff = gdaltest.compare_ds(ds, ds_ref, verbose = 0)
    if maxdiff > 1:
        gdaltest.compare_ds(ds, ds_ref, verbose = 1)
        gdaltest.post_reason('Image too different from the reference')
        return 'fail'
    ds_ref = None
    ds = None
    
    #################
    # Test GDAL_NUM_THREADS config option to 1

    outfiles.append('tmp/grid_invdist_1thread.tif')
    try:
        os.remove(outfiles[-1])
    except:
        pass

    # Create a GDAL dataset from the values of "grid.csv".
    gdaltest.runexternal(gdal_grid + ' --config GDAL_NUM_THREADS 1 -txe 440720.0 441920.0 -tye 3751320.0 3750120.0 -outsize 20 20 -ot Float64 -l grid -a invdist:power=2.0:smoothing=0.0:radius1=0.0:radius2=0.0:angle=0.0:max_points=0:min_points=0:nodata=0.0 data/grid.vrt ' + outfiles[-1])

    # We should get the same values as in "ref_data/gdal_invdist.tif"
    ds = gdal.Open(outfiles[-1])
    ds_ref = gdal.Open('ref_data/grid_invdist.tif')
    maxdiff = gdaltest.compare_ds(ds, ds_ref, verbose = 0)
    if maxdiff > 1:
        gdaltest.compare_ds(ds, ds_ref, verbose = 1)
        gdaltest.post_reason('Image too different from the reference')
        return 'fail'
    ds_ref = None
    ds = None

    #################
    # Test GDAL_NUM_THREADS config option to 2

    outfiles.append('tmp/grid_invdist_2threads.tif')
    try:
        os.remove(outfiles[-1])
    except:
        pass

    # Create a GDAL dataset from the values of "grid.csv".
    gdaltest.runexternal(gdal_grid + ' --config GDAL_NUM_THREADS 2 -txe 440720.0 441920.0 -tye 3751320.0 3750120.0 -outsize 20 20 -ot Float64 -l grid -a invdist:power=2.0:smoothing=0.0:radius1=0.0:radius2=0.0:angle=0.0:max_points=0:min_points=0:nodata=0.0 data/grid.vrt ' + outfiles[-1])

    # We should get the same values as in "ref_data/gdal_invdist.tif"
    ds = gdal.Open(outfiles[-1])
    ds_ref = gdal.Open('ref_data/grid_invdist.tif')
    maxdiff = gdaltest.compare_ds(ds, ds_ref, verbose = 0)
    if maxdiff > 1:
        gdaltest.compare_ds(ds, ds_ref, verbose = 1)
        gdaltest.post_reason('Image too different from the reference')
        return 'fail'
    ds_ref = None
    ds = None

    #################
    outfiles.append('tmp/grid_invdist_90_90_8p.tif')
    try:
        os.remove(outfiles[-1])
    except:
        pass

    # Create a GDAL dataset from the values of "grid.csv".
    # Circular window, shifted, test min points and NODATA setting.
    gdaltest.runexternal(gdal_grid + ' -txe 440721.0 441920.0 -tye 3751321.0 3750120.0 -outsize 20 20 -ot Float64 -l grid -a invdist:power=2.0:radius1=90.0:radius2=90.0:angle=0.0:max_points=0:min_points=8:nodata=0.0 data/grid.vrt ' + outfiles[-1])

    # We should get the same values as in "ref_data/grid_invdist_90_90_8p.tif"
    ds = gdal.Open(outfiles[-1])
    ds_ref = gdal.Open('ref_data/grid_invdist_90_90_8p.tif')
    maxdiff = gdaltest.compare_ds(ds, ds_ref, verbose = 0)
    if maxdiff > 1:
        gdaltest.compare_ds(ds, ds_ref, verbose = 1)
        gdaltest.post_reason('Image too different from the reference')
        return 'fail'
    ds_ref = None
    ds = None

    return 'success'
示例#50
0
def test_gdal_contour_1():
    if test_cli_utilities.get_gdal_contour_path() is None:
        pytest.skip()

    try:
        os.remove('tmp/contour.shp')
    except OSError:
        pass
    try:
        os.remove('tmp/contour.dbf')
    except OSError:
        pass
    try:
        os.remove('tmp/contour.shx')
    except OSError:
        pass

    drv = gdal.GetDriverByName('GTiff')
    sr = osr.SpatialReference()
    sr.ImportFromEPSG(4326)
    wkt = sr.ExportToWkt()

    size = 160
    precision = 1. / size

    ds = drv.Create('tmp/gdal_contour.tif', size, size, 1)
    ds.SetProjection(wkt)
    ds.SetGeoTransform([1, precision, 0, 50, 0, -precision])

    raw_data = array.array('h', [10 for i in range(int(size / 2))]).tostring()
    for i in range(int(size / 2)):
        ds.WriteRaster(int(size / 4),
                       i + int(size / 4),
                       int(size / 2),
                       1,
                       raw_data,
                       buf_type=gdal.GDT_Int16,
                       band_list=[1])

    raw_data = array.array('h', [20 for i in range(int(size / 2))]).tostring()
    for i in range(int(size / 4)):
        ds.WriteRaster(int(size / 4) + int(size / 8),
                       i + int(size / 4) + int(size / 8),
                       int(size / 4),
                       1,
                       raw_data,
                       buf_type=gdal.GDT_Int16,
                       band_list=[1])

    raw_data = array.array('h', [25 for i in range(int(size / 4))]).tostring()
    for i in range(int(size / 8)):
        ds.WriteRaster(int(size / 4) + int(size / 8) + int(size / 16),
                       i + int(size / 4) + int(size / 8) + int(size / 16),
                       int(size / 8),
                       1,
                       raw_data,
                       buf_type=gdal.GDT_Int16,
                       band_list=[1])

    ds = None

    (_, err) = gdaltest.runexternal_out_and_err(
        test_cli_utilities.get_gdal_contour_path() +
        ' -a elev -i 10 tmp/gdal_contour.tif tmp/contour.shp')
    assert (err is None or err == ''), 'got error/warning'

    ds = ogr.Open('tmp/contour.shp')

    expected_envelopes = [[1.25, 1.75, 49.25, 49.75],
                          [
                              1.25 + 0.125, 1.75 - 0.125, 49.25 + 0.125,
                              49.75 - 0.125
                          ]]
    expected_height = [10, 20]

    lyr = ds.ExecuteSQL("select * from contour order by elev asc")

    assert lyr.GetSpatialRef().ExportToWkt(
    ) == wkt, 'Did not get expected spatial ref'

    assert lyr.GetFeatureCount() == len(expected_envelopes)

    i = 0
    feat = lyr.GetNextFeature()
    while feat is not None:
        envelope = feat.GetGeometryRef().GetEnvelope()
        assert feat.GetField('elev') == expected_height[i]
        for j in range(4):
            if abs(expected_envelopes[i][j] -
                   envelope[j]) > precision / 2 * 1.001:
                print('i=%d, wkt=%s' %
                      (i, feat.GetGeometryRef().ExportToWkt()))
                print(feat.GetGeometryRef().GetEnvelope())
                pytest.fail(
                    '%f, %f' %
                    (expected_envelopes[i][j] - envelope[j], precision / 2))
        i = i + 1
        feat = lyr.GetNextFeature()

    ds.ReleaseResultSet(lyr)
    ds.Destroy()
示例#51
0
def test_gdaltindex_1():
    if test_cli_utilities.get_gdaltindex_path() is None:
        return 'skip'

    try:
        os.remove('tmp/tileindex.shp')
    except:
        pass
    try:
        os.remove('tmp/tileindex.dbf')
    except:
        pass
    try:
        os.remove('tmp/tileindex.shx')
    except:
        pass
    try:
        os.remove('tmp/tileindex.prj')
    except:
        pass

    drv = gdal.GetDriverByName('GTiff')
    wkt = 'GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9108\"]],AUTHORITY[\"EPSG\",\"4326\"]]'

    ds = drv.Create('tmp/gdaltindex1.tif', 10, 10, 1)
    ds.SetProjection( wkt )
    ds.SetGeoTransform( [ 49, 0.1, 0, 2, 0, -0.1 ] )
    ds = None

    ds = drv.Create('tmp/gdaltindex2.tif', 10, 10, 1)
    ds.SetProjection( wkt )
    ds.SetGeoTransform( [ 49, 0.1, 0, 3, 0, -0.1 ] )
    ds = None

    ds = drv.Create('tmp/gdaltindex3.tif', 10, 10, 1)
    ds.SetProjection( wkt )
    ds.SetGeoTransform( [ 48, 0.1, 0, 2, 0, -0.1 ] )
    ds = None

    ds = drv.Create('tmp/gdaltindex4.tif', 10, 10, 1)
    ds.SetProjection( wkt )
    ds.SetGeoTransform( [ 48, 0.1, 0, 3, 0, -0.1 ] )
    ds = None

    (out, err) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdaltindex_path() + ' tmp/tileindex.shp tmp/gdaltindex1.tif tmp/gdaltindex2.tif')
    if not (err is None or err == '') :
        gdaltest.post_reason('got error/warning')
        print(err)
        return 'fail'
    gdaltest.runexternal(test_cli_utilities.get_gdaltindex_path() + ' tmp/tileindex.shp tmp/gdaltindex3.tif tmp/gdaltindex4.tif')

    ds = ogr.Open('tmp/tileindex.shp')
    if ds.GetLayer(0).GetFeatureCount() != 4:
        return 'fail'
    tileindex_wkt = ds.GetLayer(0).GetSpatialRef().ExportToWkt()
    if tileindex_wkt.find('GCS_WGS_1984') == -1:
        return 'fail'

    expected_wkts =['POLYGON ((49 2,50 2,50 1,49 1,49 2))',
                    'POLYGON ((49 3,50 3,50 2,49 2,49 3))',
                    'POLYGON ((48 2,49 2,49 1,48 1,48 2))',
                    'POLYGON ((48 3,49 3,49 2,48 2,48 3))' ]
    i = 0
    feat = ds.GetLayer(0).GetNextFeature()
    while feat is not None:
        if feat.GetGeometryRef().ExportToWkt() != expected_wkts[i]:
            print('i=%d, wkt=%s' % (i, feat.GetGeometryRef().ExportToWkt()))
            return 'fail'
        i = i + 1
        feat = ds.GetLayer(0).GetNextFeature()
    ds.Destroy()

    return 'success'
示例#52
0
def test_gdal_viewshed_invalid_input():

    _, err = gdaltest.runexternal_out_and_err(
        test_cli_utilities.get_gdal_viewshed_path() +
        ' -ox 0 -oy 0 /dev/null /dev/null')
    assert ('not recognized as' in err) or ('No such file or directory' in err)
def vrtderived_7():

    import test_cli_utilities
    if test_cli_utilities.get_gdalinfo_path() is None:
        return 'skip'

    ret, err = gdaltest.runexternal_out_and_err(
        test_cli_utilities.get_gdalinfo_path() +
        ' -checksum data/n43_hillshade.vrt --config GDAL_VRT_ENABLE_PYTHON YES'
    )
    if gdal.GetConfigOption('CPL_DEBUG') is not None:
        print(err)
    # Either we cannot find a Python library, either it works
    if ret.find('Checksum=0') >= 0:
        print('Did not manage to find a Python library')
    elif ret.find('Checksum=50577') < 0:
        gdaltest.post_reason('fail')
        print(ret)
        print(err)
        return 'fail'

    ret, err = gdaltest.runexternal_out_and_err(
        test_cli_utilities.get_gdalinfo_path() +
        ' -checksum data/n43_hillshade.vrt --config GDAL_VRT_ENABLE_PYTHON YES --config VRT_ENABLE_PYTHON_PATH NO'
    )
    if gdal.GetConfigOption('CPL_DEBUG') is not None:
        print(err)
# Either we cannot find a Python library, either it works
    if ret.find('Checksum=0') >= 0:
        print('Did not manage to find a Python library')
    elif ret.find('Checksum=50577') < 0:
        gdaltest.post_reason('fail')
        print(ret)
        print(err)
        return 'fail'

    ret, err = gdaltest.runexternal_out_and_err(
        test_cli_utilities.get_gdalinfo_path() +
        ' -checksum data/n43_hillshade.vrt --config GDAL_VRT_ENABLE_PYTHON YES --config VRT_ENABLE_PYTHON_SYMLINK NO'
    )
    if gdal.GetConfigOption('CPL_DEBUG') is not None:
        print(err)
# Either we cannot find a Python library, either it works
    if ret.find('Checksum=0') >= 0:
        print('Did not manage to find a Python library')
    elif ret.find('Checksum=50577') < 0:
        gdaltest.post_reason('fail')
        print(ret)
        print(err)
        return 'fail'

    # Invalid shared object name
    ret, err = gdaltest.runexternal_out_and_err(
        test_cli_utilities.get_gdalinfo_path() +
        ' -checksum data/n43_hillshade.vrt --config GDAL_VRT_ENABLE_PYTHON YES --config PYTHONSO foo'
    )
    if gdal.GetConfigOption('CPL_DEBUG') is not None:
        print(err)
    if ret.find('Checksum=0') < 0:
        gdaltest.post_reason('fail')
        print(ret)
        print(err)
        return 'fail'

    # Valid shared object name, but without Python symbols
    libgdal_so = gdaltest.find_lib('gdal')
    if libgdal_so is not None:
        ret, err = gdaltest.runexternal_out_and_err(
            test_cli_utilities.get_gdalinfo_path() +
            ' -checksum data/n43_hillshade.vrt --config GDAL_VRT_ENABLE_PYTHON YES --config PYTHONSO "%s"'
            % libgdal_so)
        if gdal.GetConfigOption('CPL_DEBUG') is not None:
            print(err)
        if ret.find('Checksum=0') < 0:
            gdaltest.post_reason('fail')
            print(ret)
            print(err)
            return 'fail'

    return 'success'