Beispiel #1
0
def test_gdal_retile_2():

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

    try:
        os.mkdir('tmp/outretile2')
    except:
        pass

    test_py_scripts.run_py_script(script_path, 'gdal_retile', '-v -levels 2 -r bilinear -targetDir tmp/outretile2 ../gcore/data/rgba.tif' )

    ds = gdal.Open('tmp/outretile2/2/rgba_1_1.tif')
    if ds.GetRasterBand(1).Checksum() != 35:
        gdaltest.post_reason('wrong checksum for band 1')
        print(ds.GetRasterBand(1).Checksum())
        return 'fail'
    if ds.GetRasterBand(4).Checksum() != 35:
        gdaltest.post_reason('wrong checksum for band 4')
        print(ds.GetRasterBand(4).Checksum())
        return 'fail'
    ds = None

    return 'success'
Beispiel #2
0
def test_gdal_edit_py_3():

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

    shutil.copy('../gcore/data/byte.tif', 'tmp/test_gdal_edit_py.tif')

    test_py_scripts.run_py_script(script_path, 'gdal_edit', "tmp/test_gdal_edit_py.tif -a_srs ''")

    ds = gdal.Open('tmp/test_gdal_edit_py.tif')
    wkt = ds.GetProjectionRef()
    gt = ds.GetGeoTransform()
    ds = None

    if gt == (0.0, 1.0, 0.0, 0.0, 0.0, 1.0):
        gdaltest.post_reason('fail')
        print(gt)
        return 'fail'

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

    return 'success'
Beispiel #3
0
def test_ogr2ogr_py_32():

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

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

    test_py_scripts.run_py_script(script_path, 'ogr2ogr', ' tmp/test_ogr2ogr_32.shp ../ogr/data/poly.shp')
    test_py_scripts.run_py_script(script_path, 'ogr2ogr', ' -append tmp/test_ogr2ogr_32.shp ../ogr/data/poly.shp')

    ds = ogr.Open('tmp/test_ogr2ogr_32.shp')
    if ds is None or ds.GetLayer(0).GetFeatureCount() != 20:
        gdaltest.post_reason('-append failed')
        return 'fail'
    ds = None

    test_py_scripts.run_py_script(script_path, 'ogr2ogr', ' -overwrite tmp/test_ogr2ogr_32.shp ../ogr/data/poly.shp')

    ds = ogr.Open('tmp/test_ogr2ogr_32.shp')
    if ds is None or ds.GetLayer(0).GetFeatureCount() != 10:
        gdaltest.post_reason('-overwrite failed')
        return 'fail'
    ds = None

    ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('tmp/test_ogr2ogr_32.shp')
    return 'success'
Beispiel #4
0
def test_ogr2ogr_py_37():

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

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

    try:
        os.mkdir('tmp/test_ogr2ogr_37_src')
    except:
        pass
    shutil.copy('../ogr/data/poly.shp', 'tmp/test_ogr2ogr_37_src')
    shutil.copy('../ogr/data/poly.shx', 'tmp/test_ogr2ogr_37_src')
    shutil.copy('../ogr/data/poly.dbf', 'tmp/test_ogr2ogr_37_src')
    shutil.copy('../ogr/data/testpoly.shp', 'tmp/test_ogr2ogr_37_src')
    shutil.copy('../ogr/data/testpoly.shx', 'tmp/test_ogr2ogr_37_src')
    shutil.copy('../ogr/data/testpoly.dbf', 'tmp/test_ogr2ogr_37_src')

    test_py_scripts.run_py_script(script_path, 'ogr2ogr', ' tmp/test_ogr2ogr_37_dir.shp tmp/test_ogr2ogr_37_src')

    ds = ogr.Open('tmp/test_ogr2ogr_37_dir.shp')
    if ds is None or ds.GetLayerCount() != 2:
        return 'fail'
    ds = None

    ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('tmp/test_ogr2ogr_37_src')
    ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('tmp/test_ogr2ogr_37_dir.shp')

    return 'success'
Beispiel #5
0
def test_ogr2ogr_py_26():
    script_path = test_py_scripts.get_py_script('ogr2ogr')
    if script_path is None:
        return 'skip'
        
    if not ogrtest.have_geos():
        return 'skip'

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

    test_py_scripts.run_py_script(script_path, 'ogr2ogr', 'tmp/poly.shp ../ogr/data/poly.shp -clipdst "POLYGON((479609 4764629,479609 4764817,479764 4764817,479764 4764629,479609 4764629))"')

    ds = ogr.Open('tmp/poly.shp')
    if ds is None or ds.GetLayer(0).GetFeatureCount() != 4:
        return 'fail'
        
    if ds.GetLayer(0).GetExtent() != (479609, 479764, 4764629, 4764817):
        print(ds.GetLayer(0).GetExtent())
        gdaltest.post_reason('unexpected extent')
        return 'fail'
        
    ds.Destroy()

    ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('tmp/poly.shp')

    return 'success'
Beispiel #6
0
def test_gdal_pansharpen_1():

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

    src_ds = gdal.Open('../gdrivers/data/small_world.tif')
    src_data = src_ds.GetRasterBand(1).ReadRaster()
    gt = src_ds.GetGeoTransform()
    wkt = src_ds.GetProjectionRef()
    src_ds = None
    pan_ds = gdal.GetDriverByName('GTiff').Create('tmp/small_world_pan.tif', 800, 400)
    gt = [ gt[i] for i in range(len(gt)) ]
    gt[1] *= 0.5
    gt[5] *= 0.5
    pan_ds.SetGeoTransform(gt)
    pan_ds.SetProjection(wkt)
    pan_ds.GetRasterBand(1).WriteRaster(0,0,800,400,src_data,400,200)
    pan_ds = None

    test_py_scripts.run_py_script(script_path, 'gdal_pansharpen', ' tmp/small_world_pan.tif ../gdrivers/data/small_world.tif tmp/out.tif')

    ds = gdal.Open('tmp/out.tif')
    cs = [ ds.GetRasterBand(i+1).Checksum() for i in range(ds.RasterCount) ]
    ds = None
    gdal.GetDriverByName('GTiff').Delete('tmp/out.tif')

    if cs != [4735, 10000, 9742]:
        gdaltest.post_reason('fail')
        print(cs)
        return 'fail'

    return 'success'
Beispiel #7
0
def test_ogr2ogr_py_3():
    script_path = test_py_scripts.get_py_script('ogr2ogr')
    if script_path is None:
        return 'skip'

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

    test_py_scripts.run_py_script(script_path, 'ogr2ogr', 'tmp/poly.shp ../ogr/data/poly.shp -spat 479609 4764629 479764 4764817')

    ds = ogr.Open('tmp/poly.shp')
    if ogrtest.have_geos():
        if ds is None or ds.GetLayer(0).GetFeatureCount() != 4:
            return 'fail'
    else:
        if ds is None or ds.GetLayer(0).GetFeatureCount() != 5:
            return 'fail'
    ds.Destroy()

    ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('tmp/poly.shp')

    return 'success'
