Example #1
0
def test_gdal_calc_py_6():
    """ test nodata """

    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
Example #2
0
def test_ogr2ogr_py_25():
    script_path = test_py_scripts.get_py_script('ogr2ogr')
    if script_path is None:
        pytest.skip()

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

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

    f = open('tmp/clip.csv', 'wt')
    f.write('foo,WKT\n')
    f.write('foo,"POLYGON((479609 4764629,479609 4764817,479764 4764817,479764 4764629,479609 4764629))"\n')
    f.close()

    test_py_scripts.run_py_script(script_path, 'ogr2ogr', 'tmp/poly.shp '+test_py_scripts.get_data_path('ogr')+'poly.shp -clipsrc tmp/clip.csv -clipsrcwhere foo=\'foo\'')

    ds = ogr.Open('tmp/poly.shp')
    assert ds is not None and ds.GetLayer(0).GetFeatureCount() == 4

    assert ds.GetLayer(0).GetExtent() == (479609, 479764, 4764629, 4764817), \
        'unexpected extent'

    ds.Destroy()

    ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('tmp/poly.shp')
    os.remove('tmp/clip.csv')
Example #3
0
def test_ogr2ogr_py_26():
    script_path = test_py_scripts.get_py_script('ogr2ogr')
    if script_path is None:
        pytest.skip()

    if not ogrtest.have_geos():
        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', 'tmp/poly.shp '+test_py_scripts.get_data_path('ogr')+'poly.shp -clipdst "POLYGON((479609 4764629,479609 4764817,479764 4764817,479764 4764629,479609 4764629))"')

    ds = ogr.Open('tmp/poly.shp')
    assert ds is not None and ds.GetLayer(0).GetFeatureCount() == 4

    assert ds.GetLayer(0).GetExtent() == (479609, 479764, 4764629, 4764817), \
        'unexpected extent'

    ds.Destroy()

    ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('tmp/poly.shp')
Example #4
0
def test_ogr2ogr_py_21():
    script_path = test_py_scripts.get_py_script('ogr2ogr')
    if script_path is None:
        pytest.skip()

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

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

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

    ds.Destroy()
    os.remove('tmp/testogr2ogr21.gtm')
Example #5
0
def test_ogr2ogr_py_23():
    script_path = test_py_scripts.get_py_script('ogr2ogr')
    if script_path is None:
        pytest.skip()
    if test_cli_utilities.get_ogr2ogr_path() is None:
        pytest.skip()

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

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

    ds.Destroy()
    ogr.GetDriverByName('MapInfo File').DeleteDataSource('tmp/testogr2ogr23.mif')
Example #6
0
def test_ogr2ogr_py_5():
    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, AttributeError):
        pass

    test_py_scripts.run_py_script(script_path, 'ogr2ogr', 'tmp/poly.shp '+test_py_scripts.get_data_path('ogr')+'poly.shp')
    test_py_scripts.run_py_script(script_path, 'ogr2ogr', '-update -append tmp/poly.shp '+test_py_scripts.get_data_path('ogr')+'poly.shp')

    ds = ogr.Open('tmp/poly.shp')
    assert ds is not None and ds.GetLayer(0).GetFeatureCount() == 20

    feat10 = ds.GetLayer(0).GetFeature(10)
    assert feat10.GetFieldAsDouble('AREA') == 215229.266, \
        'Did not get expected value for field AREA'
    assert feat10.GetFieldAsString('PRFEDEA') == '35043411', \
        'Did not get expected value for field PRFEDEA'

    ds.Destroy()

    ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('tmp/poly.shp')
Example #7
0
def get_input_file():
    infile = make_temp_filename(0)
    if not os.path.isfile(infile):
        shutil.copy(
            test_py_scripts.get_data_path('gcore') + 'stefan_full_rgba.tif',
            infile)
    return infile
Example #8
0
def test_ogrmerge_1():
    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 -o tmp/out.shp ' + test_py_scripts.get_data_path('ogr') +
        'poly.shp ' + test_py_scripts.get_data_path('ogr') + 'poly.shp')

    ds = ogr.Open('tmp/out.shp')
    lyr = ds.GetLayer(0)
    assert lyr.GetFeatureCount() == 20
    ds = None

    ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('tmp/out.shp')
Example #9
0
def test_gdalinfo_py_1():

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

    ret = test_py_scripts.run_py_script(script_path, 'gdalinfo', test_py_scripts.get_data_path('gcore') + 'byte.tif')
    assert ret.find('Driver: GTiff/GeoTIFF') != -1
Example #10
0
def test_ogrinfo_py_4():
    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',
        test_py_scripts.get_data_path('ogr') + 'poly.shp poly')
    assert ret.find('Feature Count: 10') != -1
Example #11
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 ' + test_py_scripts.get_data_path('ogr') + 'poly.shp')
    assert ret.find('ESRI Shapefile') != -1
