Esempio n. 1
0
def test_gdal_sieve_1():

    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'
    return 'success'
Esempio n. 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'
Esempio n. 3
0
def test_ogr2ogr_py_22():
    script_path = test_py_scripts.get_py_script("ogr2ogr")
    if script_path is None:
        return "skip"

    test_py_scripts.run_py_script(
        script_path,
        "ogr2ogr",
        '-f "MapInfo File" tmp/testogr2ogr22.mif ../utilities/data/dataforogr2ogr21.csv '
        + '-sql "SELECT comment, name FROM dataforogr2ogr21" -nlt POINT',
    )
    ds = ogr.Open("tmp/testogr2ogr22.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/testogr2ogr22.mif")
        return "fail"

    ds.Destroy()
    ogr.GetDriverByName("MapInfo File").DeleteDataSource("tmp/testogr2ogr22.mif")

    return "success"
Esempio n. 4
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'
Esempio n. 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"
Esempio n. 6
0
def test_gdal_edit_py_2():

    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 -unsetgt")

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

    if gt is not None:
        gdaltest.post_reason('fail')
        print(gt)
        return 'fail'

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

    return 'success'
Esempio n. 7
0
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'
Esempio n. 8
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'
Esempio n. 9
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 )
    ds.GetRasterBand(1).ComputeStatistics(False)
    ds = None

    ds = gdal.Open('tmp/test_gdal_edit_py.tif')
    if ds.GetRasterBand(1).GetMetadataItem('STATISTICS_MINIMUM') is None:
        gdaltest.post_reason('fail')
        return 'fail'
    ds = 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')
    if ds.GetRasterBand(1).GetMetadataItem('STATISTICS_MINIMUM') is not None:
        gdaltest.post_reason('fail')
        return 'fail'
    ds = None
    
    try:
        os.stat('tmp/test_gdal_edit_py.tif.aux.xml')
        gdaltest.post_reason('fail')
        return 'fail'
    except:
        pass

    return 'success'
Esempio n. 10
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'
Esempio n. 11
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'
Esempio n. 12
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'
Esempio n. 13
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'
Esempio n. 14
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'
Esempio n. 15
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'
Esempio n. 16
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'
Esempio n. 17
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'
Esempio n. 18
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'
Esempio n. 19
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'
Esempio n. 20
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'
Esempio n. 21
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'
Esempio n. 22
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'
Esempio n. 23
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'
Esempio n. 24
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'
Esempio n. 25
0
def test_ogr2ogr_py_22():
    script_path = test_py_scripts.get_py_script('ogr2ogr')
    if script_path is None:
        return 'skip'
        
    test_py_scripts.run_py_script(script_path, 'ogr2ogr', \
        '-f "MapInfo File" tmp/testogr2ogr22.mif ../utilities/data/dataforogr2ogr21.csv ' +
        '-sql "SELECT comment, name FROM dataforogr2ogr21" -nlt POINT')
    ds = ogr.Open('tmp/testogr2ogr22.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/testogr2ogr22.mif')
        return 'fail'

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

    return 'success'
Esempio n. 26
0
def test_gdalmove_1():

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

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

    test_py_scripts.run_py_script(script_path, 'gdalmove', '-s_srs "+proj=utm +zone=11 +ellps=clrk66 +towgs84=0,0,0 +no_defs" -t_srs EPSG:32611 tmp/test_gdalmove_1.tif -et 1' )

    ds = gdal.Open('tmp/test_gdalmove_1.tif')
    got_gt = ds.GetGeoTransform()
    expected_gt = (440719.95870935748, 60.000041745067577, 1.9291142234578728e-05, 3751294.2109841029, 1.9099167548120022e-05, -60.000041705276814)
    for i in range(6):
        if abs(got_gt[i] - expected_gt[i]) / abs(got_gt[i]) > 1e-5:
            gdaltest.post_reason('bad gt')
            print(got_gt)
            print(expected_gt)
            return 'fail'
    wkt = ds.GetProjection()
    if wkt.find('32611') < 0:
        gdaltest.post_reason('bad geotransform')
        print(wkt)
        return 'fail'
    ds = None

    return 'success'
Esempio n. 27
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' 
Esempio n. 28
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"
Esempio n. 29
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"
Esempio n. 30
0
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'
Esempio n. 31
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 ../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)

    assert entry == data

    ds = None
    ori_ds = None