Beispiel #8
0
def test_ogr2ogr_py_23():
    script_path = test_py_scripts.get_py_script('ogr2ogr')
    if script_path is None:
        return 'skip'
    if test_cli_utilities.get_ogr2ogr_path() is None:
        return 'skip'

    gdaltest.runexternal(test_cli_utilities.get_ogr2ogr_path() +
        ' -f "MapInfo File" tmp/testogr2ogr23.mif ../utilities/data/dataforogr2ogr21.csv ' +
        '-sql "SELECT comment, name FROM dataforogr2ogr21" -select comment,name -nlt POINT')
    ds = ogr.Open('tmp/testogr2ogr23.mif')

    if ds is None:
        return 'fail'
    layer_defn = ds.GetLayer(0).GetLayerDefn()
    lyr = ds.GetLayer(0)
    feat = lyr.GetNextFeature()
    if feat.GetFieldAsString('name') != 'NAME' or \
       feat.GetFieldAsString('comment') != 'COMMENT':
        print(feat.GetFieldAsString('name'))
        print(feat.GetFieldAsString('comment'))
        ds.Destroy()
        ogr.GetDriverByName('MapInfo File').DeleteDataSource('tmp/testogr2ogr23.mif')
        return 'fail'

    ds.Destroy()
    ogr.GetDriverByName('MapInfo File').DeleteDataSource('tmp/testogr2ogr23.mif')

    return 'success'
Beispiel #9
0
def test_ogr2ogr_py_36():

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

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

    test_py_scripts.run_py_script(script_path, 'ogr2ogr', ' tmp/test_ogr2ogr_36.shp ../ogr/data/poly.shp -zfield EAS_ID')

    ds = ogr.Open('tmp/test_ogr2ogr_36.shp')
    feat = ds.GetLayer(0).GetNextFeature()
    wkt = feat.GetGeometryRef().ExportToWkt()
    ds = None

    ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('tmp/test_ogr2ogr_36.shp')

    if wkt.find(' 168,') == -1:
        return 'fail'

    return 'success'
Beispiel #10
0
def test_gdal_calc_py_3():

    if gdalnumeric_not_available:
        return 'skip'

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

    test_py_scripts.run_py_script(script_path, 'gdal_calc', '-A tmp/test_gdal_calc_py.tif --allBands A --calc=A --overwrite --outfile tmp/test_gdal_calc_py_3.tif')

    ds = gdal.Open('tmp/test_gdal_calc_py_3.tif')

    if ds is None:
        gdaltest.post_reason('ds not found')
        return 'fail'
    if ds.GetRasterBand(1).Checksum() != 12603:
        gdaltest.post_reason('band 1 wrong checksum')
        return 'fail'
    if ds.GetRasterBand(2).Checksum() != 58561:
        gdaltest.post_reason('band 2 wrong checksum')
        return 'fail'
    if ds.GetRasterBand(3).Checksum() != 36064:
        gdaltest.post_reason('band 3 wrong checksum')
        return 'fail'
    if ds.GetRasterBand(4).Checksum() != 10807:
        gdaltest.post_reason('band 4 wrong checksum')
        return 'fail'

    ds = None

    return 'success'
Beispiel #11
0
def test_ogr2ogr_py_21():
    script_path = test_py_scripts.get_py_script('ogr2ogr')
    if script_path is None:
        return 'skip'

    try:
        os.remove('tmp/testogr2ogr21.gtm')
    except:
        pass

    test_py_scripts.run_py_script(script_path, 'ogr2ogr', \
        '-f GPSTrackMaker tmp/testogr2ogr21.gtm ../utilities/data/dataforogr2ogr21.csv ' +
        '-sql "SELECT comment, name FROM dataforogr2ogr21" -nlt POINT')
    ds = ogr.Open('tmp/testogr2ogr21.gtm')

    if ds is None:
        return 'fail'
    layer_defn = ds.GetLayer(0).GetLayerDefn()
    lyr = ds.GetLayer(0)
    feat = lyr.GetNextFeature()
    if feat.GetFieldAsString('name') != 'NAME' or \
       feat.GetFieldAsString('comment') != 'COMMENT':
        print(feat.GetFieldAsString('name'))
        print(feat.GetFieldAsString('comment'))
        ds.Destroy()
        os.remove('tmp/testogr2ogr21.gtm')
        return 'fail'

    ds.Destroy()
    os.remove('tmp/testogr2ogr21.gtm')

    return 'success'
Beispiel #12
0
def test_ogr2ogr_py_1():
    script_path = test_py_scripts.get_py_script('ogr2ogr')
    if script_path is None:
        return 'skip'

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

    test_py_scripts.run_py_script(script_path, 'ogr2ogr', 'tmp/poly.shp ../ogr/data/poly.shp')
    ds = ogr.Open('tmp/poly.shp')
    if ds is None or ds.GetLayer(0).GetFeatureCount() != 10:
        return 'fail'

    feat0 = ds.GetLayer(0).GetFeature(0)
    if feat0.GetFieldAsDouble('AREA') != 215229.266:
        print(feat0.GetFieldAsDouble('AREA'))
        gdaltest.post_reason('Did not get expected value for field AREA')
        return 'fail'
    if feat0.GetFieldAsString('PRFEDEA') != '35043411':
        print(feat0.GetFieldAsString('PRFEDEA'))
        gdaltest.post_reason('Did not get expected value for field PRFEDEA')
        return 'fail'
        
    ds.Destroy()
    
    ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('tmp/poly.shp')

    return 'success'
Beispiel #13
0
def test_pct2rgb_1():
    try:
        from osgeo import gdalnumeric
        gdalnumeric.BandRasterIONumPy
    except:
        return 'skip'

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

    test_py_scripts.run_py_script(script_path, 'pct2rgb', 'tmp/test_rgb2pct_1.tif tmp/test_pct2rgb_1.tif' )

    ds = gdal.Open('tmp/test_pct2rgb_1.tif')
    if ds.GetRasterBand(1).Checksum() != 20963:
        print(ds.GetRasterBand(1).Checksum())
        return 'fail'

    ori_ds = gdal.Open('../gcore/data/rgbsmall.tif')
    max_diff = gdaltest.compare_ds(ori_ds, ds)
    if max_diff > 18:
        return 'fail'

    ds = None
    ori_ds = None

    return 'success'
def test_gdalinfo_py_8():
    script_path = test_py_scripts.get_py_script('gdalinfo')
    if script_path is None:
        return 'skip'

    try:
        os.remove('../gcore/data/byte.tif.aux.xml')
    except:
        pass

    ret = test_py_scripts.run_py_script(script_path, 'gdalinfo', '../gcore/data/byte.tif')
    if ret.find('0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 37 0 0 0 0 0 0 0 57 0 0 0 0 0 0 0 62 0 0 0 0 0 0 0 66 0 0 0 0 0 0 0 0 72 0 0 0 0 0 0 0 31 0 0 0 0 0 0 0 24 0 0 0 0 0 0 0 12 0 0 0 0 0 0 0 0 7 0 0 0 0 0 0 0 12 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1') != -1:
        gdaltest.post_reason( 'did not expect histogram.' )
        print(ret)
        return 'fail'

    ret = test_py_scripts.run_py_script(script_path, 'gdalinfo', '-hist ../gcore/data/byte.tif')
    if ret.find('0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 37 0 0 0 0 0 0 0 57 0 0 0 0 0 0 0 62 0 0 0 0 0 0 0 66 0 0 0 0 0 0 0 0 72 0 0 0 0 0 0 0 31 0 0 0 0 0 0 0 24 0 0 0 0 0 0 0 12 0 0 0 0 0 0 0 0 7 0 0 0 0 0 0 0 12 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1') == -1:
        gdaltest.post_reason( 'did not get expected histogram.' )
        print(ret)
        return 'fail'

    # We will blow an exception if the file does not exist now!
    os.remove('../gcore/data/byte.tif.aux.xml')

    return 'success'