Example #12
0
def test_gdalinfo_py_2():
    script_path = test_py_scripts.get_py_script('gdalinfo')
    if script_path is None:
        pytest.skip()

    ret = test_py_scripts.run_py_script(
        script_path, 'gdalinfo',
        '-checksum ' + test_py_scripts.get_data_path('gcore') + 'byte.tif')
    assert ret.find('Checksum=4672') != -1
Example #13
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',
        test_py_scripts.get_data_path('ogr') + 'poly.shp poly -fid 9')
    assert ret.find('OGRFeature(poly):9') != -1
Example #14
0
def test_gdal_polygonize_1():

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

    # Create a OGR datasource to put results in.
    shp_drv = ogr.GetDriverByName('ESRI Shapefile')
    try:
        os.stat('tmp/poly.shp')
        shp_drv.DeleteDataSource('tmp/poly.shp')
    except OSError:
        pass

    shp_ds = shp_drv.CreateDataSource('tmp/poly.shp')

    shp_layer = shp_ds.CreateLayer('poly', None, ogr.wkbPolygon)

    fd = ogr.FieldDefn('DN', ogr.OFTInteger)
    shp_layer.CreateField(fd)

    shp_ds.Destroy()

    # run the algorithm.
    test_py_scripts.run_py_script(
        script_path, 'gdal_polygonize',
        test_py_scripts.get_data_path('alg') + 'polygonize_in.grd tmp poly DN')

    # Confirm we get the set of expected features in the output layer.

    shp_ds = ogr.Open('tmp')
    shp_lyr = shp_ds.GetLayerByName('poly')

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

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

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

    # check at least one geometry.
    if tr:
        shp_lyr.SetAttributeFilter('dn = 156')
        feat_read = shp_lyr.GetNextFeature()
        if ogrtest.check_feature_geometry(
                feat_read,
                'POLYGON ((440720 3751200,440900 3751200,440900 3751020,440720 3751020,440720 3751200),(440780 3751140,440780 3751080,440840 3751080,440840 3751140,440780 3751140))'
        ) != 0:
            tr = 0
        feat_read.Destroy()

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

    assert tr
Example #15
0
def test_gdalinfo_py_6():
    script_path = test_py_scripts.get_py_script('gdalinfo')
    if script_path is None:
        pytest.skip()

    ret = test_py_scripts.run_py_script(
        script_path, 'gdalinfo',
        test_py_scripts.get_data_path('gdrivers') + 'hfa/int.img')
    assert ret.find('Overviews') != -1
Example #16
0
def test_pct2rgb_4():
    try:
        from osgeo import gdal_array
        gdal_array.BandRasterIONumPy
    except (ImportError, AttributeError):
        pytest.skip()

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

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

    ds = gdal.Open('tmp/test_pct2rgb_4.tif')
    ori_ds = gdal.Open(test_py_scripts.get_data_path('gcore') + '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)

    assert entry == data

    ds = None
    ori_ds = None
Example #17
0
def test_ogrinfo_py_11():
    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',
        test_py_scripts.get_data_path('ogr') + 'poly.shp poly -fields=no')
    assert ret.find('AREA (Real') == -1
    assert ret.find('POLYGON (') != -1
Example #18
0
def test_gdalinfo_py_7():
    script_path = test_py_scripts.get_py_script('gdalinfo')
    if script_path is None:
        pytest.skip()

    ret = test_py_scripts.run_py_script(
        script_path, 'gdalinfo',
        test_py_scripts.get_data_path('gcore') + 'gcps.vrt')
    assert ret.find('GCP Projection =') != -1
    assert ret.find('PROJCS["NAD27 / UTM zone 11N"') != -1
    assert ret.find('(100,100) -> (446720,3745320,0)') != -1

    # Same but with -nogcps
    ret = test_py_scripts.run_py_script(
        script_path, 'gdalinfo',
        '-nogcp ' + test_py_scripts.get_data_path('gcore') + 'gcps.vrt')
    assert ret.find('GCP Projection =') == -1
    assert ret.find('PROJCS["NAD27 / UTM zone 11N"') == -1
    assert ret.find('(100,100) -> (446720,3745320,0)') == -1