Esempio n. 32
0
def test_gdallocationinfo_py_3():
    """ Test -valonly """
    script_path = test_py_scripts.get_py_script('gdallocationinfo')
    if script_path is None:
        pytest.skip()
    ret = test_py_scripts.run_py_script(
        script_path, 'gdallocationinfo',
        ' -b 1 -valonly ../gcore/data/byte.tif 0 0')
    expected_ret = """107"""
    assert ret.startswith(expected_ret)
Esempio n. 33
0
def test_ogr2ogr_py_31():

    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', ' -overwrite 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() == 10
    ds.Destroy()

    ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('tmp/poly.shp')
Esempio n. 34
0
def test_ogrinfo_py_9():
    script_path = test_py_scripts.get_py_script('ogrinfo')
    if script_path is None:
        return 'skip'

    ret = test_py_scripts.run_py_script(script_path, 'ogrinfo', '../ogr/data/poly.shp poly -where "EAS_ID=171"')
    if ret.find('Feature Count: 1') == -1:
        return 'fail'

    return 'success'
Esempio n. 35
0
def test_ogrinfo_py_2():
    script_path = test_py_scripts.get_py_script('ogrinfo')
    if script_path is None:
        return 'skip'

    ret = test_py_scripts.run_py_script(script_path, 'ogrinfo', '-ro ../ogr/data/poly.shp')
    if ret.find('ESRI Shapefile') == -1:
        return 'fail'

    return 'success'
Esempio n. 36
0
def test_ogrinfo_py_10():
    script_path = test_py_scripts.get_py_script('ogrinfo')
    if script_path is None:
        return 'skip'

    ret = test_py_scripts.run_py_script(script_path, 'ogrinfo', '../ogr/data/poly.shp poly -fid 9')
    if ret.find('OGRFeature(poly):9') == -1:
        return 'fail'

    return 'success'
def test_gdal_polygonize_2():

    script_path = test_py_scripts.get_py_script('gdal_polygonize')
    if script_path is None:
        return '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
    if shp_lyr.GetFeatureCount() != expected_feature_number:
        gdaltest.post_reason(
            'GetFeatureCount() returned %d instead of %d' %
            (shp_lyr.GetFeatureCount(), expected_feature_number))
        return 'fail'

    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')

    return 'success' if tr else 'fail'
Esempio n. 38
0
def test_ogrinfo_py_5():
    script_path = test_py_scripts.get_py_script('ogrinfo')
    if script_path is None:
        return 'skip'

    ret = test_py_scripts.run_py_script(script_path, 'ogrinfo', '../ogr/data/poly.shp -sql "select * from poly"')
    if ret.find('Feature Count: 10') == -1:
        return 'fail'

    return 'success'
def test_gdal_edit_py_3():

    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')

    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

    assert gt != (0.0, 1.0, 0.0, 0.0, 0.0, 1.0)

    assert wkt == ''
Esempio n. 40
0
def test_gdal_edit_py_8():

    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',
                   '../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')
    assert ds.GetRasterBand(4).GetColorInterpretation() == gdal.GCI_AlphaBand

    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')
    assert ds.GetRasterBand(4).GetColorInterpretation() == gdal.GCI_Undefined
Esempio n. 41
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)
Esempio n. 42
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'
Esempio n. 43
0
def test_ogrmerge_5():
    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 ../ogr/data/testpoly.shp -nln '
                                  '"foo_{DS_NAME}_{DS_BASENAME}_{DS_INDEX}_{LAYER_NAME}_{LAYER_INDEX}"')

    ds = ogr.Open('tmp/out.vrt')
    lyr = ds.GetLayer(0)
    assert lyr.GetName() == 'foo_../ogr/data/poly.shp_poly_0_poly_0'
    assert lyr.GetFeatureCount() == 10
    lyr = ds.GetLayer(1)
    assert lyr.GetName() == 'foo_../ogr/data/testpoly.shp_testpoly_1_testpoly_0'
    assert lyr.GetFeatureCount() == 14
    ds = None

    gdal.Unlink('tmp/out.vrt')
def test_gdal_edit_py_2():

    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')

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

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

    assert gt is None

    assert wkt != ''