Beispiel #15
0
def test_gdal_proximity_1():

    script_path = test_py_scripts.get_py_script('gdal_proximity')
    if script_path is None:
        return 'skip'
    
    drv = gdal.GetDriverByName( 'GTiff' )
    dst_ds = drv.Create('tmp/proximity_1.tif', 25, 25, 1, gdal.GDT_Byte )
    dst_ds = None

    test_py_scripts.run_py_script(script_path, 'gdal_proximity', '../alg/data/pat.tif tmp/proximity_1.tif' )

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

    cs_expected = 1941
    cs = dst_band.Checksum()
    
    dst_band = None
    dst_ds = None

    if cs != cs_expected:
        print('Got: ', cs)
        gdaltest.post_reason( 'got wrong checksum' )
        return 'fail'
    else:
        return 'success' 
Beispiel #16
0
def test_ogr2ogr_py_7():

    import ogr_pg

    script_path = test_py_scripts.get_py_script('ogr2ogr')
    if script_path is None:
        return 'skip'
    if test_cli_utilities.get_ogrinfo_path() is None:
        return 'skip'

    import gdal
    ogr_pg.ogr_pg_1()
    if gdaltest.pg_ds is None:
        return 'skip'
    gdaltest.pg_ds.Destroy()

    gdaltest.runexternal(test_cli_utilities.get_ogrinfo_path() + ' PG:"' + gdaltest.pg_connection_string + '" -sql "DELLAYER:tpoly"')

    test_py_scripts.run_py_script(script_path, 'ogr2ogr', '-f PostgreSQL PG:"' + gdaltest.pg_connection_string + '" ../ogr/data/poly.shp -nln tpoly -gt 1')

    ds = ogr.Open('PG:' + gdaltest.pg_connection_string)
    if ds is None or ds.GetLayerByName('tpoly').GetFeatureCount() != 10:
        return 'fail'
    ds.Destroy()

    gdaltest.runexternal(test_cli_utilities.get_ogrinfo_path() + ' PG:"' + gdaltest.pg_connection_string + '" -sql "DELLAYER:tpoly"')

    return 'success'
Beispiel #17
0
def test_ogrinfo_py_9():
    script_path = test_py_scripts.get_py_script('ogrinfo')
    if script_path is None:
        pytest.skip()

    ret = test_py_scripts.run_py_script(script_path, 'ogrinfo', '../ogr/data/poly.shp poly -where "EAS_ID=171"')
    assert ret.find('Feature Count: 1') != -1
Beispiel #18
0
def test_ogr2ogr_py_15():

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

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

    test_py_scripts.run_py_script(script_path, 'ogr2ogr', 'tmp/poly.shp ../ogr/data/poly.shp')

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

    # Overwrite
    test_py_scripts.run_py_script(script_path, 'ogr2ogr', '-overwrite tmp ../ogr/data/poly.shp')

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

    ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('tmp/poly.shp')
    return 'success'
Beispiel #19
0
def test_ogrinfo_py_2():
    script_path = test_py_scripts.get_py_script('ogrinfo')
    if script_path is None:
        pytest.skip()

    ret = test_py_scripts.run_py_script(script_path, 'ogrinfo', '-ro ../ogr/data/poly.shp')
    assert ret.find('ESRI Shapefile') != -1
Beispiel #20
0
def test_ogrinfo_py_10():
    script_path = test_py_scripts.get_py_script('ogrinfo')
    if script_path is None:
        pytest.skip()

    ret = test_py_scripts.run_py_script(script_path, 'ogrinfo', '../ogr/data/poly.shp poly -fid 9')
    assert ret.find('OGRFeature(poly):9') != -1
Beispiel #21
0
def test_ogrinfo_py_3():
    script_path = test_py_scripts.get_py_script('ogrinfo')
    if script_path is None:
        pytest.skip()

    ret = test_py_scripts.run_py_script(script_path, 'ogrinfo', '-al ../ogr/data/poly.shp')
    assert ret.find('Feature Count: 10') != -1
Beispiel #22
0
def test_ogrinfo_py_5():
    script_path = test_py_scripts.get_py_script('ogrinfo')
    if script_path is None:
        pytest.skip()

    ret = test_py_scripts.run_py_script(script_path, 'ogrinfo', '../ogr/data/poly.shp -sql "select * from poly"')
    assert ret.find('Feature Count: 10') != -1
def test_gdalinfo_py_5():
    script_path = test_py_scripts.get_py_script('gdalinfo')
    if script_path is None:
        return 'skip'

    try:
        os.remove('../gcore/data/byte.tif.aux.xml')
    except:
        pass

    ret = test_py_scripts.run_py_script(script_path, 'gdalinfo', '../gcore/data/byte.tif')
    if ret.find('STATISTICS_MINIMUM=74') != -1:
        gdaltest.post_reason( 'got wrong minimum.' )
        print(ret)
        return 'fail'

    ret = test_py_scripts.run_py_script(script_path, 'gdalinfo', '-stats ../gcore/data/byte.tif')
    if ret.find('STATISTICS_MINIMUM=74') == -1:
        gdaltest.post_reason( 'got wrong minimum (2).' )
        print(ret)
        return 'fail'

    # We will blow an exception if the file does not exist now!
    os.remove('../gcore/data/byte.tif.aux.xml')

    return 'success'
Beispiel #24
0
def test_gdal_edit_py_7():

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

    gdal.Translate('tmp/test_gdal_edit_py.tif',
                   '../gcore/data/byte.tif',
                   options = '-b 1 -b 1 -b 1 -b 1 -co PHOTOMETRIC=RGB -co ALPHA=NO')

    test_py_scripts.run_py_script(script_path, 'gdal_edit', "tmp/test_gdal_edit_py.tif -colorinterp_4 alpha")

    ds = gdal.Open('tmp/test_gdal_edit_py.tif')
    if ds.GetRasterBand(4).GetColorInterpretation() != gdal.GCI_AlphaBand:
        gdaltest.post_reason('fail')
        return 'fail'

    test_py_scripts.run_py_script(script_path, 'gdal_edit', "tmp/test_gdal_edit_py.tif -colorinterp_4 undefined")

    ds = gdal.Open('tmp/test_gdal_edit_py.tif')
    if ds.GetRasterBand(4).GetColorInterpretation() != gdal.GCI_Undefined:
        gdaltest.post_reason('fail')
        return 'fail'

    return 'success'
Beispiel #25
0
def test_ogrmerge_11():
    script_path = test_py_scripts.get_py_script('ogrmerge')
    if script_path is None:
        return 'skip'

    test_py_scripts.run_py_script(script_path, 'ogrmerge',
        '-f VRT -o /vsimem/out.vrt ../ogr/data/poly.shp '
        '-a_srs EPSG:32630')

    ds = ogr.Open('/vsimem/out.vrt')
    if ds is None:
        gdaltest.post_reason('fail')
        return 'fail'
    ds = None

    f = gdal.VSIFOpenL('/vsimem/out.vrt', 'rb')
    content = ''
    if f is not None:
        content = gdal.VSIFReadL(1, 10000, f).decode('UTF-8')
        gdal.VSIFCloseL(f)
    gdal.Unlink('/vsimem/out.vrt')

    if content.find('<LayerSRS>EPSG:32630</LayerSRS>') < 0:
        gdaltest.post_reason('fail')
        print(content)
        return 'fail'

    return 'success'