Example #19
0
def test_gdal_edit_py_5():

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

    gdal_array = pytest.importorskip('osgeo.gdal_array')
    try:
        gdal_array.BandRasterIONumPy
    except AttributeError:
        pytest.skip('osgeo.gdal_array.BandRasterIONumPy is unavailable')

    filename = 'tmp/test_gdal_edit_py.tif'
    shutil.copy(test_py_scripts.get_data_path('gcore') + 'byte.tif', filename)
    try:
        ds = gdal.Open(filename, gdal.GA_Update)
        band = ds.GetRasterBand(1)
        array = band.ReadAsArray()
        # original minimum is 74; modify a pixel value from 99 to 22
        array[15, 12] = 22
        band.WriteArray(array)
        ds = band = None

        ds = gdal.Open(filename)
        assert ds.ReadAsArray()[15, 12] == 22
        ds = None

        test_py_scripts.run_py_script(script_path, 'gdal_edit',
                                      filename + " -stats")

        ds = gdal.Open(filename)
        stat_min = ds.GetRasterBand(1).GetMetadataItem('STATISTICS_MINIMUM')
        assert stat_min is not None and float(stat_min) == 22
        ds = None

        ds = gdal.Open(filename, gdal.GA_Update)
        band = ds.GetRasterBand(1)
        array = band.ReadAsArray()
        array[15, 12] = 26
        band.WriteArray(array)
        ds = band = None

        ds = gdal.Open(filename)
        assert ds.ReadAsArray()[15, 12] == 26
        ds = None

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

        ds = gdal.Open(filename)
        stat_min = ds.GetRasterBand(1).GetMetadataItem('STATISTICS_MINIMUM')
        assert stat_min is not None and float(stat_min) == 26
        ds = None
    finally:
        gdal.GetDriverByName('GTiff').Delete(filename)
Example #20
0
def test_ogrinfo_py_7():
    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',
        test_py_scripts.get_data_path('ogr') + 'poly.shp poly -geom=summary')
    assert ret.find('Feature Count: 10') != -1
    assert ret.find('POLYGON (') == -1
    assert ret.find('POLYGON :') != -1
Example #21
0
def test_gdal_fillnodata_1():

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

    test_py_scripts.run_py_script(script_path, 'gdal_fillnodata', test_py_scripts.get_data_path('gcore') + 'byte.tif tmp/test_gdal_fillnodata_1.tif')

    ds = gdal.Open('tmp/test_gdal_fillnodata_1.tif')
    assert ds.GetRasterBand(1).Checksum() == 4672
    ds = None
Example #22
0
def test_gdal_pansharpen_2():

    script_path = test_py_scripts.get_py_script('gdal_pansharpen')
    if script_path is None:
        pytest.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 '
        + test_py_scripts.get_data_path('gdrivers') +
        'small_world.tif,band=1 ' + test_py_scripts.get_data_path('gdrivers') +
        'small_world.tif,band=2 ' + test_py_scripts.get_data_path('gdrivers') +
        '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')

    assert cs == [9742, 4735]
Example #23
0
def test_gdal_edit_py_5():

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

    try:
        from osgeo import gdal_array
        gdal_array.BandRasterIONumPy
    except:
        pytest.skip()

    shutil.copy(
        test_py_scripts.get_data_path('gcore') + 'byte.tif',
        'tmp/test_gdal_edit_py.tif')
    ds = gdal.Open('tmp/test_gdal_edit_py.tif', gdal.GA_Update)
    band = ds.GetRasterBand(1)
    array = band.ReadAsArray()
    # original minimum is 74; modify a pixel value from 99 to 22
    array[15, 12] = 22
    band.WriteArray(array)
    ds = band = None

    ds = gdal.Open('tmp/test_gdal_edit_py.tif')
    assert ds.ReadAsArray()[15, 12] == 22
    ds = None

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

    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
    ds = None

    ds = gdal.Open('tmp/test_gdal_edit_py.tif', gdal.GA_Update)
    band = ds.GetRasterBand(1)
    array = band.ReadAsArray()
    array[15, 12] = 26
    band.WriteArray(array)
    ds = band = None

    ds = gdal.Open('tmp/test_gdal_edit_py.tif')
    assert ds.ReadAsArray()[15, 12] == 26
    ds = None

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

    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) == 26
    ds = None
Example #24
0
def test_gdal_fillnodata_2():

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

    test_py_scripts.run_py_script(script_path, 'gdal_fillnodata', test_py_scripts.get_data_path('gcore') + 'nodata_byte.tif tmp/test_gdal_fillnodata_2.tif')

    ds = gdal.Open('tmp/test_gdal_fillnodata_2.tif')
    assert ds.GetRasterBand(1).GetNoDataValue() == 0, \
        'Failed to copy No Data Value to dst dataset.'
    ds = None