Esempio n. 45
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
Esempio n. 46
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 ../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('tmp/out.vrt')
    lyr = ds.GetLayer(0)
    f = lyr.GetNextFeature()
    if f['source'] != 'foo_../ogr/data/poly.shp_poly_0_poly_0':
        f.DumpReadable()
        pytest.fail()
    ds = None

    gdal.Unlink('tmp/out.vrt')
Esempio n. 47
0
def test_ogrinfo_py_6():
    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=no')
    assert ret.find('Feature Count: 10') != -1
    assert ret.find('POLYGON') == -1
Esempio n. 48
0
def test_ogrmerge_12():
    script_path = test_py_scripts.get_py_script('ogrmerge')
    if script_path is None:
        pytest.skip()

    with open('tmp/tmp.json', 'wb') as f:
        f.write(
            b"""{ "type": "FeatureCollection", "name": "\xc3\xa9ven", "features": [ { "type": "Feature", "properties": {}, "geometry": null} ]}"""
        )

    test_py_scripts.run_py_script(script_path, 'ogrmerge',
                                  '-f VRT -o tmp/out.vrt tmp/tmp.json')

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

    gdal.Unlink('tmp/tmp.json')
    gdal.Unlink('tmp/out.vrt')
Esempio n. 49
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',
        test_py_scripts.get_data_path('ogr') +
        'poly.shp -sql "select * from poly"')
    assert ret.find('Feature Count: 10') != -1
Esempio n. 50
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',
        test_py_scripts.get_data_path('ogr') +
        'poly.shp poly -where "EAS_ID=171"')
    assert ret.find('Feature Count: 1') != -1
Esempio n. 51
0
def test_ogr2ogr_py_12():

    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', '-nlt POLYGON25D tmp/poly.shp '+test_py_scripts.get_data_path('ogr')+'poly.shp')

    ds = ogr.Open('tmp/poly.shp')
    assert ds.GetLayer(0).GetLayerDefn().GetGeomType() == ogr.wkbPolygon25D
    ds.Destroy()

    ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('tmp/poly.shp')
Esempio n. 52
0
def test_gdallocationinfo_py_wgs84():
    script_path = test_py_scripts.get_py_script('gdallocationinfo')
    if script_path is None:
        pytest.skip()
    ret = test_py_scripts.run_py_script(
        script_path, 'gdallocationinfo',
        ' -valonly -wgs84 ../gcore/data/byte.tif -117.6354747 33.8970515')

    expected_ret = """115"""
    assert expected_ret in ret
Esempio n. 53
0
def test_ogr2ogr_py_46():

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

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

    gdal.Unlink('tmp/test_ogr2ogr_45.gml')
    gdal.Unlink('tmp/test_ogr2ogr_45.xsd')

    ds = ogr.GetDriverByName('ESRI Shapefile').CreateDataSource('tmp/test_ogr2ogr_45_src.shp')
    lyr = ds.CreateLayer('test_ogr2ogr_45_src', geom_type=ogr.wkbLineString)
    feat = ogr.Feature(lyr.GetLayerDefn())
    feat.SetGeometry(ogr.CreateGeometryFromWkt('LINESTRING(0 0,0 1,1 1,0 0)'))
    lyr.CreateFeature(feat)
    feat = ogr.Feature(lyr.GetLayerDefn())
    feat.SetGeometry(ogr.CreateGeometryFromWkt('MULTILINESTRING((0 0,0 1,1 1,0 0),(10 0,10 1,11 1,10 0))'))
    lyr.CreateFeature(feat)
    ds = None

    test_py_scripts.run_py_script(script_path, 'ogr2ogr', ' -f GML tmp/test_ogr2ogr_45.gml tmp/test_ogr2ogr_45_src.shp -nlt PROMOTE_TO_MULTI')

    f = open('tmp/test_ogr2ogr_45.xsd')
    data = f.read()
    f.close()

    assert data.find('type="gml:MultiLineStringPropertyType"') != -1

    f = open('tmp/test_ogr2ogr_45.gml')
    data = f.read()
    f.close()

    assert data.find('<ogr:geometryProperty><gml:MultiLineString><gml:lineStringMember><gml:LineString><gml:coordinates>0,0 0,1 1,1 0,0</gml:coordinates></gml:LineString></gml:lineStringMember></gml:MultiLineString></ogr:geometryProperty>') != -1

    ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('tmp/test_ogr2ogr_45_src.shp')
    gdal.Unlink('tmp/test_ogr2ogr_45.gml')
    gdal.Unlink('tmp/test_ogr2ogr_45.xsd')