Beispiel #26
0
def test_ogr2ogr_py_16():
    script_path = test_py_scripts.get_py_script('ogr2ogr')
    if script_path is None:
        return 'skip'

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

    test_py_scripts.run_py_script(script_path, 'ogr2ogr', '-fid 8 tmp/poly.shp ../ogr/data/poly.shp')

    src_ds = ogr.Open('../ogr/data/poly.shp')
    ds = ogr.Open('tmp/poly.shp')
    if ds is None or ds.GetLayer(0).GetFeatureCount() != 1:
        return 'fail'
    src_feat = src_ds.GetLayer(0).GetFeature(8)
    feat = ds.GetLayer(0).GetNextFeature()
    if feat.GetField("EAS_ID") != src_feat.GetField("EAS_ID"):
        return 'fail'
    ds.Destroy()
    src_ds.Destroy()

    ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('tmp/poly.shp')
    return 'success'
Beispiel #27
0
def test_ogrmerge_5():
    script_path = test_py_scripts.get_py_script('ogrmerge')
    if script_path is None:
        return 'skip'

    test_py_scripts.run_py_script(script_path, 'ogrmerge',
        '-f VRT -o /vsimem/out.vrt ../ogr/data/poly.shp ../ogr/data/testpoly.shp -nln '
        '"foo_{DS_NAME}_{DS_BASENAME}_{DS_INDEX}_{LAYER_NAME}_{LAYER_INDEX}"')

    ds = ogr.Open('/vsimem/out.vrt')
    lyr = ds.GetLayer(0)
    if lyr.GetName() != 'foo_../ogr/data/poly.shp_poly_0_poly_0':
        gdaltest.post_reason('fail')
        print(lyr.GetName())
        return 'fail'
    if lyr.GetFeatureCount() != 10:
        gdaltest.post_reason('fail')
        return 'fail'
    lyr = ds.GetLayer(1)
    if lyr.GetName() != 'foo_../ogr/data/testpoly.shp_testpoly_1_testpoly_0':
        gdaltest.post_reason('fail')
        print(lyr.GetName())
        return 'fail'
    if lyr.GetFeatureCount() != 14:
        gdaltest.post_reason('fail')
        return 'fail'
    ds = None

    gdal.Unlink('/vsimem/out.vrt')

    return 'success'
Beispiel #28
0
def run_gdal_ls(argv):
    script_path = test_py_scripts.get_py_script('gdal_ls')
    if script_path is None:
        return ('skip', None)

    saved_syspath = sys.path
    sys.path.append(script_path)
    try:
        import gdal_ls
    except ImportError:
        sys.path = saved_syspath
        return ('fail', None)

    sys.path = saved_syspath

    from sys import version_info
    if version_info >= (3, 0, 0):
        import io
        outstr = io.StringIO()
    else:
        import StringIO
        outstr = StringIO.StringIO()
    ret = gdal_ls.gdal_ls(argv, outstr)
    retstr = outstr.getvalue()
    outstr.close()

    if ret != 0:
        gdaltest.post_reason('got error code : %d' % ret)
        return ('fail', 'None')

    return ('success', retstr)
Beispiel #29
0
def test_gdal_calc_py_6():

    if gdalnumeric_not_available:
        gdaltest.post_reason('gdalnumeric is not available, skipping all tests')
        return 'skip'

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

    backup_sys_path = sys.path
    sys.path.insert(0, script_path)
    import gdal_calc

    gdal.Translate('tmp/test_gdal_calc_py.tif', '../gcore/data/byte.tif', options='-a_nodata 74')

    gdal_calc.Calc('A', A='tmp/test_gdal_calc_py.tif', overwrite=True, quiet=True, outfile='tmp/test_gdal_calc_py_6.tif', NoDataValue=1)

    sys.path = backup_sys_path

    ds = gdal.Open('tmp/test_gdal_calc_py_6.tif')
    cs = ds.GetRasterBand(1).Checksum()
    if cs != 4673:
        gdaltest.post_reason('failure')
        print(cs)
        return 'fail'
    result = ds.GetRasterBand(1).ComputeRasterMinMax()
    if result != (90, 255):
        gdaltest.post_reason('failure')
        print(result)
        return 'fail'

    return 'success'
Beispiel #30
0
def test_pct2rgb_4():
    try:
        from osgeo import gdalnumeric
        gdalnumeric.BandRasterIONumPy
    except:
        return 'skip'

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

    test_py_scripts.run_py_script(script_path, 'pct2rgb', '-rgba ../gcore/data/rat.img tmp/test_pct2rgb_4.tif')

    ds = gdal.Open('tmp/test_pct2rgb_4.tif')
    ori_ds = gdal.Open('../gcore/data/rat.img')

    ori_data = struct.unpack('H', ori_ds.GetRasterBand(1).ReadRaster(1990, 1990, 1, 1, 1, 1))[0]
    data = (struct.unpack('B', ds.GetRasterBand(1).ReadRaster(1990, 1990, 1, 1, 1, 1))[0],
            struct.unpack('B', ds.GetRasterBand(2).ReadRaster(1990, 1990, 1, 1, 1, 1))[0],
            struct.unpack('B', ds.GetRasterBand(3).ReadRaster(1990, 1990, 1, 1, 1, 1))[0],
            struct.unpack('B', ds.GetRasterBand(4).ReadRaster(1990, 1990, 1, 1, 1, 1))[0],)

    ct = ori_ds.GetRasterBand(1).GetRasterColorTable()
    entry = ct.GetColorEntry(ori_data)

    if entry != data:
        return 'fail'

    ds = None
    ori_ds = None

    return 'success'
Beispiel #31
0
def test_ogrmerge_4():
    script_path = test_py_scripts.get_py_script('ogrmerge')
    if script_path is None:
        return 'skip'

    test_py_scripts.run_py_script(script_path, 'ogrmerge',
                                  '-f VRT -o /vsimem/out.vrt ../ogr/data/poly.shp')

    ds = ogr.Open('/vsimem/out.vrt')
    lyr = ds.GetLayer(0)
    if lyr.GetName() != 'poly':
        gdaltest.post_reason('fail')
        return 'fail'
    if lyr.GetFeatureCount() != 10:
        gdaltest.post_reason('fail')
        return 'fail'
    ds = None

    gdal.Unlink('/vsimem/out.vrt')

    return 'success'
Beispiel #32
0
def test_rgb2pct_2():

    script_path = test_py_scripts.get_py_script('rgb2pct')
    if script_path is None:
        pytest.skip()

    test_py_scripts.run_py_script(
        script_path, 'rgb2pct',
        '-n 16 ' + test_py_scripts.get_data_path('gcore') +
        'rgbsmall.tif tmp/test_rgb2pct_2.tif')

    ds = gdal.Open('tmp/test_rgb2pct_2.tif')
    assert ds.GetRasterBand(1).Checksum() == 16596

    ct = ds.GetRasterBand(1).GetRasterColorTable()
    for i in range(16, 255):
        entry = ct.GetColorEntry(i)
        assert (entry[0] == 0 and entry[1] == 0 and entry[2] == 0), \
            'Color table has more than 16 entries'

    ds = None
Beispiel #33
0
def test_gdal_calc_py_8():

    if gdalnumeric_not_available:
        pytest.skip()

    script_path = test_py_scripts.get_py_script('gdal_calc')
    if script_path is None:
        pytest.skip()

    test_py_scripts.run_py_script(
        script_path, 'gdal_calc', '-A tmp/test_gdal_calc_py.tif --A_band=1 -B tmp/test_gdal_calc_py.tif --B_band=2 -Z tmp/test_gdal_calc_py.tif --Z_band=2 --calc=A --calc=B --calc=Z --overwrite --outfile tmp/test_gdal_calc_py_8_1.tif')

    ds1 = gdal.Open('tmp/test_gdal_calc_py_8_1.tif')

    assert ds1 is not None, 'ds1 not found'

    assert ds1.GetRasterBand(1).Checksum() == 12603, 'ds1#1 wrong checksum'
    assert ds1.GetRasterBand(2).Checksum() == 58561, 'ds1#2 wrong checksum'
    assert ds1.GetRasterBand(3).Checksum() == 58561, 'ds1#3 wrong checksum'

    ds1 = None