Example #25
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')
Example #26
0
def test_ogr2ogr_py_39():

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

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

    try:
        os.mkdir('tmp/test_ogr2ogr_39_src')
    except OSError:
        pass
    shutil.copy(test_py_scripts.get_data_path('ogr') + 'poly.shp', 'tmp/test_ogr2ogr_39_src')
    shutil.copy(test_py_scripts.get_data_path('ogr') + 'poly.shx', 'tmp/test_ogr2ogr_39_src')
    shutil.copy(test_py_scripts.get_data_path('ogr') + 'poly.dbf', 'tmp/test_ogr2ogr_39_src')
    shutil.copy(test_py_scripts.get_data_path('ogr') + 'shp/testpoly.shp', 'tmp/test_ogr2ogr_39_src')
    shutil.copy(test_py_scripts.get_data_path('ogr') + 'shp/testpoly.shx', 'tmp/test_ogr2ogr_39_src')
    shutil.copy(test_py_scripts.get_data_path('ogr') + 'shp/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')
    assert ds is not None and ds.GetLayerCount() == 1
    ds = None

    ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('tmp/test_ogr2ogr_39_src')
    ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('tmp/test_ogr2ogr_39.shp')
Example #27
0
def test_gdal_edit_py_unsetrpc():

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

    gdal.Translate('tmp/test_gdal_edit_py.tif', test_py_scripts.get_data_path('gcore') + 'byte_rpc.tif')

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

    ds = gdal.Open('tmp/test_gdal_edit_py.tif')
    assert not ds.GetMetadata('RPC')
Example #28
0
def test_gdalinfo_py_5():
    script_path = test_py_scripts.get_py_script('gdalinfo')
    if script_path is None:
        pytest.skip()

    try:
        os.remove(test_py_scripts.get_data_path('gcore') + 'byte.tif.aux.xml')
    except OSError:
        pass

    ret = test_py_scripts.run_py_script(
        script_path, 'gdalinfo',
        test_py_scripts.get_data_path('gcore') + 'byte.tif')
    assert ret.find('STATISTICS_MINIMUM=74') == -1, 'got wrong minimum.'

    ret = test_py_scripts.run_py_script(
        script_path, 'gdalinfo',
        '-stats ' + test_py_scripts.get_data_path('gcore') + 'byte.tif')
    assert ret.find('STATISTICS_MINIMUM=74') != -1, 'got wrong minimum (2).'

    # We will blow an exception if the file does not exist now!
    os.remove(test_py_scripts.get_data_path('gcore') + 'byte.tif.aux.xml')
Example #29
0
def test_gdalattachpct_1():
    pct_filename = 'tmp/test_rgb2pct_2.tif'
    src_filename = test_py_scripts.get_data_path('gcore') + 'rgbsmall.tif'
    pct_filename4 = 'tmp/test_gdalattachpct_1_4.txt'

    # pct from raster
    ct0 = color_table.get_color_table(pct_filename)
    ds1, err = rgb2pct.doit(src_filename=src_filename,
                            pct_filename=pct_filename)
    ct1 = color_table.get_color_table(ds1)
    assert err == 0 and ds1 is not None and ct1 is not None and color_table.are_equal_color_table(
        ct0, ct1)
    ds1 = None

    # generate some junk color table
    color2 = (1, 2, 3, 4)
    ct2a = color_table.get_fixed_color_table(color2)
    assert color_table.is_fixed_color_table(ct2a, color2)

    # assign junk color table
    ds2, err = gdalattachpct.doit(src_filename=src_filename, pct_filename=ct2a)
    ct2b = color_table.get_color_table(ds2)
    assert err == 0 and ds2 is not None and color_table.are_equal_color_table(
        ct2a, ct2b)
    ds2 = None

    # pct from gdal.ColorTable object
    ds3, err = gdalattachpct.doit(src_filename=src_filename, pct_filename=ct1)
    ct3 = color_table.get_color_table(ds3)
    assert err == 0 and ds3 is not None and color_table.are_equal_color_table(
        ct1, ct3)
    ds3 = None

    # write color table to a txt file
    color_table.write_color_table_to_file(ct3, pct_filename4)
    ct4 = color_table.get_color_table(pct_filename4)
    assert color_table.are_equal_color_table(ct3, ct4)

    # pct from txt file
    ds5, err = rgb2pct.doit(src_filename=src_filename, pct_filename=ct4)
    ct5 = color_table.get_color_table(ds5)
    assert err == 0 and ds5 is not None and ct5 is not None and color_table.are_equal_color_table(
        ct4, ct5)
    ds5 = None

    ct1 = None
    ct2b = None
    ct2a = None
    ct3 = None
    ct4 = None
    ct5 = None
Example #30
0
def test_gdal_edit_py_7():

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

    shutil.copy(test_py_scripts.get_data_path('gcore') + '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')
    assert ds.GetRasterBand(1).GetScale() == 2
    assert ds.GetRasterBand(1).GetOffset() == 3
    ds = None

    shutil.copy(test_py_scripts.get_data_path('gcore') + '1bit_2bands.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 4 -offset 10 20")

    ds = gdal.Open('tmp/test_gdal_edit_py.tif')
    for i in [1, 2]:
        assert ds.GetRasterBand(i).GetScale() == i*2
        assert ds.GetRasterBand(i).GetOffset() == i*10

    ds = None