Esempio n. 54
0
def test_ogrmerge_7():
    script_path = test_py_scripts.get_py_script('ogrmerge')
    if script_path is None:
        pytest.skip()

    # No match in -single mode
    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_geom_type POINT')

    ds = ogr.Open('tmp/out.vrt')
    assert ds.GetLayerCount() == 0
    ds = None

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

    # Match in single mode
    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_geom_type POLYGON')

    ds = ogr.Open('tmp/out.vrt')
    assert ds.GetLayerCount() == 1
    ds = None

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

    # No match in default mode
    test_py_scripts.run_py_script(
        script_path, 'ogrmerge', '-f VRT -o tmp/out.vrt ' +
        test_py_scripts.get_data_path('ogr') + 'poly.shp '
        '-src_geom_type POINT')

    ds = ogr.Open('tmp/out.vrt')
    assert ds.GetLayerCount() == 0
    ds = None

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

    # Match in default mode
    test_py_scripts.run_py_script(
        script_path, 'ogrmerge', '-f VRT -o tmp/out.vrt ' +
        test_py_scripts.get_data_path('ogr') + 'poly.shp '
        '-src_geom_type POLYGON')

    ds = ogr.Open('tmp/out.vrt')
    assert ds.GetLayerCount() == 1
    ds = None

    gdal.Unlink('tmp/out.vrt')
Esempio n. 55
0
def test_ogr2ogr_py_43():

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

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

    test_py_scripts.run_py_script(
        script_path, 'ogr2ogr',
        ' tmp/test_ogr2ogr_43_3d.shp ../ogr/data/poly.shp -dim 3')

    ds = ogr.Open('tmp/test_ogr2ogr_43_3d.shp')
    lyr = ds.GetLayerByIndex(0)
    assert lyr.GetGeomType() == ogr.wkbPolygon25D
    ds = None

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

    test_py_scripts.run_py_script(
        script_path, 'ogr2ogr',
        ' tmp/test_ogr2ogr_43_2d.shp tmp/test_ogr2ogr_43_3d.shp -dim 2')

    ds = ogr.Open('tmp/test_ogr2ogr_43_2d.shp')
    lyr = ds.GetLayerByIndex(0)
    assert lyr.GetGeomType() == ogr.wkbPolygon
    ds = None

    ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource(
        'tmp/test_ogr2ogr_43_2d.shp')
    ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource(
        'tmp/test_ogr2ogr_43_3d.shp')
def CheckFileSize(src_filename):

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

    test_py_scripts.run_py_script(script_path, 'ogr2ogr',
                                  'tmp/CheckFileSize.dbf ' + src_filename)
    statBufSrc = gdal.VSIStatL(
        src_filename, gdal.VSI_STAT_EXISTS_FLAG | gdal.VSI_STAT_NATURE_FLAG
        | gdal.VSI_STAT_SIZE_FLAG)
    statBufDst = gdal.VSIStatL(
        'tmp/CheckFileSize.dbf', gdal.VSI_STAT_EXISTS_FLAG
        | gdal.VSI_STAT_NATURE_FLAG | gdal.VSI_STAT_SIZE_FLAG)
    ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource(
        'tmp/CheckFileSize.dbf')

    assert statBufSrc.size == statBufDst.size, \
        ('src_size = %d, dst_size = %d', statBufSrc.size, statBufDst.size)
Esempio n. 57
0
def test_gdal_retile_2():

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

    try:
        os.mkdir('tmp/outretile2')
    except OSError:
        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')
    assert ds.GetRasterBand(1).Checksum() == 35, 'wrong checksum for band 1'
    assert ds.GetRasterBand(4).Checksum() == 35, 'wrong checksum for band 4'
    ds = None
Esempio n. 58
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'
Esempio n. 59
0
def test_ogr2ogr_py_9():
    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',
        '-a_srs EPSG:4326 tmp/poly.shp ../ogr/data/poly.shp')

    ds = ogr.Open('tmp/poly.shp')
    assert str(ds.GetLayer(0).GetSpatialRef()).find('1984') != -1
    ds.Destroy()

    ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('tmp/poly.shp')
Esempio n. 60
0
def test_ogr2ogr_py_2():
    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 -sql "select * from poly"')

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

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