def test_ogrmerge_11():
    script_path = test_py_scripts.get_py_script('ogrmerge')
    if script_path is None:
        pytest.skip()

    test_py_scripts.run_py_script(script_path, 'ogrmerge',
                                  '-f VRT -o tmp/out.vrt ../ogr/data/poly.shp '
                                  '-a_srs EPSG:32630')

    ds = ogr.Open('tmp/out.vrt')
    assert ds is not None
    ds = None

    f = gdal.VSIFOpenL('tmp/out.vrt', 'rb')
    content = ''
    if f is not None:
        content = gdal.VSIFReadL(1, 10000, f).decode('UTF-8')
        gdal.VSIFCloseL(f)
    gdal.Unlink('tmp/out.vrt')

    assert '<LayerSRS>EPSG:32630</LayerSRS>' in content
Beispiel #35
0
def test_gdal_calc_py_6():
    """ test nodata """
    if not numpy_available:
        pytest.skip("numpy is not available, skipping all tests", allow_module_level=True)

    script_path = test_py_scripts.get_py_script('gdal_calc')
    if script_path is None:
        pytest.skip("gdal_calc script not found, skipping all tests", allow_module_level=True)

    test_id, test_count = 6, 2
    out = make_temp_filename_list(test_id, test_count)

    gdal.Translate(out[0], test_py_scripts.get_data_path('gcore') + 'byte.tif', options='-a_nodata 74')
    gdal_calc.Calc('A', A=out[0], overwrite=True, quiet=True, outfile=out[1], NoDataValue=1)

    for i, checksum in zip(range(test_count), (4672, 4673)):
        ds = check_file(out[i], checksum, i+1)
        if i == 1:
            result = ds.GetRasterBand(1).ComputeRasterMinMax()
            assert result == (90, 255), 'Error! min/max not correct!'
        ds = None
Beispiel #36
0
def test_ogr2ogr_py_38():

    script_path = test_py_scripts.get_py_script('ogr2ogr')
    if script_path is None:
        pytest.skip()

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

    test_py_scripts.run_py_script(script_path, 'ogr2ogr', ' tmp/test_ogr2ogr_38.shp ../ogr/data/poly.shp -select AREA -where "EAS_ID = 170"')

    ds = ogr.Open('tmp/test_ogr2ogr_38.shp')
    lyr = ds.GetLayer(0)
    feat = lyr.GetNextFeature()
    assert feat is not None
    ds = None

    ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('tmp/test_ogr2ogr_38.shp')
Beispiel #37
0
def test_gdal2tiles_py_zoom_option():

    script_path = test_py_scripts.get_py_script('gdal2tiles')
    if script_path is None:
        pytest.skip()

    shutil.rmtree('tmp/out_gdal2tiles_smallworld', ignore_errors=True)

    # Because of multiprocessing, run as external process, to avoid issues with
    # Ubuntu 12.04 and socket.setdefaulttimeout()
    # as well as on Windows that doesn't manage to fork
    test_py_scripts.run_py_script_as_external_script(
        script_path, 'gdal2tiles',
        '-q --force-kml --processes=2 -z 0-1 ../gdrivers/data/small_world.tif tmp/out_gdal2tiles_smallworld'
    )

    _verify_raster_band_checksums('tmp/out_gdal2tiles_smallworld/1/0/0.png',
                                  expected_cs=[8130, 10496, 65274, 63715])

    ds = gdal.Open('tmp/out_gdal2tiles_smallworld/doc.kml')
    assert ds is not None, 'did not get kml'
Beispiel #38
0
def test_ogr2ogr_py_3():
    script_path = test_py_scripts.get_py_script('ogr2ogr')
    if script_path is None:
        pytest.skip()

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

    test_py_scripts.run_py_script(script_path, 'ogr2ogr', 'tmp/poly.shp ../ogr/data/poly.shp -spat 479609 4764629 479764 4764817')

    ds = ogr.Open('tmp/poly.shp')
    if ogrtest.have_geos():
        assert ds is not None and ds.GetLayer(0).GetFeatureCount() == 4
    else:
        assert ds is not None and ds.GetLayer(0).GetFeatureCount() == 5
    ds.Destroy()

    ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('tmp/poly.shp')
Beispiel #39
0
def test_gdal_edit_py_6():

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

    shutil.copy('../gcore/data/byte.tif', 'tmp/test_gdal_edit_py.tif')

    test_py_scripts.run_py_script(
        script_path, 'gdal_edit',
        "tmp/test_gdal_edit_py.tif -scale 2 -offset 3")

    ds = gdal.Open('tmp/test_gdal_edit_py.tif')
    if ds.GetRasterBand(1).GetScale() != 2:
        gdaltest.post_reason('fail')
        return 'fail'
    if ds.GetRasterBand(1).GetOffset() != 3:
        gdaltest.post_reason('fail')
        return 'fail'

    return 'success'
Beispiel #40
0
def test_ogrmerge_6():
    script_path = test_py_scripts.get_py_script('ogrmerge')
    if script_path is None:
        pytest.skip()

    test_py_scripts.run_py_script(
        script_path, 'ogrmerge', '-single -f VRT -o tmp/out.vrt ' +
        test_py_scripts.get_data_path('ogr') + 'poly.shp '
        '-src_layer_field_name source -src_layer_field_content '
        '"foo_{DS_NAME}_{DS_BASENAME}_{DS_INDEX}_{LAYER_NAME}_{LAYER_INDEX}"')

    ds = ogr.Open('tmp/out.vrt')
    lyr = ds.GetLayer(0)
    f = lyr.GetNextFeature()
    if f['source'] != 'foo_' + test_py_scripts.get_data_path(
            'ogr') + 'poly.shp_poly_0_poly_0':
        f.DumpReadable()
        pytest.fail()
    ds = None

    gdal.Unlink('tmp/out.vrt')
Beispiel #41
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'
def test_gdal_calc_py_2():

    if gdalnumeric_not_available:
        return 'skip'

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

    test_py_scripts.run_py_script(script_path, 'gdal_calc', '-A tmp/test_gdal_calc_py.tif --A_band 1 -B tmp/test_gdal_calc_py.tif --B_band 2 --calc=A+B --overwrite --outfile tmp/test_gdal_calc_py_2_1.tif')
    test_py_scripts.run_py_script(script_path, 'gdal_calc', '-A tmp/test_gdal_calc_py.tif --A_band 1 -B tmp/test_gdal_calc_py.tif --B_band 2 --calc=A*B --overwrite --outfile tmp/test_gdal_calc_py_2_2.tif')
    test_py_scripts.run_py_script(script_path, 'gdal_calc', '-A tmp/test_gdal_calc_py.tif --A_band 1 --calc="sqrt(A)" --type=Float32 --overwrite --outfile tmp/test_gdal_calc_py_2_3.tif')

    ds1 = gdal.Open('tmp/test_gdal_calc_py_2_1.tif')
    ds2 = gdal.Open('tmp/test_gdal_calc_py_2_2.tif')
    ds3 = gdal.Open('tmp/test_gdal_calc_py_2_3.tif')

    if ds1 is None:
        gdaltest.post_reason('ds1 not found')
        return 'fail'
    if ds2 is None:
        gdaltest.post_reason('ds2 not found')
        return 'fail'
    if ds3 is None:
        gdaltest.post_reason('ds3 not found')
        return 'fail'
    if ds1.GetRasterBand(1).Checksum() != 12368:
        gdaltest.post_reason('ds1 wrong checksum')
        return 'fail'
    if ds2.GetRasterBand(1).Checksum() != 62785:
        gdaltest.post_reason('ds2 wrong checksum')
        return 'fail'
    if ds3.GetRasterBand(1).Checksum() != 47132:
        gdaltest.post_reason('ds3 wrong checksum')
        return 'fail'
    ds1 = None
    ds2 = None
    ds3 = None

    return 'success'
Beispiel #43
0
def test_ogr2ogr_py_39():

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

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

    try:
        os.mkdir('tmp/test_ogr2ogr_39_src')
    except:
        pass
    shutil.copy('../ogr/data/poly.shp', 'tmp/test_ogr2ogr_39_src')
    shutil.copy('../ogr/data/poly.shx', 'tmp/test_ogr2ogr_39_src')
    shutil.copy('../ogr/data/poly.dbf', 'tmp/test_ogr2ogr_39_src')
    shutil.copy('../ogr/data/testpoly.shp', 'tmp/test_ogr2ogr_39_src')
    shutil.copy('../ogr/data/testpoly.shx', 'tmp/test_ogr2ogr_39_src')
    shutil.copy('../ogr/data/testpoly.dbf', 'tmp/test_ogr2ogr_39_src')

    test_py_scripts.run_py_script(
        script_path, 'ogr2ogr',
        ' tmp/test_ogr2ogr_39.shp tmp/test_ogr2ogr_39_src -sql "select * from poly"'
    )

    ds = ogr.Open('tmp/test_ogr2ogr_39.shp')
    if ds is None or ds.GetLayerCount() != 1:
        return 'fail'
    ds = None

    ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource(
        'tmp/test_ogr2ogr_39_src')
    ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource(
        'tmp/test_ogr2ogr_39.shp')

    return 'success'
Beispiel #44
0
def test_gdal_calc_py_1():

    if gdalnumeric_not_available:
        pytest.skip('gdalnumeric is not available, skipping all tests')

    script_path = test_py_scripts.get_py_script('gdal_calc')
    if script_path is None:
        pytest.skip()

    shutil.copy('../gcore/data/stefan_full_rgba.tif',
                'tmp/test_gdal_calc_py.tif')

    test_py_scripts.run_py_script(
        script_path, 'gdal_calc',
        '-A tmp/test_gdal_calc_py.tif --calc=A --overwrite --outfile tmp/test_gdal_calc_py_1_1.tif'
    )
    test_py_scripts.run_py_script(
        script_path, 'gdal_calc',
        '-A tmp/test_gdal_calc_py.tif --A_band=2 --calc=A --overwrite --outfile tmp/test_gdal_calc_py_1_2.tif'
    )
    test_py_scripts.run_py_script(
        script_path, 'gdal_calc',
        '-Z tmp/test_gdal_calc_py.tif --Z_band=2 --calc=Z --overwrite --outfile tmp/test_gdal_calc_py_1_3.tif'
    )

    ds1 = gdal.Open('tmp/test_gdal_calc_py_1_1.tif')
    ds2 = gdal.Open('tmp/test_gdal_calc_py_1_2.tif')
    ds3 = gdal.Open('tmp/test_gdal_calc_py_1_3.tif')

    assert ds1 is not None, 'ds1 not found'
    assert ds2 is not None, 'ds2 not found'
    assert ds3 is not None, 'ds3 not found'

    assert ds1.GetRasterBand(1).Checksum() == 12603, 'ds1 wrong checksum'
    assert ds2.GetRasterBand(1).Checksum() == 58561, 'ds2 wrong checksum'
    assert ds3.GetRasterBand(1).Checksum() == 58561, 'ds3 wrong checksum'

    ds1 = None
    ds2 = None
    ds3 = None
Beispiel #45
0
def test_gdal_edit_py_6():

    script_path = test_py_scripts.get_py_script('gdal_edit')
    if script_path is None:
        pytest.skip()

    shutil.copy('../gcore/data/byte.tif', 'tmp/test_gdal_edit_py.tif')

    # original values should be min=74, max=255, mean=126.765 StdDev=22.928470838676
    test_py_scripts.run_py_script(
        script_path, 'gdal_edit',
        "tmp/test_gdal_edit_py.tif -setstats None None None None")

    ds = gdal.Open('tmp/test_gdal_edit_py.tif')
    stat_min = ds.GetRasterBand(1).GetMetadataItem('STATISTICS_MINIMUM')
    assert stat_min is not None and float(stat_min) == 74
    stat_max = ds.GetRasterBand(1).GetMetadataItem('STATISTICS_MAXIMUM')
    assert stat_max is not None and float(stat_max) == 255
    stat_mean = ds.GetRasterBand(1).GetMetadataItem('STATISTICS_MEAN')
    assert not (stat_mean is None or abs(float(stat_mean) - 126.765) > 0.001)
    stat_stddev = ds.GetRasterBand(1).GetMetadataItem('STATISTICS_STDDEV')
    assert not (stat_stddev is None
                or abs(float(stat_stddev) - 22.928) > 0.001)

    ds = None

    test_py_scripts.run_py_script(
        script_path, 'gdal_edit',
        "tmp/test_gdal_edit_py.tif -setstats 22 217 100 30")

    ds = gdal.Open('tmp/test_gdal_edit_py.tif')
    stat_min = ds.GetRasterBand(1).GetMetadataItem('STATISTICS_MINIMUM')
    assert stat_min is not None and float(stat_min) == 22
    stat_max = ds.GetRasterBand(1).GetMetadataItem('STATISTICS_MAXIMUM')
    assert stat_max is not None and float(stat_max) == 217
    stat_mean = ds.GetRasterBand(1).GetMetadataItem('STATISTICS_MEAN')
    assert stat_mean is not None and float(stat_mean) == 100
    stat_stddev = ds.GetRasterBand(1).GetMetadataItem('STATISTICS_STDDEV')
    assert stat_stddev is not None and float(stat_stddev) == 30
    ds = None
Beispiel #46
0
def test_gdal_edit_py_4():

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

    shutil.copy('../gcore/data/byte.tif', 'tmp/test_gdal_edit_py.tif')
    ds = gdal.Open('tmp/test_gdal_edit_py.tif', gdal.GA_Update)
    band = ds.GetRasterBand(1)
    band.ComputeStatistics(False)
    band.SetMetadataItem('FOO', 'BAR')
    ds = band = None

    ds = gdal.Open('tmp/test_gdal_edit_py.tif')
    band = ds.GetRasterBand(1)
    if (band.GetMetadataItem('STATISTICS_MINIMUM') is None
            or band.GetMetadataItem('FOO') is None):
        gdaltest.post_reason('fail')
        return 'fail'
    ds = band = None

    test_py_scripts.run_py_script(script_path, 'gdal_edit',
                                  "tmp/test_gdal_edit_py.tif -unsetstats")

    ds = gdal.Open('tmp/test_gdal_edit_py.tif')
    band = ds.GetRasterBand(1)
    if (band.GetMetadataItem('STATISTICS_MINIMUM') is not None
            or band.GetMetadataItem('FOO') is None):
        gdaltest.post_reason('fail')
        return 'fail'
    ds = band = None

    try:
        os.stat('tmp/test_gdal_edit_py.tif.aux.xml')
        gdaltest.post_reason('fail')
        return 'fail'
    except:
        pass

    return 'success'
Beispiel #47
0
def test_gdal_calc_py_7():
    """ test --optfile """

    script_path = test_py_scripts.get_py_script('gdal_calc')
    if script_path is None:
        pytest.skip("gdal_calc script not found, skipping all tests",
                    allow_module_level=True)

    infile = get_input_file()
    test_id, test_count = 7, 4
    out = make_temp_filename_list(test_id, test_count)
    opt_files = make_temp_filename_list(test_id, test_count, is_opt=True)

    with open(opt_files[0], 'w') as f:
        f.write(f'-A {infile} --calc=A --overwrite --outfile {out[0]}')

    # Lines in optfiles beginning with '#' should be ignored
    with open(opt_files[1], 'w') as f:
        f.write(
            f'-A {infile} --A_band=2 --calc=A --overwrite --outfile {out[1]}')
        f.write('\n# -A_band=1')

    # options on separate lines should work, too
    opts = f'-Z {infile}', '--Z_band=2', '--calc=Z', '--overwrite', f'--outfile  {out[2]}'
    with open(opt_files[2], 'w') as f:
        for i in opts:
            f.write(i + '\n')

    # double-quoted options should be read as single arguments. Mixed numbers of arguments per line should work.
    opts = f'-Z {infile} --Z_band=2', '--calc "Z + 0"', f'--overwrite --outfile {out[3]}'
    with open(opt_files[3], 'w') as f:
        for i in opts:
            f.write(i + '\n')
    for opt_prefix in ['--optfile ', '@']:
        for i, checksum in zip(range(test_count),
                               (input_checksum[0], input_checksum[1],
                                input_checksum[1], input_checksum[1])):
            test_py_scripts.run_py_script(script_path, 'gdal_calc',
                                          f'{opt_prefix}{opt_files[i]}')
            check_file(out[i], checksum, i + 1)
Beispiel #48
0
def test_gdal_edit_py_1():

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

    shutil.copy('../gcore/data/byte.tif', 'tmp/test_gdal_edit_py.tif')

    test_py_scripts.run_py_script(script_path, 'gdal_edit', 'tmp/test_gdal_edit_py.tif -a_srs EPSG:4326 -a_ullr 2 50 3 49 -a_nodata 123 -mo FOO=BAR')

    ds = gdal.Open('tmp/test_gdal_edit_py.tif')
    wkt = ds.GetProjectionRef()
    gt = ds.GetGeoTransform()
    nd = ds.GetRasterBand(1).GetNoDataValue()
    md = ds.GetMetadata()
    ds = None

    if wkt.find('4326') == -1:
        gdaltest.post_reason('fail')
        print(wkt)
        return 'fail'

    expected_gt = (2.0, 0.050000000000000003, 0.0, 50.0, 0.0, -0.050000000000000003)
    for i in range(6):
        if abs(gt[i] - expected_gt[i]) > 1e-10:
            gdaltest.post_reason('fail')
            print(gt)
            return 'fail'

    if nd != 123:
        gdaltest.post_reason('fail')
        print(nd)
        return 'fail'

    if md['FOO'] != 'BAR':
        gdaltest.post_reason('fail')
        print(md)
        return 'fail'

    return 'success'
Beispiel #49
0
def test_gdal_calc_py_8():
    """ test multiple calcs """

    script_path = test_py_scripts.get_py_script('gdal_calc')
    if script_path is None:
        pytest.skip("gdal_calc script not found, skipping all tests",
                    allow_module_level=True)

    infile = get_input_file()
    test_id, test_count = 8, 1
    out = make_temp_filename_list(test_id, test_count)

    test_py_scripts.run_py_script(
        script_path, 'gdal_calc',
        '-A {} --A_band=1 -B {} --B_band=2 -Z {} --Z_band=2 --calc=A --calc=B --calc=Z --overwrite --outfile {}'
        .format(infile, infile, infile, out[0]))

    bnd_count = 3
    for i, checksum in zip(
            range(bnd_count),
        (input_checksum[0], input_checksum[1], input_checksum[1])):
        check_file(out[0], checksum, 1, bnd_idx=i + 1)
Beispiel #50
0
def test_ogr2ogr_py_32():

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

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

    test_py_scripts.run_py_script(
        script_path, 'ogr2ogr',
        ' tmp/test_ogr2ogr_32.shp ../ogr/data/poly.shp')
    test_py_scripts.run_py_script(
        script_path, 'ogr2ogr',
        ' -append tmp/test_ogr2ogr_32.shp ../ogr/data/poly.shp')

    ds = ogr.Open('tmp/test_ogr2ogr_32.shp')
    if ds is None or ds.GetLayer(0).GetFeatureCount() != 20:
        gdaltest.post_reason('-append failed')
        return 'fail'
    ds = None

    test_py_scripts.run_py_script(
        script_path, 'ogr2ogr',
        ' -overwrite tmp/test_ogr2ogr_32.shp ../ogr/data/poly.shp')

    ds = ogr.Open('tmp/test_ogr2ogr_32.shp')
    if ds is None or ds.GetLayer(0).GetFeatureCount() != 10:
        gdaltest.post_reason('-overwrite failed')
        return 'fail'
    ds = None

    ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource(
        'tmp/test_ogr2ogr_32.shp')
    return 'success'
Beispiel #51
0
def test_ogr2ogr_py_13():
    script_path = test_py_scripts.get_py_script('ogr2ogr')
    if script_path is None:
        return 'skip'

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

    test_py_scripts.run_py_script(script_path, 'ogr2ogr',
                                  'tmp/poly.shp ../ogr/data/poly.shp poly')

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

    ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('tmp/poly.shp')

    return 'success'
Beispiel #52
0
def test_ogr2ogr_py_14():
    script_path = test_py_scripts.get_py_script('ogr2ogr')
    if script_path is None:
        pytest.skip()

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

    test_py_scripts.run_py_script(
        script_path, 'ogr2ogr',
        '-segmentize 100 tmp/poly.shp ../ogr/data/poly.shp poly')

    ds = ogr.Open('tmp/poly.shp')
    assert ds is not None and ds.GetLayer(0).GetFeatureCount() == 10
    feat = ds.GetLayer(0).GetNextFeature()
    assert feat.GetGeometryRef().GetGeometryRef(0).GetPointCount() == 36
    ds.Destroy()

    ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('tmp/poly.shp')
Beispiel #53
0
def test_exclude_transparent_tiles():
    script_path = test_py_scripts.get_py_script('gdal2tiles')
    if script_path is None:
        return 'skip'

    output_folder = 'tmp/test_exclude_transparent_tiles'
    os.makedirs(output_folder)

    try:
        test_py_scripts.run_py_script_as_external_script(
            script_path,
            'gdal2tiles',
            '-x -z 15 data/test_gdal2tiles_exclude_transparent.tif %s' % output_folder)

        # First row totally transparent - no tiles
        tiles_folder = os.path.join(output_folder, '15', '21898')
        dir_files = os.listdir(tiles_folder)
        if dir_files:
            gdaltest.post_reason('Generated empty tiles for row 21898: %s' % dir_files)
            return 'fail'

        # Second row - only 2 non-transparent tiles
        tiles_folder = os.path.join(output_folder, '15', '21899')
        dir_files = sorted(os.listdir(tiles_folder))
        if ['22704.png', '22705.png'] != dir_files:
            gdaltest.post_reason('Generated empty tiles for row 21899: %s' % dir_files)
            return 'fail'

        # Third row - only 1 non-transparent tile
        tiles_folder = os.path.join(output_folder, '15', '21900')
        dir_files = os.listdir(tiles_folder)
        if ['22705.png'] != dir_files:
            gdaltest.post_reason('Generated empty tiles for row 21900: %s' % dir_files)
            return 'fail'

        return 'success'

    finally:
        shutil.rmtree(output_folder)
Beispiel #54
0
def test_ogrmerge_3():
    script_path = test_py_scripts.get_py_script('ogrmerge')
    if script_path is None:
        return 'skip'

    test_py_scripts.run_py_script(
        script_path, 'ogrmerge',
        '-overwrite_ds -o /vsimem/out.shp ../ogr/data/poly.shp')
    test_py_scripts.run_py_script(
        script_path, 'ogrmerge',
        '-overwrite_ds -single -o /vsimem/out.shp ../ogr/data/poly.shp')

    ds = ogr.Open('/vsimem/out.shp')
    lyr = ds.GetLayer(0)
    if lyr.GetFeatureCount() != 10:
        gdaltest.post_reason('fail')
        return 'fail'
    ds = None

    ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('/vsimem/out.shp')

    return 'success'
Beispiel #55
0
def test_gdal_calc_py_5():
    """ test python interface, basic copy """

    script_path = test_py_scripts.get_py_script('gdal_calc')
    if script_path is None:
        pytest.skip("gdal_calc script not found, skipping all tests", allow_module_level=True)

    infile = get_input_file()
    test_id, test_count = 5, 4
    out = make_temp_filename_list(test_id, test_count)

    gdal_calc.Calc('A', A=infile, overwrite=True, quiet=True, outfile=out[0])
    gdal_calc.Calc('A', A=infile, A_band=2, overwrite=True, quiet=True, outfile=out[1])
    gdal_calc.Calc('Z', Z=infile, Z_band=2, overwrite=True, quiet=True, outfile=out[2])
    gdal_calc.Calc(['A', 'Z'], A=infile, Z=infile, Z_band=2, overwrite=True, quiet=True, outfile=out[3])

    for i, checksum in zip(range(test_count), (input_checksum[0], input_checksum[1], input_checksum[1])):
        check_file(out[i], checksum, i+1)

    bnd_count = 2
    for i, checksum in zip(range(bnd_count), (input_checksum[0], input_checksum[1])):
        check_file(out[3], checksum, 4, bnd_idx=i+1)
Beispiel #56
0
def run_gdal_ls(argv):
    script_path = test_py_scripts.get_py_script('gdal_ls')
    if script_path is None:
        pytest.skip()

    saved_syspath = sys.path
    sys.path.append(script_path)
    try:
        import gdal_ls
    except ImportError:
        sys.path = saved_syspath
        pytest.fail()

    sys.path = saved_syspath

    with io.StringIO() as outstr:
        ret = gdal_ls.gdal_ls(argv, outstr)
        retstr = outstr.getvalue()

    assert ret == 0, 'got error code : %d' % ret

    return retstr
Beispiel #57
0
def test_gdal_polygonize_4():

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

    # Test mask syntax
    test_py_scripts.run_py_script(
        script_path, 'gdal_polygonize',
        '-q -f GML -b mask ../gcore/data/byte.tif tmp/out.gml')

    content = open('tmp/out.gml', 'rt').read()

    os.unlink('tmp/out.gml')

    if content.find(
            '<ogr:geometryProperty><gml:Polygon srsName="EPSG:26711"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>440720,3751320 440720,3750120 441920,3750120 441920,3751320 440720,3751320</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>'
    ) < 0:
        gdaltest.post_reason('fail')
        print(content)
        return 'fail'

    # Test mask,1 syntax
    test_py_scripts.run_py_script(
        script_path, 'gdal_polygonize',
        '-q -f GML -b mask,1 ../gcore/data/byte.tif tmp/out.gml')

    content = open('tmp/out.gml', 'rt').read()

    os.unlink('tmp/out.gml')

    if content.find(
            '<ogr:geometryProperty><gml:Polygon srsName="EPSG:26711"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>440720,3751320 440720,3750120 441920,3750120 441920,3751320 440720,3751320</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>'
    ) < 0:
        gdaltest.post_reason('fail')
        print(content)
        return 'fail'

    return 'success'
Beispiel #58
0
def test_ogrmerge_6():
    script_path = test_py_scripts.get_py_script('ogrmerge')
    if script_path is None:
        return 'skip'

    test_py_scripts.run_py_script(script_path, 'ogrmerge',
        '-single -f VRT -o /vsimem/out.vrt ../ogr/data/poly.shp '
        '-src_layer_field_name source -src_layer_field_content '
        '"foo_{DS_NAME}_{DS_BASENAME}_{DS_INDEX}_{LAYER_NAME}_{LAYER_INDEX}"')

    ds = ogr.Open('/vsimem/out.vrt')
    lyr = ds.GetLayer(0)
    f = lyr.GetNextFeature()
    if f['source'] != 'foo_../ogr/data/poly.shp_poly_0_poly_0':
        gdaltest.post_reason('fail')
        f.DumpReadable()
        return 'fail'
    ds = None

    gdal.Unlink('/vsimem/out.vrt')

    return 'success'
def test_gdal_polygonize_2():

    script_path = test_py_scripts.get_py_script('gdal_polygonize')
    if script_path is None:
        pytest.skip()

    shp_drv = ogr.GetDriverByName('ESRI Shapefile')
    try:
        os.stat('tmp/out.shp')
        shp_drv.DeleteDataSource('tmp/out.shp')
    except OSError:
        pass

    # run the algorithm.
    test_py_scripts.run_py_script(
        script_path, 'gdal_polygonize',
        '-b 1 -f "ESRI Shapefile" -q -nomask ../alg/data/polygonize_in.grd tmp'
    )

    # Confirm we get the set of expected features in the output layer.
    shp_ds = ogr.Open('tmp')
    shp_lyr = shp_ds.GetLayerByName('out')

    expected_feature_number = 17
    assert shp_lyr.GetFeatureCount() == expected_feature_number

    expect = [
        107, 123, 115, 132, 115, 132, 140, 132, 148, 123, 140, 132, 156, 100,
        101, 102, 103
    ]

    tr = ogrtest.check_features_against_list(shp_lyr, 'DN', expect)

    shp_ds.Destroy()
    # Reload drv because of side effects of run_py_script()
    shp_drv = ogr.GetDriverByName('ESRI Shapefile')
    shp_drv.DeleteDataSource('tmp/out.shp')

    assert tr
Beispiel #60
0
def test_gdal_pansharpen_2():

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

    test_py_scripts.run_py_script(
        script_path, 'gdal_pansharpen',
        ' -q -b 3 -b 1 -bitdepth 8 -threads ALL_CPUS -spat_adjust union -w 0.33333333333333333 -w 0.33333333333333333 -w 0.33333333333333333 -of VRT -r cubic tmp/small_world_pan.tif ../gdrivers/data/small_world.tif,band=1 ../gdrivers/data/small_world.tif,band=2 ../gdrivers/data/small_world.tif,band=3 tmp/out.vrt'
    )

    ds = gdal.Open('tmp/out.vrt')
    cs = [ds.GetRasterBand(i + 1).Checksum() for i in range(ds.RasterCount)]
    ds = None
    gdal.GetDriverByName('VRT').Delete('tmp/out.vrt')

    if cs != [9742, 4735]:
        gdaltest.post_reason('fail')
        print(cs)
        return 'fail'

    return 'success'