def mrsid_online_4(): if gdaltest.jp2mrsid_drv is None: return 'skip' if not gdaltest.download_file('http://www.openjpeg.org/samples/Bretagne2.j2k', 'Bretagne2.j2k'): return 'skip' if not gdaltest.download_file('http://www.openjpeg.org/samples/Bretagne2.bmp', 'Bretagne2.bmp'): return 'skip' # Checksum = 53186 on my PC tst = gdaltest.GDALTest('JP2MrSID', 'tmp/cache/Bretagne2.j2k', 1, None, filename_absolute=1) if tst.testOpen() != 'success': return 'fail' ds = gdal.Open('tmp/cache/Bretagne2.j2k') ds_ref = gdal.Open('tmp/cache/Bretagne2.bmp') maxdiff = gdaltest.compare_ds(ds, ds_ref, width=256, height=256) ds = None ds_ref = None # Difference between the image before and after compression if maxdiff > 1: print(ds.GetRasterBand(1).Checksum()) print(ds_ref.GetRasterBand(1).Checksum()) gdaltest.post_reason('Image too different from reference') return 'fail' return 'success'
def hfa_write_desc(): src_ds = gdal.Open( 'data/byte.tif' ) new_ds = gdal.GetDriverByName('HFA').CreateCopy( 'tmp/test_desc.img', src_ds ) bnd = new_ds.GetRasterBand(1) bnd.SetDescription( 'CustomBandName' ) bnd = None src_ds = None new_ds = None new_ds = gdal.Open( 'tmp/test_desc.img' ) bnd = new_ds.GetRasterBand(1) if bnd.GetDescription() != 'CustomBandName': gdaltest.post_reason( 'Didnt get custom band name.' ) return 'fail' bnd = None new_ds = None gdal.GetDriverByName('HFA').Delete( 'tmp/test_desc.img' ) return 'success'
def test_gdaldem_lib_hillshade_azimuth(): from sys import version_info src_ds = gdal.GetDriverByName('MEM').Create('', 100, 100, 1) src_ds.SetGeoTransform([2,0.01,0,49,0,-0.01]) sr = osr.SpatialReference() sr.ImportFromEPSG(4326) src_ds.SetProjection(sr.ExportToWkt()) for j in range(100): data = '' for i in range(100): val = 255 - 5 * max(abs(50-i),abs(50-j)) data = data + ('%c' % (val)) if version_info >= (3,0,0): data = bytes(data, 'ISO-8859-1') src_ds.GetRasterBand(1).WriteRaster(0,j,100,1,data) # Light from the east ds = gdal.DEMProcessing('', src_ds, 'hillshade', format = 'MEM', azimuth = 90, scale = 111120, zFactor = 100) if ds is None: return 'fail' ds_ref = gdal.Open('data/pyramid_shaded_ref.tif') if gdaltest.compare_ds(ds, ds_ref, verbose = 1) > 1: gdaltest.post_reason('Bad checksum') return 'fail' ds = None ds_ref = None return 'success'
def osr_ct_3(): if gdaltest.have_proj4 == 0: return 'skip' utm_srs = osr.SpatialReference() utm_srs.SetUTM( 11 ) utm_srs.SetWellKnownGeogCS( 'WGS84' ) ll_srs = osr.SpatialReference() ll_srs.SetWellKnownGeogCS( 'WGS84' ) ct = osr.CoordinateTransformation( ll_srs, utm_srs ) pnt = ogr.CreateGeometryFromWkt( 'POINT(-117.5 32.0)', ll_srs ) result = pnt.Transform( ct ) ll_srs = None ct = None utm_srs = None out_srs = pnt.GetSpatialReference().ExportToPrettyWkt() if out_srs[0:6] != 'PROJCS': gdaltest.post_reason( 'output srs corrupt, ref counting issue?' ) return 'fail' pnt = None return 'success'
def asyncreader_1(): ds = gdal.Open('data/rgbsmall.tif') asyncreader = ds.BeginAsyncReader(0,0,ds.RasterXSize,ds.RasterYSize) buf = asyncreader.GetBuffer() result = asyncreader.GetNextUpdatedRegion(0) if result != [gdal.GARIO_COMPLETE, 0, 0, ds.RasterXSize,ds.RasterYSize]: gdaltest.post_reason('wrong return values for GetNextUpdatedRegion()') print(result) return 'fail' ds.EndAsyncReader(asyncreader) asyncreader = None out_ds = gdal.GetDriverByName('GTiff').Create('/vsimem/asyncresult.tif', ds.RasterXSize,ds.RasterYSize,ds.RasterCount) out_ds.WriteRaster(0,0,ds.RasterXSize,ds.RasterYSize,buf) expected_cs = [ ds.GetRasterBand(i+1).Checksum() for i in range(ds.RasterCount)] cs = [ out_ds.GetRasterBand(i+1).Checksum() for i in range(ds.RasterCount)] ds = None out_ds = None gdal.Unlink('/vsimem/asyncresult.tif') for i in range(len(cs)): if cs[i] != expected_cs[i]: gdaltest.post_reason('did not get expected checksum for band %d' % (i+1)) print(cs[i]) print(expected_cs[i]) 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: 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'
def check_qix_non_overlapping_geoms(lyr): geoms = [] lyr.SetSpatialFilter(None) extents = lyr.GetExtent() fc_ref = lyr.GetFeatureCount() feat = lyr.GetNextFeature() while feat is not None: geom = feat.GetGeometryRef() geoms.append(geom.Clone()) feat = lyr.GetNextFeature() # Test getting each geom 1 by 1 for geom in geoms: bbox = geom.GetEnvelope() lyr.SetSpatialFilterRect(bbox[0], bbox[2], bbox[1], bbox[3]) lyr.ResetReading() feat = lyr.GetNextFeature() got_geom = feat.GetGeometryRef() if got_geom.Equals(geom) == 0: gdaltest.post_reason('expected %s. got %s' % (geom.ExportToWkt(), got_geom.ExportToWkt())) return 'fail' # Get all geoms in a single gulp. We do not use exactly the extent bounds, because # there is an optimization in the shapefile driver to skip the spatial index in that # case. That trick can only work with non point geometries of course lyr.SetSpatialFilterRect(extents[0] + 0.001, extents[2] + 0.001, extents[1] - 0.001, extents[3] - 0.001) lyr.ResetReading() fc = lyr.GetFeatureCount() if fc != fc_ref: gdaltest.post_reason('expected %d. got %d' % (fc_ref, fc)) return 'fail' return 'success'
def ogr_plscenes_data_v1_catalog_no_paging(): if gdaltest.plscenes_drv is None: return 'skip' gdal.FileFromMemBuffer('/vsimem/data_v1/item-types', '{ "item_types": [ { "id": "PSScene3Band" } ] }') gdal.SetConfigOption('PL_URL', '/vsimem/data_v1/') ds = gdal.OpenEx('PLScenes:', gdal.OF_VECTOR, open_options = ['VERSION=data_v1', 'API_KEY=foo']) gdal.SetConfigOption('PL_URL', None) if ds is None: gdaltest.post_reason('fail') return 'fail' with gdaltest.error_handler(): if ds.GetLayerByName('non_existing') is not None: gdaltest.post_reason('fail') return 'fail' if ds.GetLayerByName('PSScene3Band') is None: gdaltest.post_reason('fail') return 'fail' if ds.GetLayerCount() != 1: gdaltest.post_reason('fail') return 'fail' with gdaltest.error_handler(): if ds.GetLayerByName('non_existing') is not None: gdaltest.post_reason('fail') return 'fail' gdal.Unlink('/vsimem/data_v1/item-types') return 'success'
def mask_21(): types = [ gdal.GDT_Byte, gdal.GDT_Int16, gdal.GDT_UInt16, gdal.GDT_Int32, gdal.GDT_UInt32, gdal.GDT_Float32, gdal.GDT_Float64, gdal.GDT_CFloat32, gdal.GDT_CFloat64 ] nodatavalue = [ 1, -1, 1, -1, 1, 0.5, 0.5, 0.5, 0.5 ] drv = gdal.GetDriverByName('GTiff') for i in range(len(types)): ds = drv.Create('tmp/mask21.tif', 1, 1, 3, types[i]) md = {} md['NODATA_VALUES'] = '%f %f %f' % (nodatavalue[i], nodatavalue[i], nodatavalue[i]) ds.SetMetadata(md) ds.GetRasterBand(1).Fill(nodatavalue[i]) ds.GetRasterBand(2).Fill(nodatavalue[i]) ds.GetRasterBand(3).Fill(nodatavalue[i]) if ds.GetRasterBand(1).GetMaskFlags() != gdal.GMF_PER_DATASET + gdal.GMF_NODATA: gdaltest.post_reason( 'did not get expected mask flags for type %s' % gdal.GetDataTypeName(types[i]) ) return 'fail' msk = ds.GetRasterBand(1).GetMaskBand() if msk.Checksum() != 0: gdaltest.post_reason( 'did not get expected mask checksum for type %s : %d' % (gdal.GetDataTypeName(types[i]), msk.Checksum()) ) return 'fail' msk = None ds = None drv.Delete('tmp/mask21.tif') return 'success'
def plmosaic_14(): if gdaltest.plmosaic_drv is None: return 'skip' gdal.FileFromMemBuffer('/vsimem/root/my_mosaic', """{ "name": "my_mosaic", "coordinate_system": "EPSG:3857", "datatype": "byte", "quad_pattern": "my_{glevel:d}_missing_tilex_and_tiley", "quad_size": 4096, "resolution": 4.77731426716, "links" : { "quads" : "/vsimem/root/my_mosaic/quads/" } }""") gdal.SetConfigOption('PL_URL', '/vsimem/root') gdal.PushErrorHandler() ds = gdal.OpenEx('PLMosaic:', gdal.OF_RASTER, open_options = ['API_KEY=foo', 'MOSAIC=my_mosaic']) gdal.PopErrorHandler() gdal.SetConfigOption('PL_URL', None) if ds is not None or gdal.GetLastErrorMsg().find('Invalid quad_pattern') < 0: gdaltest.post_reason('fail') print(gdal.GetLastErrorMsg()) return 'fail' return 'success'
def plmosaic_19(): if gdaltest.plmosaic_drv is None: return 'skip' gdal.SetConfigOption('PL_URL', '/vsimem/root') ds = gdal.OpenEx('PLMosaic:', gdal.OF_RASTER, open_options = ['API_KEY=foo', 'MOSAIC=my_mosaic', 'CACHE_PATH=/does_not_exist']) gdal.SetConfigOption('PL_URL', None) gdal.PushErrorHandler() val = ds.ReadRaster(0,0,1,1) gdal.PopErrorHandler() val = struct.unpack('B' * 4, val) if val != (254, 0, 0, 0): gdaltest.post_reason('fail') print(val) return 'fail' val = ds.ReadRaster(256,0,1,1) val = struct.unpack('B' * 4, val) if val != (254, 0, 0, 0): gdaltest.post_reason('fail') print(val) return 'fail' ds = None return 'success'
def l1b_solar_zenith_angles_before_noaa_15(): try: os.stat('tmp/cache/n12gac10bit.l1b') except: return 'skip' ds = gdal.Open('tmp/cache/n12gac10bit.l1b') md = ds.GetMetadata('SUBDATASETS') expected_md = { 'SUBDATASET_1_NAME' : 'L1B_SOLAR_ZENITH_ANGLES:"tmp/cache/n12gac10bit.l1b"', 'SUBDATASET_1_DESC' : 'Solar zenith angles' } for key in expected_md: if md[key] != expected_md[key]: print(md) return 'fail' ds = None ds = gdal.Open('L1B_SOLAR_ZENITH_ANGLES:"tmp/cache/n12gac10bit.l1b"') cs = ds.GetRasterBand(1).Checksum() if cs != 22924: gdaltest.post_reason('fail') print(cs) return 'fail' return 'success'
def l1b_clouds_after_noaa_15(): try: os.stat('tmp/cache/n16gac10bit.l1b') except: return 'skip' ds = gdal.Open('tmp/cache/n16gac10bit.l1b') md = ds.GetMetadata('SUBDATASETS') expected_md = { 'SUBDATASET_2_NAME' : 'L1B_CLOUDS:"tmp/cache/n16gac10bit.l1b"', 'SUBDATASET_2_DESC' : 'Clouds from AVHRR (CLAVR)' } for key in expected_md: if md[key] != expected_md[key]: print(md) return 'fail' ds = None ds = gdal.Open('L1B_CLOUDS:"tmp/cache/n16gac10bit.l1b"') cs = ds.GetRasterBand(1).Checksum() if cs != 0: gdaltest.post_reason('fail') print(cs) return 'fail' return 'success'
def hfa_delete_colortable(): # copy a file to tmp dir to modify. open('tmp/i8u.img','wb').write(open('data/i8u_c_i.img', 'rb').read()) # clear color table. ds = gdal.Open( 'tmp/i8u.img', gdal.GA_Update ) try: ds.GetRasterBand(1).SetColorTable except: # OG python bindings don't have SetColorTable, and if we use # SetRasterColorTable, it doesn't work either as None isn't a valid # value for them ds = None gdal.GetDriverByName('HFA').Delete('tmp/i8u.img') return 'skip' ds.GetRasterBand(1).SetColorTable(None) ds = None # check color table gone. ds = gdal.Open( 'tmp/i8u.img' ) if ds.GetRasterBand(1).GetColorTable() != None: gdaltest.post_reason( 'failed to remove color table' ) return 'fail' ds = None gdal.GetDriverByName('HFA').Delete('tmp/i8u.img') return 'success'
def hfa_delete_colortable2(): # copy a file to tmp dir to modify. src_ds = gdal.Open('../gcore/data/8bit_pal.bmp') ds = gdal.GetDriverByName('HFA').CreateCopy('tmp/hfa_delete_colortable2.img', src_ds) src_ds = None ds = None # clear color table. ds = gdal.Open( 'tmp/hfa_delete_colortable2.img', gdal.GA_Update ) try: ds.GetRasterBand(1).SetColorTable except: # OG python bindings don't have SetColorTable, and if we use # SetRasterColorTable, it doesn't work either as None isn't a valid # value for them ds = None gdal.GetDriverByName('HFA').Delete('tmp/hfa_delete_colortable2.img') return 'skip' ds.GetRasterBand(1).SetColorTable(None) ds = None # check color table gone. ds = gdal.Open( 'tmp/hfa_delete_colortable2.img' ) if ds.GetRasterBand(1).GetColorTable() != None: gdaltest.post_reason( 'failed to remove color table' ) return 'fail' ds = None gdal.GetDriverByName('HFA').Delete('tmp/hfa_delete_colortable2.img') return 'success'
def hfa_proName(): drv = gdal.GetDriverByName('HFA') src_ds = gdal.Open('data/stateplane.vrt') dst_ds = drv.CreateCopy( 'tmp/proname.img', src_ds ) dst_ds = None src_ds = None # Make sure we don't have interference from an .aux.xml try: os.remove('tmp/proname.img.aux.xml') except: pass ds = gdal.Open( 'tmp/proname.img' ) srs = ds.GetProjectionRef() if srs[:55] != 'PROJCS["NAD_1983_StatePlane_Ohio_South_FIPS_3402_Feet",': gdaltest.post_reason( 'did not get expected PROJCS name.' ) print(srs) result = 'fail' else: result = 'success' ds = None drv.Delete( 'tmp/proname.img' ) return result
def hfa_unique_values_color_table(): ds = gdal.Open( 'data/i8u_c_i.img' ) ct = ds.GetRasterBand(1).GetRasterColorTable() if ct.GetCount() != 256: print(ct.GetCount()) gdaltest.post_reason( 'got wrong color count' ) return 'fail' if ct.GetColorEntry(253) != (0,0,0,0) \ or ct.GetColorEntry(254) != (255,255,170,255) \ or ct.GetColorEntry(255) != (255,255,255,255): print(ct.GetColorEntry(253)) print(ct.GetColorEntry(254)) print(ct.GetColorEntry(255)) gdaltest.post_reason( 'Got wrong colors' ) return 'fail' ct = None ds = None return 'success'
def hfa_grow_rrdlist(): import shutil shutil.copyfile('data/bug_1109.img' , 'tmp/bug_1109.img') #os.system("copy data\\bug_1109.img tmp") # Add two overview levels. ds = gdal.Open('tmp/bug_1109.img',gdal.GA_Update) result = ds.BuildOverviews( overviewlist = [4,8] ) ds = None if result != 0: gdaltest.post_reason( 'BuildOverviews failed.' ) return 'fail' # Verify overviews are now findable. ds = gdal.Open( 'tmp/bug_1109.img' ) if ds.GetRasterBand(1).GetOverviewCount() != 3: gdaltest.post_reason( 'Overview count wrong.' ) print(ds.GetRasterBand(1).GetOverviewCount()) return 'fail' ds = None gdal.GetDriverByName('HFA').Delete( 'tmp/bug_1109.img' ) return 'success'
def hfa_nodata_read(): ds = gdal.Open( 'tmp/nodata.img' ) b = ds.GetRasterBand(1) if b.GetNoDataValue() != 1: gdaltest.post_reason( 'failed to preserve nodata value' ) return 'fail' stats = b.GetStatistics(False, True) tolerance = 0.0001 if abs(stats[0] - 2) > tolerance: gdaltest.post_reason( 'Minimum value is wrong.' ) return 'fail' if abs(stats[1] - 4) > tolerance: gdaltest.post_reason( 'Maximum value is wrong.' ) return 'fail' if abs(stats[2] - 2.6666666666667) > tolerance: gdaltest.post_reason( 'Mean value is wrong.' ) return 'fail' if abs(stats[3] - 0.94280904158206) > tolerance: gdaltest.post_reason( 'StdDev value is wrong.' ) return 'fail' b = None ds = None gdal.GetDriverByName( 'HFA' ).Delete( 'tmp/nodata.img' ) return 'success'
def test_gdaltindex_3(): if test_cli_utilities.get_gdaltindex_path() is None: return 'skip' drv = gdal.GetDriverByName('GTiff') wkt = 'GEOGCS[\"WGS 72\",DATUM[\"WGS_1972\"]]' ds = drv.Create('tmp/gdaltindex5.tif', 10, 10, 1) ds.SetProjection(wkt) ds.SetGeoTransform([47, 0.1, 0, 2, 0, -0.1]) ds = None (_, ret_stderr) = gdaltest.runexternal_out_and_err(test_cli_utilities.get_gdaltindex_path() + ' -skip_different_projection tmp/tileindex.shp tmp/gdaltindex5.tif') if ret_stderr.find('Warning : tmp/gdaltindex5.tif is not using the same projection system as other files in the tileindex.') == -1 or \ ret_stderr.find('Use -t_srs option to set target projection system (not supported by MapServer).') == -1: print(ret_stderr) gdaltest.post_reason('got unexpected error message \n[%s]' % (ret_stderr)) return 'fail' ds = ogr.Open('tmp/tileindex.shp') if ds.GetLayer(0).GetFeatureCount() != 4: return 'fail' ds.Destroy() return 'success'
def hfa_createcopy_statistics(): tmpAuxXml = '../gcore/data/byte.tif.aux.xml' try: os.remove(tmpAuxXml) except: pass ds_src = gdal.Open('../gcore/data/byte.tif') out_ds = gdal.GetDriverByName('HFA').CreateCopy( '/vsimem/byte.img', ds_src, options = ['STATISTICS=YES'] ) out_ds = None ds_src = None if os.path.exists(tmpAuxXml): os.remove(tmpAuxXml) gdal.Unlink( '/vsimem/byte.img.aux.xml' ) ds = gdal.Open('/vsimem/byte.img') md = ds.GetRasterBand(1).GetMetadata() ds = None gdal.GetDriverByName('HFA').Delete('/vsimem/byte.img') if md['STATISTICS_MINIMUM'] != '74': gdaltest.post_reason( 'STATISTICS_MINIMUM is wrong.' ) print(md['STATISTICS_MINIMUM']) return 'fail' return 'success'
def ogr_rfc28_41(): ds = ogr.GetDriverByName("Memory").CreateDataSource( "my_ds") lyr = ds.CreateLayer( "my_layer") field_defn = ogr.FieldDefn('a.b', ogr.OFTInteger) lyr.CreateField(field_defn) feat = ogr.Feature(lyr.GetLayerDefn()) feat.SetField(0, 2) lyr.CreateFeature(feat) sql_lyr = ds.ExecuteSQL( 'select * from my_layer' ) feat = sql_lyr.GetNextFeature() if feat.GetField('a.b') != 2: gdaltest.post_reason('fail') return 'fail' ds.ReleaseResultSet( sql_lyr ) sql_lyr = ds.ExecuteSQL( 'select l.* from my_layer l' ) feat = sql_lyr.GetNextFeature() if feat.GetField('l.a.b') != 2: gdaltest.post_reason('fail') return 'fail' ds.ReleaseResultSet( sql_lyr ) ds = None return 'success'
def ogr_rfc28_12(): lyr = gdaltest.ds.ExecuteSQL( "SELECT 'constant string', 'other' as abc, eas_id from idlink where eas_id = 165" ) count = lyr.GetFeatureCount() if count != 1: gdaltest.post_reason( 'Got wrong count with GetFeatureCount() - %d, expecting 1' % count ) return 'fail' expect = [ 'other' ] tr = ogrtest.check_features_against_list( lyr, 'abc', expect ) expect = [ 165 ] if tr: lyr.ResetReading() tr = ogrtest.check_features_against_list( lyr, 'eas_id', expect ) expect = [ 'constant string' ] if tr: lyr.ResetReading() tr = ogrtest.check_features_against_list( lyr, 'field_1', expect ) gdaltest.ds.ReleaseResultSet( lyr ) if tr: return 'success' else: return 'fail'
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'
def srtmhgt_1(): ds = gdal.Open( 'data/n43.dt0' ) bandSrc = ds.GetRasterBand(1) driver = gdal.GetDriverByName( "GTiff" ); dsDst = driver.Create( 'tmp/n43.dt1.tif', 1201, 1201, 1, gdal.GDT_Int16 ) dsDst.SetProjection('GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]]') dsDst.SetGeoTransform((-80.0004166666666663, 0.0008333333333333, 0, 44.0004166666666670, 0, -0.0008333333333333)) bandDst = dsDst.GetRasterBand(1) data = bandSrc.ReadRaster( 0, 0, 121, 121, 1201, 1201, gdal.GDT_Int16 ) bandDst.WriteRaster( 0, 0, 1201, 1201, data, 1201, 1201, gdal.GDT_Int16 ) bandDst.FlushCache() bandDst = None ds = None dsDst = None ds = gdal.Open( 'tmp/n43.dt1.tif' ) driver = gdal.GetDriverByName( "SRTMHGT" ); dsDst = driver.CreateCopy( 'tmp/n43w080.hgt', ds) band = dsDst.GetRasterBand(1) chksum = band.Checksum() if chksum != 60918: gdaltest.post_reason('Wrong checksum. Checksum found %d' % chksum) return 'fail' return 'success'
def osr_ct_6(): if gdaltest.have_proj4 == 0: return 'skip' ct = osr.CreateCoordinateTransformation( None, None ) if ct is not None: return 'fail' utm_srs = osr.SpatialReference() utm_srs.SetUTM( 11 ) utm_srs.SetWellKnownGeogCS( 'WGS84' ) ll_srs = osr.SpatialReference() ll_srs.SetWellKnownGeogCS( 'WGS84' ) ct = osr.CoordinateTransformation( ll_srs, utm_srs ) if ct is None: return 'fail' result = ct.TransformPoints( ( (-117.5, 32.0, 0.0), (-117.5, 32.0) ) ) for i in range(2): if abs(result[i][0] - 452772.06) > 0.01 \ or abs(result[i][1] - 3540544.89 ) > 0.01 \ or abs(result[i][2] - 0.0) > 0.01: gdaltest.post_reason( 'Wrong LL to UTM result' ) return 'fail' return 'success'
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'
def ogr_rfc28_24(): sql_lyr = gdaltest.ds.ExecuteSQL( "select *, NULL, NULL as nullstrfield, CAST(null as integer) as nullintfield from poly where NULL IS NULL" ) feat = sql_lyr.GetNextFeature() if feat.IsFieldSet('FIELD_4'): feat.DumpReadable() gdaltest.ds.ReleaseResultSet( sql_lyr ) return 'fail' if feat.IsFieldSet('nullstrfield'): feat.DumpReadable() gdaltest.ds.ReleaseResultSet( sql_lyr ) return 'fail' if feat.IsFieldSet('nullintfield'): feat.DumpReadable() gdaltest.ds.ReleaseResultSet( sql_lyr ) return 'fail' count = sql_lyr.GetFeatureCount() gdaltest.ds.ReleaseResultSet( sql_lyr ) if count != 10: gdaltest.post_reason( 'Got wrong count with GetFeatureCount() - %d, expecting %d' % (count, 10) ) return 'fail' return 'success'
def osr_ct_5(): if gdaltest.have_proj4 == 0: return 'skip' # Test NG bindings try: x = gdal.Transformer gdaltest.have_ng = 1 except: gdaltest.have_ng = 0 return 'skip' utm_srs = osr.SpatialReference() utm_srs.SetUTM( 11 ) utm_srs.SetWellKnownGeogCS( 'WGS84' ) ll_srs = osr.SpatialReference() ll_srs.SetWellKnownGeogCS( 'WGS84' ) gdaltest.ct = osr.CoordinateTransformation( ll_srs, utm_srs ) result = gdaltest.ct.TransformPoints( ( (-117.5, 32.0, 0.0), (-117.5, 32.0) ) ) for i in range(2): if abs(result[i][0] - 452772.06) > 0.01 \ or abs(result[i][1] - 3540544.89 ) > 0.01 \ or abs(result[i][2] - 0.0) > 0.01: gdaltest.post_reason( 'Wrong LL to UTM result' ) return 'fail' return 'success'
def stats_nan_3(): src_ds = gdal.Open('data/nan32_nodata.tif') nodata = src_ds.GetRasterBand(1).GetNoDataValue() if not gdaltest.isnan(nodata): gdaltest.post_reason('expected nan, got %f' % nodata) return 'fail' out_ds = gdaltest.gtiff_drv.CreateCopy('tmp/nan32_nodata.tif', src_ds) del out_ds src_ds = None try: os.remove('tmp/nan32_nodata.tif.aux.xml') except: pass ds = gdal.Open('tmp/nan32_nodata.tif') nodata = ds.GetRasterBand(1).GetNoDataValue() ds = None gdaltest.gtiff_drv.Delete('tmp/nan32_nodata.tif') if not gdaltest.isnan(nodata): gdaltest.post_reason('expected nan, got %f' % nodata) return 'fail' return 'success'
def test_gdaltindex_1(): if test_cli_utilities.get_gdaltindex_path() is None: return 'skip' try: os.remove('tmp/tileindex.shp') except: pass try: os.remove('tmp/tileindex.dbf') except: pass try: os.remove('tmp/tileindex.shx') except: pass try: os.remove('tmp/tileindex.prj') except: pass drv = gdal.GetDriverByName('GTiff') wkt = 'GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9108\"]],AUTHORITY[\"EPSG\",\"4326\"]]' ds = drv.Create('tmp/gdaltindex1.tif', 10, 10, 1) ds.SetProjection(wkt) ds.SetGeoTransform([49, 0.1, 0, 2, 0, -0.1]) ds = None ds = drv.Create('tmp/gdaltindex2.tif', 10, 10, 1) ds.SetProjection(wkt) ds.SetGeoTransform([49, 0.1, 0, 3, 0, -0.1]) ds = None ds = drv.Create('tmp/gdaltindex3.tif', 10, 10, 1) ds.SetProjection(wkt) ds.SetGeoTransform([48, 0.1, 0, 2, 0, -0.1]) ds = None ds = drv.Create('tmp/gdaltindex4.tif', 10, 10, 1) ds.SetProjection(wkt) ds.SetGeoTransform([48, 0.1, 0, 3, 0, -0.1]) ds = None (out, err) = gdaltest.runexternal_out_and_err( test_cli_utilities.get_gdaltindex_path() + ' tmp/tileindex.shp tmp/gdaltindex1.tif tmp/gdaltindex2.tif') if not (err is None or err == ''): gdaltest.post_reason('got error/warning') print(err) return 'fail' (ret_stdout, ret_stderr) = gdaltest.runexternal_out_and_err( test_cli_utilities.get_gdaltindex_path() + ' tmp/tileindex.shp tmp/gdaltindex3.tif tmp/gdaltindex4.tif') ds = ogr.Open('tmp/tileindex.shp') if ds.GetLayer(0).GetFeatureCount() != 4: gdaltest.post_reason('fail') print(ret_stdout) print(ret_stderr) print(ds.GetLayer(0).GetFeatureCount()) return 'fail' tileindex_wkt = ds.GetLayer(0).GetSpatialRef().ExportToWkt() if tileindex_wkt.find('WGS_1984') == -1: gdaltest.post_reason('fail') print(ret_stdout) print(ret_stderr) print(tileindex_wkt) return 'fail' expected_wkts = [ 'POLYGON ((49 2,50 2,50 1,49 1,49 2))', 'POLYGON ((49 3,50 3,50 2,49 2,49 3))', 'POLYGON ((48 2,49 2,49 1,48 1,48 2))', 'POLYGON ((48 3,49 3,49 2,48 2,48 3))' ] i = 0 feat = ds.GetLayer(0).GetNextFeature() while feat is not None: if feat.GetGeometryRef().ExportToWkt() != expected_wkts[i]: print('i=%d, wkt=%s' % (i, feat.GetGeometryRef().ExportToWkt())) return 'fail' i = i + 1 feat = ds.GetLayer(0).GetNextFeature() ds.Destroy() return 'success'
def ogr_rfc41_2(): # Check implicit geometry field creation feature_defn = ogr.FeatureDefn() if feature_defn.GetGeomFieldCount() != 1: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomType() != ogr.wkbUnknown: gdaltest.post_reason('fail') return 'fail' # Test IsSame() if feature_defn.IsSame(feature_defn) != 1: gdaltest.post_reason('fail') return 'fail' other_feature_defn = ogr.FeatureDefn() if feature_defn.IsSame(other_feature_defn) != 1: gdaltest.post_reason('fail') return 'fail' other_feature_defn.GetGeomFieldDefn(0).SetSpatialRef( osr.SpatialReference()) if feature_defn.IsSame(other_feature_defn) != 0: gdaltest.post_reason('fail') return 'fail' feature_defn.GetGeomFieldDefn(0).SetSpatialRef(osr.SpatialReference()) if feature_defn.IsSame(other_feature_defn) != 1: gdaltest.post_reason('fail') return 'fail' other_feature_defn.GetGeomFieldDefn(0).SetSpatialRef(None) if feature_defn.IsSame(other_feature_defn) != 0: gdaltest.post_reason('fail') return 'fail' feature_defn = None feature_defn = ogr.FeatureDefn() # Check changing geometry type feature_defn.SetGeomType(ogr.wkbPoint) if feature_defn.GetGeomType() != ogr.wkbPoint: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomFieldDefn(0).GetType() != ogr.wkbPoint: gdaltest.post_reason('fail') return 'fail' # Check setting to wkbNone and implicitly destroying the field. for _ in range(2): feature_defn.SetGeomType(ogr.wkbNone) if feature_defn.GetGeomFieldCount() != 0: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomType() != ogr.wkbNone: gdaltest.post_reason('fail') return 'fail' # Recreate the field for t in [ogr.wkbPoint, ogr.wkbLineString]: feature_defn.SetGeomType(t) if feature_defn.GetGeomFieldCount() != 1: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomType() != t: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomFieldDefn(0).GetType() != t: gdaltest.post_reason('fail') return 'fail' # Test setting invalid value old_val = feature_defn.GetGeomType() gdal.PushErrorHandler('CPLQuietErrorHandler') feature_defn.SetGeomType(-3) gdal.PopErrorHandler() if feature_defn.GetGeomType() != old_val: gdaltest.post_reason('fail') return 'fail' # Test SetIgnored() / IsIgnored() if feature_defn.IsGeometryIgnored() != 0: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomFieldDefn(0).IsIgnored() != 0: gdaltest.post_reason('fail') return 'fail' feature_defn.SetGeometryIgnored(1) if feature_defn.IsGeometryIgnored() != 1: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomFieldDefn(0).IsIgnored() != 1: gdaltest.post_reason('fail') return 'fail' # Test wrong index values for GetGeomFieldDefn() for idx in [-1, 1]: gdal.PushErrorHandler('CPLQuietErrorHandler') ret = feature_defn.GetGeomFieldDefn(idx) gdal.PopErrorHandler() if ret is not None: gdaltest.post_reason('fail') return 'fail' # Test GetGeomFieldIndex() if feature_defn.GetGeomFieldIndex("") != 0: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomFieldIndex("invalid") != -1: gdaltest.post_reason('fail') return 'fail' # Test AddGeomFieldDefn() gfld_defn = ogr.GeomFieldDefn('polygon_field', ogr.wkbPolygon) feature_defn.AddGeomFieldDefn(gfld_defn) if feature_defn.GetGeomFieldCount() != 2: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomFieldIndex("polygon_field") != 1: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomFieldDefn(1).GetName() != 'polygon_field': gdaltest.post_reason('fail') return 'fail' # Test DeleteGeomFieldDefn() : error cases if feature_defn.DeleteGeomFieldDefn(-1) == 0: gdaltest.post_reason('fail') return 'fail' if feature_defn.DeleteGeomFieldDefn(2) == 0: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomFieldCount() != 2: gdaltest.post_reason('fail') return 'fail' # Test DeleteGeomFieldDefn() : valid cases if feature_defn.DeleteGeomFieldDefn(0) != 0: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomFieldCount() != 1: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomFieldIndex("polygon_field") != 0: gdaltest.post_reason('fail') return 'fail' if feature_defn.DeleteGeomFieldDefn(0) != 0: gdaltest.post_reason('fail') return 'fail' if feature_defn.GetGeomFieldCount() != 0: gdaltest.post_reason('fail') return 'fail' if feature_defn.IsSame(feature_defn) != 1: gdaltest.post_reason('fail') return 'fail' if feature_defn.IsSame(ogr.FeatureDefn()) != 0: gdaltest.post_reason('fail') return 'fail' feature_defn = None return 'success'
def ogr_rfc41_8(): import ogr_sql_sqlite if not ogr_sql_sqlite.ogr_sql_sqlite_available(): return 'skip' ds = ogr.GetDriverByName('memory').CreateDataSource('') lyr = ds.CreateLayer('mytable', geom_type=ogr.wkbPolygon) lyr.GetLayerDefn().GetGeomFieldDefn(0).SetName('geomfield') gfld_defn = ogr.GeomFieldDefn('geomfield2', ogr.wkbPoint25D) sr = osr.SpatialReference() sr.ImportFromEPSG(4326) gfld_defn.SetSpatialRef(sr) lyr.CreateGeomField(gfld_defn) # Check that we get the geometry columns, even with no features sql_lyr = ds.ExecuteSQL('SELECT * FROM mytable', dialect='SQLite') if sql_lyr.GetLayerDefn().GetGeomFieldCount() != 2: print(sql_lyr.GetLayerDefn().GetGeomFieldCount()) gdaltest.post_reason('fail') return 'fail' if sql_lyr.GetLayerDefn().GetGeomFieldDefn(0).GetType() != ogr.wkbPolygon: gdaltest.post_reason('fail') return 'fail' if sql_lyr.GetLayerDefn().GetGeomFieldDefn(0).GetSpatialRef() is not None: gdaltest.post_reason('fail') return 'fail' if sql_lyr.GetLayerDefn().GetGeomFieldDefn(1).GetType() != ogr.wkbPoint25D: gdaltest.post_reason('fail') return 'fail' srs = sql_lyr.GetLayerDefn().GetGeomFieldDefn(1).GetSpatialRef() if srs.GetAuthorityCode(None) != '4326': gdaltest.post_reason('fail') return 'fail' ds.ReleaseResultSet(sql_lyr) # Test INSERT INTO request ds.ExecuteSQL("INSERT INTO mytable (geomfield, geomfield2) VALUES (" + "GeomFromText('POLYGON ((0 0,0 1,1 1,1 0,0 0))'), " + "GeomFromText('POINT Z(0 1 2)') )", dialect='SQLite') # Check output sql_lyr = ds.ExecuteSQL('SELECT geomfield2, geomfield FROM mytable', dialect='SQLite') feat = sql_lyr.GetNextFeature() geom = feat.GetGeomFieldRef('geomfield') if geom.ExportToWkt() != 'POLYGON ((0 0,0 1,1 1,1 0,0 0))': feat.DumpReadable() gdaltest.post_reason('fail') return 'fail' geom = feat.GetGeomFieldRef('geomfield2') if geom.ExportToWkt() != 'POINT (0 1 2)': feat.DumpReadable() gdaltest.post_reason('fail') return 'fail' feat = None ds.ReleaseResultSet(sql_lyr) # Test UPDATE ds.ExecuteSQL("UPDATE mytable SET geomfield2 = " + "GeomFromText('POINT Z(3 4 5)')", dialect='SQLite') # Check output sql_lyr = ds.ExecuteSQL('SELECT geomfield2, geomfield FROM mytable', dialect='SQLite') feat = sql_lyr.GetNextFeature() geom = feat.GetGeomFieldRef('geomfield') if geom.ExportToWkt() != 'POLYGON ((0 0,0 1,1 1,1 0,0 0))': feat.DumpReadable() gdaltest.post_reason('fail') return 'fail' geom = feat.GetGeomFieldRef('geomfield2') if geom.ExportToWkt() != 'POINT (3 4 5)': feat.DumpReadable() gdaltest.post_reason('fail') return 'fail' feat = None ds.ReleaseResultSet(sql_lyr) return 'success'
def vrtderived_10(): try: import numpy numpy.ones except (ImportError, AttributeError): return 'skip' content = """<VRTDataset rasterXSize="10" rasterYSize="10"> <VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand"> <ColorInterp>Gray</ColorInterp> <PixelFunctionType>vrtderived.one_pix_func</PixelFunctionType> <PixelFunctionLanguage>Python</PixelFunctionLanguage> </VRTRasterBand> </VRTDataset> """ ds = gdal.Open(content) gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', "YES") cs = ds.GetRasterBand(1).Checksum() gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', None) if cs != 100: gdaltest.post_reason('invalid checksum') print(cs) print(gdal.GetLastErrorMsg()) return 'fail' # GDAL_VRT_TRUSTED_MODULES not defined ds = gdal.Open(content) with gdaltest.error_handler(): cs = ds.GetRasterBand(1).Checksum() if cs != 0: gdaltest.post_reason('invalid checksum') print(cs) print(gdal.GetLastErrorMsg()) return 'fail' # GDAL_VRT_PYTHON_TRUSTED_MODULES *NOT* matching our module for val in [ 'vrtderive', 'vrtderivedX', 'vrtderivedX*', 'vrtderive.*' 'vrtderivedX.*' ]: ds = gdal.Open(content) gdal.SetConfigOption('GDAL_VRT_PYTHON_TRUSTED_MODULES', val) with gdaltest.error_handler(): cs = ds.GetRasterBand(1).Checksum() gdal.SetConfigOption('GDAL_VRT_PYTHON_TRUSTED_MODULES', None) if cs != 0: gdaltest.post_reason('invalid checksum') print(cs) print(gdal.GetLastErrorMsg()) return 'fail' # GDAL_VRT_PYTHON_TRUSTED_MODULES matching our module for val in [ 'foo,vrtderived,bar', '*', 'foo,vrtderived*,bar', 'foo,vrtderived.*,bar', 'foo,vrtderi*,bar' ]: ds = gdal.Open(content) gdal.SetConfigOption('GDAL_VRT_PYTHON_TRUSTED_MODULES', val) cs = ds.GetRasterBand(1).Checksum() gdal.SetConfigOption('GDAL_VRT_PYTHON_TRUSTED_MODULES', None) if cs != 100: gdaltest.post_reason('invalid checksum') print(cs) print(gdal.GetLastErrorMsg()) return 'fail' return 'success'
def vrtderived_code_that_only_makes_sense_with_GDAL_VRT_ENABLE_PYTHON_equal_IF_SAFE_but_that_is_now_disabled( ): # untrusted import ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10"> <VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand"> <PixelFunctionType>my_func</PixelFunctionType> <PixelFunctionLanguage>Python</PixelFunctionLanguage> <PixelFunctionCode><![CDATA[ def my_func(in_ar, out_ar, xoff, yoff, xsize, ysize, raster_xsize, raster_ysize, r, gt, **kwargs): import foo ]]> </PixelFunctionCode> </VRTRasterBand> </VRTDataset> """) with gdaltest.error_handler(): cs = ds.GetRasterBand(1).Checksum() if cs != 0: gdaltest.post_reason('invalid checksum') print(cs) print(gdal.GetLastErrorMsg()) return 'fail' # untrusted function ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10"> <VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand"> <PixelFunctionType>my_func</PixelFunctionType> <PixelFunctionLanguage>Python</PixelFunctionLanguage> <PixelFunctionCode><![CDATA[ def my_func(in_ar, out_ar, xoff, yoff, xsize, ysize, raster_xsize, raster_ysize, r, gt, **kwargs): open('/etc/passwd').read() ]]> </PixelFunctionCode> </VRTRasterBand> </VRTDataset> """) with gdaltest.error_handler(): cs = ds.GetRasterBand(1).Checksum() if cs != 0: gdaltest.post_reason('invalid checksum') print(cs) print(gdal.GetLastErrorMsg()) return 'fail' # GDAL_VRT_ENABLE_PYTHON not set to YES ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10"> <VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand"> <PixelFunctionType>vrtderived.one_pix_func</PixelFunctionType> <PixelFunctionLanguage>Python</PixelFunctionLanguage> </VRTRasterBand> </VRTDataset> """) with gdaltest.error_handler(): cs = ds.GetRasterBand(1).Checksum() if cs != 0: gdaltest.post_reason('invalid checksum') print(cs) print(gdal.GetLastErrorMsg()) return 'fail' return 'success'
def vrtderived_9(): try: import numpy numpy.ones except (ImportError, AttributeError): return 'skip' # Missing PixelFunctionType with gdaltest.error_handler(): ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10"> <VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand"> <PixelFunctionLanguage>Python</PixelFunctionLanguage> </VRTRasterBand> </VRTDataset> """) if ds is not None: gdaltest.post_reason('fail') return 'fail' # Unsupported PixelFunctionLanguage with gdaltest.error_handler(): ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10"> <VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand"> <PixelFunctionType>identity</PixelFunctionType> <PixelFunctionLanguage>foo</PixelFunctionLanguage> </VRTRasterBand> </VRTDataset> """) if ds is not None: gdaltest.post_reason('fail') return 'fail' # PixelFunctionCode can only be used with Python with gdaltest.error_handler(): ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10"> <VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand"> <PixelFunctionType>identity</PixelFunctionType> <PixelFunctionCode><![CDATA[ def identity(in_ar, out_ar, xoff, yoff, xsize, ysize, raster_xsize, raster_ysize, r, gt, **kwargs): syntax_error ]]> </PixelFunctionCode> </VRTRasterBand> </VRTDataset> """) if ds is not None: gdaltest.post_reason('fail') return 'fail' # PixelFunctionArguments can only be used with Python with gdaltest.error_handler(): ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10"> <VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand"> <PixelFunctionType>identity</PixelFunctionType> <PixelFunctionArguments foo="bar"/> </VRTRasterBand> </VRTDataset> """) if ds is not None: gdaltest.post_reason('fail') return 'fail' # BufferRadius can only be used with Python with gdaltest.error_handler(): ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10"> <VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand"> <PixelFunctionType>identity</PixelFunctionType> <BufferRadius>1</BufferRadius> </VRTRasterBand> </VRTDataset> """) if ds is not None: gdaltest.post_reason('fail') return 'fail' # Invalid BufferRadius with gdaltest.error_handler(): ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10"> <VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand"> <PixelFunctionType>identity</PixelFunctionType> <PixelFunctionLanguage>Python</PixelFunctionLanguage> <BufferRadius>-1</BufferRadius> </VRTRasterBand> </VRTDataset> """) if ds is not None: gdaltest.post_reason('fail') return 'fail' # Error at Python code compilation (indentation error) ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10"> <VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand"> <PixelFunctionType>identity</PixelFunctionType> <PixelFunctionLanguage>Python</PixelFunctionLanguage> <PixelFunctionCode><![CDATA[ def identity(in_ar, out_ar, xoff, yoff, xsize, ysize, raster_xsize, raster_ysize, r, gt, **kwargs): syntax_error ]]> </PixelFunctionCode> </VRTRasterBand> </VRTDataset> """) gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', 'YES') with gdaltest.error_handler(): cs = ds.GetRasterBand(1).Checksum() gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', None) if cs != 0: gdaltest.post_reason('invalid checksum') print(cs) print(gdal.GetLastErrorMsg()) return 'fail' # Error at run time (in global code) ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10"> <VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand"> <PixelFunctionType>identity</PixelFunctionType> <PixelFunctionLanguage>Python</PixelFunctionLanguage> <PixelFunctionCode><![CDATA[ runtime_error def identity(in_ar, out_ar, xoff, yoff, xsize, ysize, raster_xsize, raster_ysize, r, gt, **kwargs): pass ]]> </PixelFunctionCode> </VRTRasterBand> </VRTDataset> """) gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', 'YES') with gdaltest.error_handler(): cs = ds.GetRasterBand(1).Checksum() gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', None) if cs != 0: gdaltest.post_reason('invalid checksum') print(cs) print(gdal.GetLastErrorMsg()) return 'fail' # Error at run time (in pixel function) ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10"> <VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand"> <PixelFunctionType>identity</PixelFunctionType> <PixelFunctionLanguage>Python</PixelFunctionLanguage> <PixelFunctionCode><![CDATA[ def identity(in_ar, out_ar, xoff, yoff, xsize, ysize, raster_xsize, raster_ysize, r, gt, **kwargs): runtime_error ]]> </PixelFunctionCode> </VRTRasterBand> </VRTDataset> """) gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', 'YES') with gdaltest.error_handler(): cs = ds.GetRasterBand(1).Checksum() gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', None) if cs != 0: gdaltest.post_reason('invalid checksum') print(cs) print(gdal.GetLastErrorMsg()) return 'fail' # User exception ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10"> <VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand"> <PixelFunctionType>identity</PixelFunctionType> <PixelFunctionLanguage>Python</PixelFunctionLanguage> <PixelFunctionCode><![CDATA[ def identity(in_ar, out_ar, xoff, yoff, xsize, ysize, raster_xsize, raster_ysize, r, gt, **kwargs): raise Exception('my exception') ]]> </PixelFunctionCode> </VRTRasterBand> </VRTDataset> """) gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', 'YES') with gdaltest.error_handler(): cs = ds.GetRasterBand(1).Checksum() gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', None) if cs != 0: gdaltest.post_reason('invalid checksum') print(cs) print(gdal.GetLastErrorMsg()) return 'fail' # unknown_function ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10"> <VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand"> <PixelFunctionType>unknown_function</PixelFunctionType> <PixelFunctionLanguage>Python</PixelFunctionLanguage> <PixelFunctionCode><![CDATA[ def identity(in_ar, out_ar, xoff, yoff, xsize, ysize, raster_xsize, raster_ysize, r, gt, **kwargs): pass ]]> </PixelFunctionCode> </VRTRasterBand> </VRTDataset> """) gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', 'YES') with gdaltest.error_handler(): cs = ds.GetRasterBand(1).Checksum() gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', None) if cs != 0: gdaltest.post_reason('invalid checksum') print(cs) print(gdal.GetLastErrorMsg()) return 'fail' # uncallable object ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10"> <VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand"> <PixelFunctionType>uncallable_object</PixelFunctionType> <PixelFunctionLanguage>Python</PixelFunctionLanguage> <PixelFunctionCode><![CDATA[ uncallable_object = True ]]> </PixelFunctionCode> </VRTRasterBand> </VRTDataset> """) gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', 'YES') with gdaltest.error_handler(): cs = ds.GetRasterBand(1).Checksum() gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', None) if cs != 0: gdaltest.post_reason('invalid checksum') print(cs) print(gdal.GetLastErrorMsg()) return 'fail' # unknown_module ds = gdal.Open("""<VRTDataset rasterXSize="10" rasterYSize="10"> <VRTRasterBand dataType="Byte" band="1" subClass="VRTDerivedRasterBand"> <PixelFunctionType>unknown_module.unknown_function</PixelFunctionType> <PixelFunctionLanguage>Python</PixelFunctionLanguage> </VRTRasterBand> </VRTDataset> """) with gdaltest.error_handler(): gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', "YES") cs = ds.GetRasterBand(1).Checksum() gdal.SetConfigOption('GDAL_VRT_ENABLE_PYTHON', None) if cs != 0: gdaltest.post_reason('invalid checksum') print(cs) print(gdal.GetLastErrorMsg()) return 'fail' return 'success'
def vrtderived_7(): import test_cli_utilities if test_cli_utilities.get_gdalinfo_path() is None: return 'skip' ret, err = gdaltest.runexternal_out_and_err( test_cli_utilities.get_gdalinfo_path() + ' -checksum data/n43_hillshade.vrt --config GDAL_VRT_ENABLE_PYTHON YES' ) if gdal.GetConfigOption('CPL_DEBUG') is not None: print(err) # Either we cannot find a Python library, either it works if ret.find('Checksum=0') >= 0: print('Did not manage to find a Python library') elif ret.find('Checksum=50577') < 0: gdaltest.post_reason('fail') print(ret) print(err) return 'fail' ret, err = gdaltest.runexternal_out_and_err( test_cli_utilities.get_gdalinfo_path() + ' -checksum data/n43_hillshade.vrt --config GDAL_VRT_ENABLE_PYTHON YES --config VRT_ENABLE_PYTHON_PATH NO' ) if gdal.GetConfigOption('CPL_DEBUG') is not None: print(err) # Either we cannot find a Python library, either it works if ret.find('Checksum=0') >= 0: print('Did not manage to find a Python library') elif ret.find('Checksum=50577') < 0: gdaltest.post_reason('fail') print(ret) print(err) return 'fail' ret, err = gdaltest.runexternal_out_and_err( test_cli_utilities.get_gdalinfo_path() + ' -checksum data/n43_hillshade.vrt --config GDAL_VRT_ENABLE_PYTHON YES --config VRT_ENABLE_PYTHON_SYMLINK NO' ) if gdal.GetConfigOption('CPL_DEBUG') is not None: print(err) # Either we cannot find a Python library, either it works if ret.find('Checksum=0') >= 0: print('Did not manage to find a Python library') elif ret.find('Checksum=50577') < 0: gdaltest.post_reason('fail') print(ret) print(err) return 'fail' # Invalid shared object name ret, err = gdaltest.runexternal_out_and_err( test_cli_utilities.get_gdalinfo_path() + ' -checksum data/n43_hillshade.vrt --config GDAL_VRT_ENABLE_PYTHON YES --config PYTHONSO foo' ) if gdal.GetConfigOption('CPL_DEBUG') is not None: print(err) if ret.find('Checksum=0') < 0: gdaltest.post_reason('fail') print(ret) print(err) return 'fail' # Valid shared object name, but without Python symbols libgdal_so = gdaltest.find_lib('gdal') if libgdal_so is not None: ret, err = gdaltest.runexternal_out_and_err( test_cli_utilities.get_gdalinfo_path() + ' -checksum data/n43_hillshade.vrt --config GDAL_VRT_ENABLE_PYTHON YES --config PYTHONSO "%s"' % libgdal_so) if gdal.GetConfigOption('CPL_DEBUG') is not None: print(err) if ret.find('Checksum=0') < 0: gdaltest.post_reason('fail') print(ret) print(err) return 'fail' return 'success'
def test_gdalbuildvrt_7(): if test_cli_utilities.get_gdalbuildvrt_path() is None: return 'skip' out_ds = gdal.GetDriverByName('GTiff').Create('tmp/vrtnull1.tif', 20, 10, 3, gdal.GDT_UInt16) out_ds.SetGeoTransform([2, 0.1, 0, 49, 0, -0.1]) srs = osr.SpatialReference() srs.SetFromUserInput('EPSG:4326') out_ds.SetProjection(srs.ExportToWkt()) out_ds.GetRasterBand(1).SetRasterColorInterpretation(gdal.GCI_RedBand) out_ds.GetRasterBand(2).SetRasterColorInterpretation(gdal.GCI_GreenBand) out_ds.GetRasterBand(3).SetRasterColorInterpretation(gdal.GCI_BlueBand) out_ds.GetRasterBand(1).SetNoDataValue(256) try: ff = '\xff'.encode('latin1') except UnicodeDecodeError: ff = '\xff' out_ds.GetRasterBand(1).WriteRaster(0, 0, 10, 10, ff, buf_type=gdal.GDT_Byte, buf_xsize=1, buf_ysize=1) out_ds.GetRasterBand(2).WriteRaster(0, 0, 10, 10, '\x00', buf_type=gdal.GDT_Byte, buf_xsize=1, buf_ysize=1) out_ds.GetRasterBand(3).WriteRaster(0, 0, 10, 10, '\x00', buf_type=gdal.GDT_Byte, buf_xsize=1, buf_ysize=1) out_ds = None out_ds = gdal.GetDriverByName('GTiff').Create('tmp/vrtnull2.tif', 20, 10, 3, gdal.GDT_UInt16) out_ds.SetGeoTransform([2, 0.1, 0, 49, 0, -0.1]) srs = osr.SpatialReference() srs.SetFromUserInput('EPSG:4326') out_ds.SetProjection(srs.ExportToWkt()) out_ds.GetRasterBand(1).SetRasterColorInterpretation(gdal.GCI_RedBand) out_ds.GetRasterBand(2).SetRasterColorInterpretation(gdal.GCI_GreenBand) out_ds.GetRasterBand(3).SetRasterColorInterpretation(gdal.GCI_BlueBand) out_ds.GetRasterBand(1).SetNoDataValue(256) out_ds.GetRasterBand(1).WriteRaster(10, 0, 10, 10, '\x00', buf_type=gdal.GDT_Byte, buf_xsize=1, buf_ysize=1) out_ds.GetRasterBand(2).WriteRaster(10, 0, 10, 10, ff, buf_type=gdal.GDT_Byte, buf_xsize=1, buf_ysize=1) out_ds.GetRasterBand(3).WriteRaster(10, 0, 10, 10, '\x00', buf_type=gdal.GDT_Byte, buf_xsize=1, buf_ysize=1) out_ds = None gdaltest.runexternal( test_cli_utilities.get_gdalbuildvrt_path() + ' tmp/gdalbuildvrt7.vrt tmp/vrtnull1.tif tmp/vrtnull2.tif') ds = gdal.Open('tmp/gdalbuildvrt7.vrt') if ds.GetRasterBand(1).Checksum() != 1217: gdaltest.post_reason('Wrong checksum') return 'fail' if ds.GetRasterBand(2).Checksum() != 1218: gdaltest.post_reason('Wrong checksum') return 'fail' if ds.GetRasterBand(3).Checksum() != 0: gdaltest.post_reason('Wrong checksum') return 'fail' ds = None return 'success'
def test_gdalwarp_lib_128(): mem_ds = gdal.GetDriverByName('MEM').Create('', 1177, 4719) rpc = [ "HEIGHT_OFF=109", "LINE_NUM_COEFF=-0.001245683 -0.09427649 -1.006342 -1.954469e-05 0.001033926 2.020534e-08 -3.845472e-07 -0.002075817 0.0005520694 0 -4.642442e-06 -3.271793e-06 2.705977e-05 -7.634384e-07 -2.132832e-05 -3.248862e-05 -8.17894e-06 -3.678094e-07 2.002032e-06 3.693162e-08", "LONG_OFF=7.1477", "SAMP_DEN_COEFF=1 0.01415176 -0.003715018 -0.001205632 -0.0007738299 4.057763e-05 -1.649126e-05 0.0001453584 0.0001628194 -7.354731e-05 4.821444e-07 -4.927701e-06 -1.942371e-05 -2.817499e-06 1.946396e-06 3.04243e-06 2.362282e-07 -2.5371e-07 -1.36993e-07 1.132432e-07", "LINE_SCALE=2360", "SAMP_NUM_COEFF=0.04337163 1.775948 -0.87108 0.007425391 0.01783631 0.0004057179 -0.000184695 -0.04257537 -0.01127869 -1.531228e-06 1.017961e-05 0.000572344 -0.0002941 -0.0001301705 -0.0003289546 5.394918e-05 6.388447e-05 -4.038289e-06 -7.525785e-06 -5.431241e-07", "LONG_SCALE=0.8383", "SAMP_SCALE=593", "SAMP_OFF=589", "LAT_SCALE=1.4127", "LAT_OFF=33.8992", "LINE_OFF=2359", "LINE_DEN_COEFF=1 0.0007273139 -0.0006006867 -4.272095e-07 2.578717e-05 4.718479e-06 -2.116976e-06 -1.347805e-05 -2.209958e-05 8.131258e-06 -7.290143e-08 5.105109e-08 -7.353388e-07 0 2.131142e-06 9.697701e-08 1.237039e-08 7.153246e-08 6.758015e-08 5.811124e-08", "HEIGHT_SCALE=96.3" ] mem_ds.SetMetadata(rpc, "RPC") mem_ds.GetRasterBand(1).Fill(255) cutlineDSName = '/vsimem/test_gdalwarp_lib_128.json' cutline_ds = ogr.GetDriverByName('GeoJSON').CreateDataSource(cutlineDSName) cutline_lyr = cutline_ds.CreateLayer('cutline') f = ogr.Feature(cutline_lyr.GetLayerDefn()) f.SetGeometry( ogr.CreateGeometryFromWkt( 'POLYGON ((7.2151 32.51930,7.214316 32.58116,7.216043 32.59476,7.21666 32.5193,7.2151 32.51930))' )) cutline_lyr.CreateFeature(f) f = None cutline_lyr = None cutline_ds = None # Default is GDALWARP_DENSIFY_CUTLINE=YES ds = gdal.Warp( '', mem_ds, format='MEM', cutlineDSName=cutlineDSName, dstSRS='EPSG:4326', outputBounds=[7.2, 32.52, 7.217, 32.59], xRes=0.000226555, yRes=0.000226555, transformerOptions=['RPC_DEM=data/test_gdalwarp_lib_128_dem.tif']) cs = ds.GetRasterBand(1).Checksum() if cs != 4351: gdaltest.post_reason('bad checksum') print(cs) return 'fail' # Below steps depend on GEOS if not ogrtest.have_geos(): gdal.Unlink(cutlineDSName) return 'success' gdal.SetConfigOption('GDALWARP_DENSIFY_CUTLINE', 'ONLY_IF_INVALID') ds = gdal.Warp( '', mem_ds, format='MEM', cutlineDSName=cutlineDSName, dstSRS='EPSG:4326', outputBounds=[7.2, 32.52, 7.217, 32.59], xRes=0.000226555, yRes=0.000226555, transformerOptions=['RPC_DEM=data/test_gdalwarp_lib_128_dem.tif']) gdal.SetConfigOption('GDALWARP_DENSIFY_CUTLINE', None) cs = ds.GetRasterBand(1).Checksum() if cs != 4351: gdaltest.post_reason('bad checksum') print(cs) return 'fail' gdal.SetConfigOption('GDALWARP_DENSIFY_CUTLINE', 'NO') with gdaltest.error_handler(): ds = gdal.Warp( '', mem_ds, format='MEM', cutlineDSName=cutlineDSName, dstSRS='EPSG:4326', outputBounds=[7.2, 32.52, 7.217, 32.59], xRes=0.000226555, yRes=0.000226555, transformerOptions=['RPC_DEM=data/test_gdalwarp_lib_128_dem.tif']) gdal.SetConfigOption('GDALWARP_DENSIFY_CUTLINE', None) if ds is not None: gdaltest.post_reason('expected none return') return 'fail' gdal.Unlink(cutlineDSName) return 'success'
def test_gdalbuildvrt_10(): if test_cli_utilities.get_gdalbuildvrt_path() is None: return 'skip' out_ds = gdal.GetDriverByName('GTiff').Create( 'tmp/test_gdalbuildvrt_10_1.tif', 10, 10, 1, gdal.GDT_Byte, options=['NBITS=1', 'PHOTOMETRIC=MINISWHITE']) out_ds.SetGeoTransform([2, 0.1, 0, 49, 0, -0.1]) srs = osr.SpatialReference() srs.SetFromUserInput('EPSG:4326') out_ds.SetProjection(srs.ExportToWkt()) out_ds.GetRasterBand(1).WriteRaster(1, 1, 3, 3, '\x01', buf_type=gdal.GDT_Byte, buf_xsize=1, buf_ysize=1) out_ds = None out_ds = gdal.GetDriverByName('GTiff').Create( 'tmp/test_gdalbuildvrt_10_2.tif', 10, 10, 1, gdal.GDT_Byte, options=['NBITS=1', 'PHOTOMETRIC=MINISWHITE']) out_ds.SetGeoTransform([2, 0.1, 0, 49, 0, -0.1]) srs = osr.SpatialReference() srs.SetFromUserInput('EPSG:4326') out_ds.SetProjection(srs.ExportToWkt()) out_ds.GetRasterBand(1).WriteRaster(6, 6, 3, 3, '\x01', buf_type=gdal.GDT_Byte, buf_xsize=1, buf_ysize=1) out_ds = None gdaltest.runexternal( test_cli_utilities.get_gdalbuildvrt_path() + ' -srcnodata 0 tmp/gdalbuildvrt10.vrt tmp/test_gdalbuildvrt_10_1.tif tmp/test_gdalbuildvrt_10_2.tif' ) ds = gdal.Open('tmp/gdalbuildvrt10.vrt') if ds.GetRasterBand(1).Checksum() != 18: print(ds.GetRasterBand(1).Checksum()) gdaltest.post_reason('Wrong checksum') return 'fail' ds = None return 'success'
def warp_30(): # Open source dataset src_ds = gdal.Open('../gcore/data/byte.tif') # Desfine target SRS dst_srs = osr.SpatialReference() dst_srs.ImportFromEPSG(4326) dst_wkt = dst_srs.ExportToWkt() error_threshold = 0.125 # error threshold --> use same value as in gdalwarp resampling = gdal.GRA_Bilinear # Call AutoCreateWarpedVRT() to fetch default values for target raster dimensions and geotransform tmp_ds = gdal.AutoCreateWarpedVRT( src_ds, \ None, # src_wkt : left to default value --> will use the one from source \ dst_wkt, \ resampling, \ error_threshold ) dst_xsize = tmp_ds.RasterXSize dst_ysize = tmp_ds.RasterYSize dst_gt = tmp_ds.GetGeoTransform() tmp_ds = None # Now create the true target dataset dst_ds = gdal.GetDriverByName('GTiff').Create('/vsimem/warp_30.tif', dst_xsize, dst_ysize, src_ds.RasterCount) dst_ds.SetProjection(dst_wkt) dst_ds.SetGeoTransform(dst_gt) # And run the reprojection cbk = warp_30_progress_callback cbk_user_data = None # value for last parameter of above warp_27_progress_callback gdal.PushErrorHandler('CPLQuietErrorHandler') ret = gdal.ReprojectImage( src_ds, \ dst_ds, \ None, # src_wkt : left to default value --> will use the one from source \ None, # dst_wkt : left to default value --> will use the one from destination \ resampling, \ 0, # WarpMemoryLimit : left to default value \ error_threshold, cbk, # Progress callback : could be left to None or unspecified for silent progress cbk_user_data) # Progress callback user data gdal.PopErrorHandler() if ret == 0: gdaltest.post_reason('failed') return 'fail' old_val = gdal.GetConfigOption('WARP_NUM_THREADS') gdal.SetConfigOption('WARP_NUM_THREADS', '2') gdal.PushErrorHandler('CPLQuietErrorHandler') ret = gdal.ReprojectImage( src_ds, \ dst_ds, \ None, # src_wkt : left to default value --> will use the one from source \ None, # dst_wkt : left to default value --> will use the one from destination \ resampling, \ 0, # WarpMemoryLimit : left to default value \ error_threshold, cbk, # Progress callback : could be left to None or unspecified for silent progress cbk_user_data) # Progress callback user data gdal.PopErrorHandler() gdal.SetConfigOption('WARP_NUM_THREADS', old_val) if ret == 0: gdaltest.post_reason('failed') return 'fail' return 'success'
def test_envisat_meris_3(self): # test Flag bands if not self.download_file(): return 'skip' ds = gdal.Open(os.path.join('tmp', 'cache', self.fileName)) if ds is None: return 'fail' flags_band = None detector_index_band = None for bandno in range(1, ds.RasterCount + 1): band = ds.GetRasterBand(bandno) name = band.GetDescription() if 'Flags' in name: flags_band = bandno if name.startswith('Detector index'): detector_index_band = bandno product = ds.GetMetadataItem('MPH_PRODUCT') level = product[8] if level == '1': if not flags_band: gdaltest.post_reason('No flag band in MERIS Level 1 product.') return 'fail' band = ds.GetRasterBand(flags_band) if band.DataType != gdal.GDT_Byte: gdaltest.post_reason('Incorrect data type of the flag band in ' 'MERIS Level 1 product.') return 'fail' if not detector_index_band: gdaltest.post_reason('No "detector index" band in MERIS ' 'Level 1 product.') return 'fail' band = ds.GetRasterBand(detector_index_band) if band.DataType != gdal.GDT_Int16: gdaltest.post_reason('Incorrect data type of the ' '"detector index" band in MERIS Level 2 ' 'product.') return 'fail' elif level == '2': if not flags_band: gdaltest.post_reason('No flag band in MERIS Level 2 product.') return 'fail' band = ds.GetRasterBand(flags_band) if band.DataType != gdal.GDT_UInt32: gdaltest.post_reason('Incorrect data type of the flag band in ' 'MERIS Level 2 product.') return 'fail' else: gdaltest.post_reason('Invalid product level: %s.' % level) return 'fail' return 'success'
def ogr_idrisi_1(): ds = ogr.Open('data/points.vct') if ds is None: gdaltest.post_reason('fail') return 'fail' lyr = ds.GetLayer(0) if lyr.GetGeomType() != ogr.wkbPoint: gdaltest.post_reason('fail') return 'fail' if lyr.GetLayerDefn().GetFieldDefn(1).GetName() != 'IntegerField': gdaltest.post_reason('fail') return 'fail' if lyr.GetLayerDefn().GetFieldDefn(1).GetType() != ogr.OFTInteger: gdaltest.post_reason('fail') return 'fail' sr = lyr.GetSpatialRef() if sr.ExportToWkt().find('PROJCS["UTM Zone 31, Northern Hemisphere"') != 0: gdaltest.post_reason('fail') print(sr.ExportToWkt()) return 'fail' if lyr.GetFeatureCount() != 2: gdaltest.post_reason('fail') return 'fail' if lyr.TestCapability(ogr.OLCFastFeatureCount) != 1: gdaltest.post_reason('fail') return 'fail' if lyr.TestCapability(ogr.OLCFastGetExtent) != 1: gdaltest.post_reason('fail') return 'fail' if lyr.GetExtent() != (400000.0, 600000.0, 4000000.0, 5000000.0): gdaltest.post_reason('fail') print(lyr.GetExtent()) return 'fail' feat = lyr.GetNextFeature() if feat.GetFieldAsDouble(0) != 1.0: gdaltest.post_reason('fail') feat.DumpReadable() return 'fail' if feat.GetFieldAsInteger(1) != 2: gdaltest.post_reason('fail') feat.DumpReadable() return 'fail' if feat.GetFieldAsDouble(2) != 3.45: gdaltest.post_reason('fail') feat.DumpReadable() return 'fail' if feat.GetFieldAsString(3) != 'foo': gdaltest.post_reason('fail') feat.DumpReadable() return 'fail' if ogrtest.check_feature_geometry( feat, ogr.CreateGeometryFromWkt('POINT(400000 5000000)')) != 0: gdaltest.post_reason('fail') feat.DumpReadable() return 'fail' feat = lyr.GetNextFeature() if feat.GetFieldAsDouble(0) != 2.0: gdaltest.post_reason('fail') feat.DumpReadable() return 'fail' if ogrtest.check_feature_geometry( feat, ogr.CreateGeometryFromWkt('POINT (600000 4000000)')) != 0: gdaltest.post_reason('fail') feat.DumpReadable() return 'fail' lyr.SetSpatialFilterRect(600000 - 1, 4000000 - 1, 600000 + 1, 4000000 + 1) lyr.ResetReading() feat = lyr.GetNextFeature() if feat.GetFieldAsDouble(0) != 2.0: gdaltest.post_reason('fail') feat.DumpReadable() return 'fail' lyr.SetSpatialFilterRect(0, 0, 1, 1) lyr.ResetReading() feat = lyr.GetNextFeature() if feat is not None: gdaltest.post_reason('fail') return 'fail' ds = None return 'success'
def ogr_xlsx_check(ds): if ds.TestCapability("foo") != 0: gdaltest.post_reason('fail') return 'fail' if ds.GetLayerCount() != 8: gdaltest.post_reason('bad layer count') return 'fail' lyr = ds.GetLayer(0) if lyr.GetName() != 'Feuille1': gdaltest.post_reason('bad layer name') return 'fail' if lyr.GetGeomType() != ogr.wkbNone: gdaltest.post_reason('bad layer geometry type') return 'fail' if lyr.GetSpatialRef() != None: gdaltest.post_reason('bad spatial ref') return 'fail' if lyr.GetFeatureCount() != 26: gdaltest.post_reason('fail') print(lyr.GetFeatureCount()) return 'fail' if lyr.TestCapability("foo") != 0: gdaltest.post_reason('fail') return 'fail' lyr = ds.GetLayer(6) if lyr.GetName() != 'Feuille7': gdaltest.post_reason('bad layer name') return 'fail' if lyr.GetLayerDefn().GetFieldCount() != 12: gdaltest.post_reason('fail') print(lyr.GetLayerDefn().GetFieldCount()) return 'fail' type_array = [ ogr.OFTString, ogr.OFTInteger, ogr.OFTReal, ogr.OFTReal, ogr.OFTDate, ogr.OFTDateTime, ogr.OFTReal, ogr.OFTTime, ogr.OFTReal, ogr.OFTInteger, ogr.OFTReal, ogr.OFTDateTime ] for i in range(len(type_array)): if lyr.GetLayerDefn().GetFieldDefn(i).GetType() != type_array[i]: gdaltest.post_reason('fail') print(i) return 'fail' feat = lyr.GetNextFeature() if feat.GetFieldAsString(0) != 'val' or \ feat.GetFieldAsInteger(1) != 23 or \ feat.GetFieldAsDouble(2) != 3.45 or \ feat.GetFieldAsDouble(3) != 0.52 or \ feat.GetFieldAsString(4) != '2012/01/22' or \ feat.GetFieldAsString(5) != '2012/01/22 18:49:00': gdaltest.post_reason('fail') feat.DumpReadable() return 'fail' feat = lyr.GetNextFeature() if feat.IsFieldSet(2): gdaltest.post_reason('fail') feat.DumpReadable() return 'fail' return 'success'
def test_gdal_grid_12(): if gdal_grid is None: return 'skip' ################# # Test generic implementation (no AVX, no SSE) outfiles.append('tmp/grid_invdistnn_generic.tif') try: os.remove(outfiles[-1]) except: pass # Create a GDAL dataset from the values of "grid.csv". (out, err) = gdaltest.runexternal_out_and_err(gdal_grid + ' -txe 440721.0 441920.0 -tye 3751321.0 3750120.0 -outsize 20 20 -ot Float64 -l grid -a invdistnn:power=2.0:radius=1.0:max_points=12:min_points=0:nodata=0.0 data/grid.vrt ' + outfiles[-1]) # We should get the same values as in "ref_data/gdal_invdistnn.tif" ds = gdal.Open(outfiles[-1]) ds_ref = gdal.Open('ref_data/grid_invdistnn.tif') maxdiff = gdaltest.compare_ds(ds, ds_ref, verbose = 0) if maxdiff > 0.00001: gdaltest.compare_ds(ds, ds_ref, verbose = 1) gdaltest.post_reason('Image too different from the reference') return 'fail' ds_ref = None ds = None ################# outfiles.append('tmp/grid_invdistnn_250_8minp.tif') try: os.remove(outfiles[-1]) except: pass # Create a GDAL dataset from the values of "grid.csv". # Circular window, shifted, test min points and NODATA setting. gdaltest.runexternal(gdal_grid + ' -txe 440721.0 441920.0 -tye 3751321.0 3750120.0 -outsize 20 20 -ot Float64 -l grid -a invdistnn:power=2.0:radius=250.0:max_points=12:min_points=8:nodata=0.0 data/grid.vrt ' + outfiles[-1]) # We should get the same values as in "ref_data/grid_invdistnn_250_8minp.tif" ds = gdal.Open(outfiles[-1]) ds_ref = gdal.Open('ref_data/grid_invdistnn_250_8minp.tif') maxdiff = gdaltest.compare_ds(ds, ds_ref, verbose = 0) if maxdiff > 0.00001: gdaltest.compare_ds(ds, ds_ref, verbose = 1) gdaltest.post_reason('Image too different from the reference') return 'fail' ds_ref = None ds = None ################# # Test generic implementation with max_points and radius specified outfiles.append('tmp/grid_invdistnn_250_10maxp_3pow.tif') try: os.remove(outfiles[-1]) except: pass # Create a GDAL dataset from the values of "grid.csv". gdaltest.runexternal(gdal_grid + ' -txe 440721.0 441920.0 -tye 3751321.0 3750120.0 -outsize 20 20 -ot Float64 -l grid -a invdistnn:power=3.0:radius=250.0:max_points=10:min_points=0:nodata=0.0 data/grid.vrt ' + outfiles[-1]) # We should get the same values as in "ref_data/gdal_invdistnn_250_10maxp_3pow.tif" ds = gdal.Open(outfiles[-1]) ds_ref = gdal.Open('ref_data/grid_invdistnn_250_10maxp_3pow.tif') maxdiff = gdaltest.compare_ds(ds, ds_ref, verbose = 0) if maxdiff > 0.00001: gdaltest.compare_ds(ds, ds_ref, verbose = 1) gdaltest.post_reason('Image too different from the reference') return 'fail' ds_ref = None ds = None return 'success'
def warp_23(): if test_cli_utilities.get_gdalwarp_path() is None: return 'skip' gcp1 = gdal.GCP() gcp1.GCPPixel = 3213 gcp1.GCPLine = 2225 gcp1.GCPX = -88.834495 gcp1.GCPY = 29.979959 gcp2 = gdal.GCP() gcp2.GCPPixel = 2804 gcp2.GCPLine = 2236 gcp2.GCPX = -88.836706 gcp2.GCPY = 29.979516 gcp3 = gdal.GCP() gcp3.GCPPixel = 3157 gcp3.GCPLine = 4344 gcp3.GCPX = -88.833389 gcp3.GCPY = 29.969519 gcp4 = gdal.GCP() gcp4.GCPPixel = 3768 gcp4.GCPLine = 5247 gcp4.GCPX = -88.830168 gcp4.GCPY = 29.964958 gcp5 = gdal.GCP() gcp5.GCPPixel = 2697 gcp5.GCPLine = 9225 gcp5.GCPX = -88.83516 gcp5.GCPY = 29.945386 gcp6 = gdal.GCP() gcp6.GCPPixel = 4087 gcp6.GCPLine = 12360 gcp6.GCPX = -88.827899 gcp6.GCPY = 29.929807 gcp7 = gdal.GCP() gcp7.GCPPixel = 4629 gcp7.GCPLine = 11258 gcp7.GCPX = -88.825102 gcp7.GCPY = 29.93527 gcp8 = gdal.GCP() gcp8.GCPPixel = 4480 gcp8.GCPLine = 7602 gcp8.GCPX = -88.826733 gcp8.GCPY = 29.95304 gcps = [gcp1, gcp2, gcp3, gcp4, gcp5, gcp6, gcp7, gcp8] sr = osr.SpatialReference() sr.ImportFromEPSG(4326) ds = gdal.GetDriverByName('GTiff').Create('tmp/test3582.tif', 70, 170, 4, options=['SPARSE_OK=YES']) for i in range(len(gcps)): gcps[i].GCPPixel = gcps[i].GCPPixel / 10 gcps[i].GCPLine = gcps[i].GCPLine / 10 ds.SetGCPs(gcps, sr.ExportToWkt()) ds = None gdaltest.runexternal(test_cli_utilities.get_gdalwarp_path() + ' tmp/test3582.tif tmp/test3582_warped.tif') ds = gdal.Open('tmp/test3582_warped.tif') ret = 'success' if ds is None: gdaltest.post_reason('could not open output dataset') ret = 'fail' ds = None os.remove('tmp/test3582.tif') try: os.remove('tmp/test3582_warped.tif') except: pass return ret
def test_gdal_grid_4(): if gdal_grid is None: return 'skip' ################# outfiles.append('tmp/grid_average.tif') try: os.remove(outfiles[-1]) except: pass # Create a GDAL dataset from the values of "grid.csv". # We are using all the points from input dataset to average, so # the result is a raster filled with the same value in each node. gdaltest.runexternal(gdal_grid + ' -txe 440720.0 441920.0 -tye 3751320.0 3750120.0 -outsize 20 20 -ot Float64 -l grid -a average:radius1=0.0:radius2=0.0:angle=0.0:min_points=0:nodata=0.0 data/grid.vrt ' + outfiles[-1]) # We should get the same values as in "ref_data/grid_average.tif" ds = gdal.Open(outfiles[-1]) ds_ref = gdal.Open('ref_data/grid_average.tif') maxdiff = gdaltest.compare_ds(ds, ds_ref, verbose = 0) ds_ref = None if maxdiff > 1: gdaltest.compare_ds(ds, ds_ref, verbose = 1) gdaltest.post_reason('Image too different from the reference') return 'fail' ds = None ################# outfiles.append('tmp/grid_average_190_190.tif') try: os.remove(outfiles[-1]) except: pass # Create a GDAL dataset from the values of "grid.csv". # This time using a circular window. gdaltest.runexternal(gdal_grid + ' -txe 440720.0 441920.0 -tye 3751320.0 3750120.0 -outsize 20 20 -ot Float64 -l grid -a average:radius1=190.0:radius2=190.0:angle=0.0:min_points=0:nodata=0.0 data/grid.vrt ' + outfiles[-1]) # We should get the same values as in "ref_data/grid_average_190_190.tif" ds = gdal.Open(outfiles[-1]) ds_ref = gdal.Open('ref_data/grid_average_190_190.tif') maxdiff = gdaltest.compare_ds(ds, ds_ref, verbose = 0) ds_ref = None if maxdiff > 1: gdaltest.compare_ds(ds, ds_ref, verbose = 1) gdaltest.post_reason('Image too different from the reference') return 'fail' ds = None ################# outfiles.append('tmp/grid_average_300_100_40.tif') try: os.remove(outfiles[-1]) except: pass # Create a GDAL dataset from the values of "grid.csv". # Elliptical window, rotated. gdaltest.runexternal(gdal_grid + ' -txe 440720.0 441920.0 -tye 3751320.0 3750120.0 -outsize 20 20 -ot Float64 -l grid -a average:radius1=300.0:radius2=100.0:angle=40.0:min_points=0:nodata=0.0 data/grid.vrt ' + outfiles[-1]) # We should get the same values as in "ref_data/grid_average_300_100_40.tif" ds = gdal.Open(outfiles[-1]) ds_ref = gdal.Open('ref_data/grid_average_300_100_40.tif') maxdiff = gdaltest.compare_ds(ds, ds_ref, verbose = 0) ds_ref = None if maxdiff > 1: gdaltest.compare_ds(ds, ds_ref, verbose = 1) gdaltest.post_reason('Image too different from the reference') return 'fail' ds = None ################# outfiles.append('tmp/grid_average_90_90_8p.tif') try: os.remove(outfiles[-1]) except: pass # Create a GDAL dataset from the values of "grid.csv". # Circular window, shifted, test min points and NODATA setting. gdaltest.runexternal(gdal_grid + ' -txe 440721.0 441920.0 -tye 3751321.0 3750120.0 -outsize 20 20 -ot Float64 -l grid -a average:radius1=90.0:radius2=90.0:angle=0.0:min_points=8:nodata=0.0 data/grid.vrt ' + outfiles[-1]) # We should get the same values as in "ref_data/grid_average_90_90_8p.tif" ds = gdal.Open(outfiles[-1]) ds_ref = gdal.Open('ref_data/grid_average_90_90_8p.tif') maxdiff = gdaltest.compare_ds(ds, ds_ref, verbose = 0) ds_ref = None if maxdiff > 1: gdaltest.compare_ds(ds, ds_ref, verbose = 1) gdaltest.post_reason('Image too different from the reference') return 'fail' ds = None return 'success'
def ogr_idrisi_3(): ds = ogr.Open('data/polygons.vct') if ds is None: gdaltest.post_reason('fail') return 'fail' lyr = ds.GetLayer(0) if lyr.GetGeomType() != ogr.wkbPolygon: gdaltest.post_reason('fail') return 'fail' if lyr.GetFeatureCount() != 2: gdaltest.post_reason('fail') return 'fail' if lyr.TestCapability(ogr.OLCFastFeatureCount) != 1: gdaltest.post_reason('fail') return 'fail' if lyr.TestCapability(ogr.OLCFastGetExtent) != 1: gdaltest.post_reason('fail') return 'fail' if lyr.GetExtent() != (400000.0, 600000.0, 4000000.0, 5000000.0): gdaltest.post_reason('fail') print(lyr.GetExtent()) return 'fail' feat = lyr.GetNextFeature() if feat.GetFieldAsDouble(0) != 1.0: gdaltest.post_reason('fail') feat.DumpReadable() return 'fail' if ogrtest.check_feature_geometry( feat, ogr.CreateGeometryFromWkt( 'POLYGON ((400000 4000000,400000 5000000,600000 5000000,600000 4000000,400000 4000000),(450000 4250000,450000 4750000,550000 4750000,550000 4250000,450000 4250000))' )) != 0: gdaltest.post_reason('fail') feat.DumpReadable() return 'fail' feat = lyr.GetNextFeature() if feat.GetFieldAsDouble(0) != 2.0: gdaltest.post_reason('fail') feat.DumpReadable() return 'fail' if ogrtest.check_feature_geometry( feat, ogr.CreateGeometryFromWkt( 'POLYGON ((400000 4000000,400000 5000000,600000 5000000,600000 4000000,400000 4000000))' )) != 0: gdaltest.post_reason('fail') feat.DumpReadable() return 'fail' lyr.SetSpatialFilterRect(0, 0, 1, 1) lyr.ResetReading() feat = lyr.GetNextFeature() if feat is not None: gdaltest.post_reason('fail') return 'fail' ds = None return 'success'
def test_gdal_grid_2(): if gdal_grid is None: return 'skip' # Open reference dataset ds_ref = gdal.Open('../gcore/data/byte.tif') checksum_ref = ds_ref.GetRasterBand(1).Checksum() ds_ref = None ################# outfiles.append('tmp/grid_near.tif') try: os.remove(outfiles[-1]) except: pass # Create a GDAL dataset from the values of "grid.csv". # Grid nodes are located exactly in raster nodes. gdaltest.runexternal(gdal_grid + ' -txe 440720.0 441920.0 -tye 3751320.0 3750120.0 -outsize 20 20 -ot Byte -l grid -a nearest:radius1=0.0:radius2=0.0:angle=0.0:nodata=0.0 data/grid.vrt ' + outfiles[-1]) # We should get the same values as in "gcore/data/byte.tif" ds = gdal.Open(outfiles[-1]) if ds.GetRasterBand(1).Checksum() != checksum_ref: gdaltest.post_reason('bad checksum') print('bad checksum : got %d, expected %d' % \ (ds.GetRasterBand(1).Checksum(), checksum_ref)) return 'fail' if ds.GetRasterBand(1).GetNoDataValue() != 0.0: print('expected a nodata value') return 'fail' ds = None ################# outfiles.append('tmp/grid_near_shift.tif') try: os.remove(outfiles[-1]) except: pass # Now the same, but shift grid nodes a bit in both horizontal and vertical # directions. gdaltest.runexternal(gdal_grid + ' -txe 440721.0 441920.0 -tye 3751321.0 3750120.0 -outsize 20 20 -ot Byte -l grid -a nearest:radius1=0.0:radius2=0.0:angle=0.0:nodata=0.0 data/grid.vrt ' + outfiles[-1]) # We should get the same values as in "gcore/data/byte.tif" ds = gdal.Open(outfiles[-1]) if ds.GetRasterBand(1).Checksum() != checksum_ref: gdaltest.post_reason('bad checksum') print('bad checksum : got %d, expected %d' % \ (ds.GetRasterBand(1).Checksum(), checksum_ref)) return 'fail' ds = None ################# outfiles.append('tmp/grid_near_search3.tif') try: os.remove(outfiles[-1]) except: pass # Now try the search ellipse larger than the raster cell. gdaltest.runexternal(gdal_grid + ' -txe 440720.0 441920.0 -tye 3751320.0 3750120.0 -outsize 20 20 -ot Byte -l grid -a nearest:radius1=180.0:radius2=180.0:angle=0.0:nodata=0.0 data/grid.vrt ' + outfiles[-1]) # We should get the same values as in "gcore/data/byte.tif" ds = gdal.Open(outfiles[-1]) if ds.GetRasterBand(1).Checksum() != checksum_ref: gdaltest.post_reason('bad checksum') print('bad checksum : got %d, expected %d' % \ (ds.GetRasterBand(1).Checksum(), checksum_ref)) return 'fail' ds = None ################# outfiles.append('tmp/grid_near_search1.tif') try: os.remove(outfiles[-1]) except: pass # Search ellipse smaller than the raster cell. gdaltest.runexternal(gdal_grid + ' -txe 440720.0 441920.0 -tye 3751320.0 3750120.0 -outsize 20 20 -ot Byte -l grid -a nearest:radius1=20.0:radius2=20.0:angle=0.0:nodata=0.0 data/grid.vrt ' + outfiles[-1]) # We should get the same values as in "gcore/data/byte.tif" ds = gdal.Open(outfiles[-1]) if ds.GetRasterBand(1).Checksum() != checksum_ref: gdaltest.post_reason('bad checksum') print('bad checksum : got %d, expected %d' % \ (ds.GetRasterBand(1).Checksum(), checksum_ref)) return 'fail' ds = None ################# outfiles.append('tmp/grid_near_shift_search3.tif') try: os.remove(outfiles[-1]) except: pass # Large search ellipse and the grid shift. gdaltest.runexternal(gdal_grid + ' -txe 440721.0 441920.0 -tye 3751321.0 3750120.0 -outsize 20 20 -ot Byte -l grid -a nearest:radius1=180.0:radius2=180.0:angle=0.0:nodata=0.0 data/grid.vrt ' + outfiles[-1]) # We should get the same values as in "gcore/data/byte.tif" ds = gdal.Open(outfiles[-1]) if ds.GetRasterBand(1).Checksum() != checksum_ref: gdaltest.post_reason('bad checksum') print('bad checksum : got %d, expected %d' % \ (ds.GetRasterBand(1).Checksum(), checksum_ref)) return 'fail' ds = None ################# outfiles.append('tmp/grid_near_shift_search1.tif') try: os.remove(outfiles[-1]) except: pass # Small search ellipse and the grid shift. gdaltest.runexternal(gdal_grid + ' -txe 440721.0 441920.0 -tye 3751321.0 3750120.0 -outsize 20 20 -ot Byte -l grid -a nearest:radius1=20.0:radius2=20.0:angle=0.0:nodata=0.0 data/grid.vrt ' + outfiles[-1]) # We should get the same values as in "gcore/data/byte.tif" ds = gdal.Open(outfiles[-1]) if ds.GetRasterBand(1).Checksum() != checksum_ref: gdaltest.post_reason('bad checksum') print('bad checksum : got %d, expected %d' % \ (ds.GetRasterBand(1).Checksum(), checksum_ref)) return 'fail' ds = None return 'success'
def test_gdal_grid_1(): if gdal_grid is None: return 'skip' shape_drv = ogr.GetDriverByName('ESRI Shapefile') outfiles.append('tmp/n43.tif') try: os.remove('tmp/n43.shp') except: pass try: os.remove('tmp/n43.dbf') except: pass try: os.remove('tmp/n43.shx') except: pass try: os.remove('tmp/n43.qix') except: pass # Create an OGR grid from the values of n43.dt0 ds = gdal.Open('../gdrivers/data/n43.dt0') geotransform = ds.GetGeoTransform() shape_drv = ogr.GetDriverByName('ESRI Shapefile') shape_ds = shape_drv.CreateDataSource( 'tmp' ) shape_lyr = shape_ds.CreateLayer( 'n43' ) data = ds.ReadRaster(0, 0, 121, 121) array_val = struct.unpack('h' * 121*121, data) for j in range(121): for i in range(121): wkt = 'POINT(%f %f %s)' % ( geotransform[0] + (i + .5) * geotransform[1], geotransform[3] + (j + .5) * geotransform[5], array_val[j * 121 + i] ) dst_feat = ogr.Feature( feature_def = shape_lyr.GetLayerDefn() ) dst_feat.SetGeometry(ogr.CreateGeometryFromWkt(wkt)) shape_lyr.CreateFeature( dst_feat ) dst_feat.Destroy() shape_ds.ExecuteSQL('CREATE SPATIAL INDEX ON n43') shape_ds.Destroy() # Create a GDAL dataset from the previous generated OGR grid (out, err) = gdaltest.runexternal_out_and_err(gdal_grid + ' -txe -80.0041667 -78.9958333 -tye 42.9958333 44.0041667 -outsize 121 121 -ot Int16 -l n43 -a nearest:radius1=0.0:radius2=0.0:angle=0.0 -co TILED=YES -co BLOCKXSIZE=256 -co BLOCKYSIZE=256 tmp/n43.shp ' + outfiles[-1]) if not (err is None or err == '') : gdaltest.post_reason('got error/warning') print(err) return 'fail' # We should get the same values as in n43.td0 ds2 = gdal.Open(outfiles[-1]) if ds.GetRasterBand(1).Checksum() != ds2.GetRasterBand(1).Checksum(): print('bad checksum : got %d, expected %d' % (ds.GetRasterBand(1).Checksum() , ds2.GetRasterBand(1).Checksum())) return 'fail' if ds2.GetRasterBand(1).GetNoDataValue() is not None: print('did not expect nodata value') return 'fail' ds = None ds2 = None return 'success'
def ogr_pgdump_8(): ds = ogr.GetDriverByName('PGDump').CreateDataSource('/vsimem/ogr_pgdump_8.sql', options = [ 'LINEFORMAT=LF' ] ) lyr = ds.CreateLayer('test', geom_type = ogr.wkbNone, options = ['FID=myfid']) lyr.CreateField(ogr.FieldDefn('str', ogr.OFTString)) gdal.PushErrorHandler() ret = lyr.CreateField(ogr.FieldDefn('myfid', ogr.OFTString)) gdal.PopErrorHandler() if ret == 0: gdaltest.post_reason('fail') return 'fail' ret = lyr.CreateField(ogr.FieldDefn('myfid', ogr.OFTInteger)) if ret != 0: gdaltest.post_reason('fail') return 'fail' lyr.CreateField(ogr.FieldDefn('str2', ogr.OFTString)) feat = ogr.Feature(lyr.GetLayerDefn()) feat.SetField('str', 'first string') feat.SetField('myfid', 10) feat.SetField('str2', 'second string') gdal.SetConfigOption( 'PG_USE_COPY', 'YES' ) ret = lyr.CreateFeature(feat) gdal.SetConfigOption( 'PG_USE_COPY', None ) if ret != 0: gdaltest.post_reason('fail') return 'fail' if feat.GetFID() != 10: gdaltest.post_reason('fail') return 'fail' feat = ogr.Feature(lyr.GetLayerDefn()) feat.SetField('str2', 'second string') gdal.SetConfigOption( 'PG_USE_COPY', 'YES' ) ret = lyr.CreateFeature(feat) gdal.SetConfigOption( 'PG_USE_COPY', None ) if ret != 0: gdaltest.post_reason('fail') return 'fail' if feat.GetFID() < 0: gdaltest.post_reason('fail') feat.DumpReadable() return 'fail' if feat.GetField('myfid') != feat.GetFID(): gdaltest.post_reason('fail') feat.DumpReadable() return 'fail' #feat.SetField('str', 'foo') #ret = lyr.SetFeature(feat) #if ret != 0: # gdaltest.post_reason('fail') # return 'fail' feat = ogr.Feature(lyr.GetLayerDefn()) feat.SetFID(1) feat.SetField('myfid', 10) gdal.PushErrorHandler() gdal.SetConfigOption( 'PG_USE_COPY', 'YES' ) ret = lyr.CreateFeature(feat) gdal.SetConfigOption( 'PG_USE_COPY', None ) gdal.PopErrorHandler() if ret == 0: gdaltest.post_reason('fail') return 'fail' #gdal.PushErrorHandler() #ret = lyr.SetFeature(feat) #gdal.PopErrorHandler() #if ret == 0: # gdaltest.post_reason('fail') # return 'fail' #feat.UnsetField('myfid') #gdal.PushErrorHandler() #ret = lyr.SetFeature(feat) #gdal.PopErrorHandler() #if ret == 0: # gdaltest.post_reason('fail') # return 'fail' feat = ogr.Feature(lyr.GetLayerDefn()) feat.SetField('str', 'first string') feat.SetField('myfid', 12) feat.SetField('str2', 'second string') gdal.SetConfigOption( 'PG_USE_COPY', 'YES' ) ret = lyr.CreateFeature(feat) gdal.SetConfigOption( 'PG_USE_COPY', None ) if ret != 0: gdaltest.post_reason('fail') return 'fail' if feat.GetFID() != 12: gdaltest.post_reason('fail') return 'fail' ds = None f = gdal.VSIFOpenL('/vsimem/ogr_pgdump_8.sql', 'rb') sql = gdal.VSIFReadL(1, 10000, f).decode('ascii') gdal.VSIFCloseL(f) gdal.Unlink('/vsimem/ogr_pgdump_8.sql') if sql.find("""CREATE TABLE "public"."test" ( "myfid" SERIAL, CONSTRAINT "test_pk" PRIMARY KEY ("myfid") )""") < 0 or \ sql.find("""ALTER TABLE "public"."test" ADD COLUMN "myfid" """) >= 0 or \ sql.find("""10\tfirst string\tsecond string""") == -1 or \ sql.find("""INSERT INTO "public"."test" ("str2") VALUES ('second string');""") == -1 or \ sql.find("""12\tfirst string\tsecond string""") == -1: print(sql) return 'fail' return 'success'
def test_gdal_grid_3(): if gdal_grid is None: return 'skip' ################# # Test generic implementation (no AVX, no SSE) outfiles.append('tmp/grid_invdist_generic.tif') try: os.remove(outfiles[-1]) except: pass # Create a GDAL dataset from the values of "grid.csv". print('Step 1: Disabling AVX/SSE optimized versions...') (out, err) = gdaltest.runexternal_out_and_err(gdal_grid + ' --debug on --config GDAL_USE_AVX NO --config GDAL_USE_SSE NO -txe 440720.0 441920.0 -tye 3751320.0 3750120.0 -outsize 20 20 -ot Float64 -l grid -a invdist:power=2.0:smoothing=0.0:radius1=0.0:radius2=0.0:angle=0.0:max_points=0:min_points=0:nodata=0.0 data/grid.vrt ' + outfiles[-1]) pos = err.find(' threads') if pos >= 0: pos_blank = err[0:pos-1].rfind(' ') if pos_blank >= 0: print('Step 1: %s threads used' % err[pos_blank+1:pos]) # We should get the same values as in "ref_data/gdal_invdist.tif" ds = gdal.Open(outfiles[-1]) ds_ref = gdal.Open('ref_data/grid_invdist.tif') maxdiff = gdaltest.compare_ds(ds, ds_ref, verbose = 0) if maxdiff > 1: gdaltest.compare_ds(ds, ds_ref, verbose = 1) gdaltest.post_reason('Image too different from the reference') return 'fail' ds_ref = None ds = None ################# # Potentially test optimized SSE implementation outfiles.append('tmp/grid_invdist_sse.tif') try: os.remove(outfiles[-1]) except: pass # Create a GDAL dataset from the values of "grid.csv". print('Step 2: Trying SSE optimized version...') (out, err) = gdaltest.runexternal_out_and_err(gdal_grid + ' --debug on --config GDAL_USE_AVX NO -txe 440720.0 441920.0 -tye 3751320.0 3750120.0 -outsize 20 20 -ot Float64 -l grid -a invdist:power=2.0:smoothing=0.0:radius1=0.0:radius2=0.0:angle=0.0:max_points=0:min_points=0:nodata=0.0 data/grid.vrt ' + outfiles[-1]) if err.find('SSE') >= 0: print('...SSE optimized version used') else: print('...SSE optimized version NOT used') # We should get the same values as in "ref_data/gdal_invdist.tif" ds = gdal.Open(outfiles[-1]) ds_ref = gdal.Open('ref_data/grid_invdist.tif') maxdiff = gdaltest.compare_ds(ds, ds_ref, verbose = 0) if maxdiff > 1: gdaltest.compare_ds(ds, ds_ref, verbose = 1) gdaltest.post_reason('Image too different from the reference') return 'fail' ds_ref = None ds = None ################# # Potentially test optimized AVX implementation outfiles.append('tmp/grid_invdist_avx.tif') try: os.remove(outfiles[-1]) except: pass # Create a GDAL dataset from the values of "grid.csv". print('Step 3: Trying AVX optimized version...') (out, err) = gdaltest.runexternal_out_and_err(gdal_grid + ' --debug on -txe 440720.0 441920.0 -tye 3751320.0 3750120.0 -outsize 20 20 -ot Float64 -l grid -a invdist:power=2.0:smoothing=0.0:radius1=0.0:radius2=0.0:angle=0.0:max_points=0:min_points=0:nodata=0.0 data/grid.vrt ' + outfiles[-1]) if err.find('AVX') >= 0: print('...AVX optimized version used') else: print('...AVX optimized version NOT used') # We should get the same values as in "ref_data/gdal_invdist.tif" ds = gdal.Open(outfiles[-1]) ds_ref = gdal.Open('ref_data/grid_invdist.tif') maxdiff = gdaltest.compare_ds(ds, ds_ref, verbose = 0) if maxdiff > 1: gdaltest.compare_ds(ds, ds_ref, verbose = 1) gdaltest.post_reason('Image too different from the reference') return 'fail' ds_ref = None ds = None ################# # Test GDAL_NUM_THREADS config option to 1 outfiles.append('tmp/grid_invdist_1thread.tif') try: os.remove(outfiles[-1]) except: pass # Create a GDAL dataset from the values of "grid.csv". gdaltest.runexternal(gdal_grid + ' --config GDAL_NUM_THREADS 1 -txe 440720.0 441920.0 -tye 3751320.0 3750120.0 -outsize 20 20 -ot Float64 -l grid -a invdist:power=2.0:smoothing=0.0:radius1=0.0:radius2=0.0:angle=0.0:max_points=0:min_points=0:nodata=0.0 data/grid.vrt ' + outfiles[-1]) # We should get the same values as in "ref_data/gdal_invdist.tif" ds = gdal.Open(outfiles[-1]) ds_ref = gdal.Open('ref_data/grid_invdist.tif') maxdiff = gdaltest.compare_ds(ds, ds_ref, verbose = 0) if maxdiff > 1: gdaltest.compare_ds(ds, ds_ref, verbose = 1) gdaltest.post_reason('Image too different from the reference') return 'fail' ds_ref = None ds = None ################# # Test GDAL_NUM_THREADS config option to 2 outfiles.append('tmp/grid_invdist_2threads.tif') try: os.remove(outfiles[-1]) except: pass # Create a GDAL dataset from the values of "grid.csv". gdaltest.runexternal(gdal_grid + ' --config GDAL_NUM_THREADS 2 -txe 440720.0 441920.0 -tye 3751320.0 3750120.0 -outsize 20 20 -ot Float64 -l grid -a invdist:power=2.0:smoothing=0.0:radius1=0.0:radius2=0.0:angle=0.0:max_points=0:min_points=0:nodata=0.0 data/grid.vrt ' + outfiles[-1]) # We should get the same values as in "ref_data/gdal_invdist.tif" ds = gdal.Open(outfiles[-1]) ds_ref = gdal.Open('ref_data/grid_invdist.tif') maxdiff = gdaltest.compare_ds(ds, ds_ref, verbose = 0) if maxdiff > 1: gdaltest.compare_ds(ds, ds_ref, verbose = 1) gdaltest.post_reason('Image too different from the reference') return 'fail' ds_ref = None ds = None ################# outfiles.append('tmp/grid_invdist_90_90_8p.tif') try: os.remove(outfiles[-1]) except: pass # Create a GDAL dataset from the values of "grid.csv". # Circular window, shifted, test min points and NODATA setting. gdaltest.runexternal(gdal_grid + ' -txe 440721.0 441920.0 -tye 3751321.0 3750120.0 -outsize 20 20 -ot Float64 -l grid -a invdist:power=2.0:radius1=90.0:radius2=90.0:angle=0.0:max_points=0:min_points=8:nodata=0.0 data/grid.vrt ' + outfiles[-1]) # We should get the same values as in "ref_data/grid_invdist_90_90_8p.tif" ds = gdal.Open(outfiles[-1]) ds_ref = gdal.Open('ref_data/grid_invdist_90_90_8p.tif') maxdiff = gdaltest.compare_ds(ds, ds_ref, verbose = 0) if maxdiff > 1: gdaltest.compare_ds(ds, ds_ref, verbose = 1) gdaltest.post_reason('Image too different from the reference') return 'fail' ds_ref = None ds = None return 'success'
def mask_22(): drv = gdal.GetDriverByName('GTiff') ds = drv.Create('tmp/mask_22.tif', 20, 20) ds.CreateMaskBand(gdal.GMF_PER_DATASET) cs = ds.GetRasterBand(1).GetMaskBand().Checksum() if cs != 0: print(cs) gdaltest.post_reason('Got wrong checksum for the mask') return 'fail' ds.GetRasterBand(1).GetMaskBand().Fill(1) cs = ds.GetRasterBand(1).GetMaskBand().Checksum() if cs != 400: print(cs) gdaltest.post_reason('Got wrong checksum for the mask') return 'fail' ds = None try: os.stat('tmp/mask_22.tif.msk') except: gdaltest.post_reason('tmp/mask_22.tif.msk is absent') return 'fail' ds = gdal.Open('tmp/mask_22.tif') if ds.GetRasterBand(1).GetMaskFlags() != gdal.GMF_PER_DATASET: gdaltest.post_reason('wrong mask flags') return 'fail' cs = ds.GetRasterBand(1).GetMaskBand().Checksum() if cs != 400: print(cs) gdaltest.post_reason('Got wrong checksum for the mask') return 'fail' ds = None drv.Delete('tmp/mask_22.tif') try: os.stat('tmp/mask_22.tif.msk') gdaltest.post_reason('tmp/mask_22.tif.msk is still there') return 'fail' except: pass return 'success'
def test_ogr_sde_5(): "Test versioned editing" if gdaltest.sde_dr is None: pytest.skip() version_name = 'TESTING' gdal.SetConfigOption('SDE_VERSIONOVERWRITE', 'TRUE') base = 'SDE:%s,%s,%s,%s,%s,SDE.TPOLY,SDE.DEFAULT,%s' % ( sde_server, sde_port, sde_db, sde_user, sde_password, version_name) ds = ogr.Open(base, update=1) l1 = ds.GetLayerByName('SDE.TPOLY') f1 = l1.GetFeature(1) f1.SetField("PRFEDEA", 'SDE.TESTING') l1.SetFeature(f1) ds.Destroy() del ds default = 'DEFAULT' gdal.SetConfigOption('SDE_VERSIONOVERWRITE', 'FALSE') default = 'SDE:%s,%s,%s,%s,%s,SDE.TPOLY,SDE.DEFAULT,%s' % ( sde_server, sde_port, sde_db, sde_user, sde_password, default) # print default ds2 = ogr.Open(default, update=1) l2 = ds2.GetLayerByName('SDE.TPOLY') f2 = l2.GetFeature(1) f2.SetField("PRFEDEA", 'SDE.DEFAULT') f2.SetField("WHEN", 2008, 3, 19, 16, 15, 00, 0) l2.SetFeature(f2) ds2.Destroy() del ds2 ds3 = ogr.Open(base) l3 = ds3.GetLayerByName('SDE.TPOLY') f3 = l3.GetFeature(1) assert f3.GetField("PRFEDEA") == "SDE.TESTING", \ 'versioned editing failed for child version SDE.TESTING' ds3.Destroy() del ds3 ds4 = ogr.Open(default) l4 = ds4.GetLayerByName('SDE.TPOLY') f4 = l4.GetFeature(1) assert f4.GetField("PRFEDEA") == "SDE.DEFAULT", \ 'versioned editing failed for parent version SDE.DEFAULT' idx = f4.GetFieldIndex('WHEN') df = f4.GetField(idx) if df != '2008/03/19 16:15:00': gdaltest.post_reason( "datetime handling did not work -- expected '2008/03/19 16:15:00' got '%s' " % df) ds4.Destroy() del ds4
def mask_14(): src_ds = gdal.Open('data/byte.tif') if src_ds is None: gdaltest.post_reason('Failed to open test dataset.') return 'fail' drv = gdal.GetDriverByName('GTiff') gdal.SetConfigOption('GDAL_TIFF_INTERNAL_MASK_TO_8BIT', 'FALSE') ds = drv.CreateCopy('tmp/byte_with_mask.tif', src_ds) gdal.SetConfigOption('GDAL_TIFF_INTERNAL_MASK_TO_8BIT', 'TRUE') src_ds = None gdal.SetConfigOption('GDAL_TIFF_INTERNAL_MASK', 'YES') ds.CreateMaskBand(gdal.GMF_PER_DATASET) gdal.SetConfigOption('GDAL_TIFF_INTERNAL_MASK', 'NO') cs = ds.GetRasterBand(1).GetMaskBand().Checksum() if cs != 0: print(cs) gdaltest.post_reason('Got wrong checksum for the mask (1)') return 'fail' ds.GetRasterBand(1).GetMaskBand().Fill(1) cs = ds.GetRasterBand(1).GetMaskBand().Checksum() if cs != 400: print(cs) gdaltest.post_reason('Got wrong checksum for the mask (2)') return 'fail' ds = None try: os.stat('tmp/byte_with_mask.tif.msk') gdaltest.post_reason('tmp/byte_with_mask.tif.msk should not exist') return 'fail' except: pass gdal.SetConfigOption('GDAL_TIFF_INTERNAL_MASK_TO_8BIT', 'FALSE') ds = gdal.Open('tmp/byte_with_mask.tif') if ds.GetRasterBand(1).GetMaskFlags() != gdal.GMF_PER_DATASET: gdaltest.post_reason('wrong mask flags') return 'fail' gdal.SetConfigOption('GDAL_TIFF_INTERNAL_MASK_TO_8BIT', 'TRUE') cs = ds.GetRasterBand(1).GetMaskBand().Checksum() if cs != 400: print(cs) gdaltest.post_reason('Got wrong checksum for the mask (3)') return 'fail' # Test fix for #5884 gdal.SetConfigOption('GDAL_TIFF_INTERNAL_MASK', 'YES') old_val = gdal.GetCacheMax() gdal.SetCacheMax(0) out_ds = drv.CreateCopy('/vsimem/byte_with_mask.tif', ds, options=['COMPRESS=JPEG']) gdal.SetConfigOption('GDAL_TIFF_INTERNAL_MASK', None) gdal.SetCacheMax(old_val) if out_ds.GetRasterBand(1).Checksum() == 0: gdaltest.post_reason('failure') return 'fail' cs = ds.GetRasterBand(1).GetMaskBand().Checksum() if cs != 400: print(cs) gdaltest.post_reason('Got wrong checksum for the mask (4)') return 'fail' out_ds = None drv.Delete('/vsimem/byte_with_mask.tif') ds = None drv.Delete('tmp/byte_with_mask.tif') return 'success'
def mask_3(): ds = gdal.Open('data/stefan_full_rgba.png') if ds is None: gdaltest.post_reason('Failed to open test dataset.') return 'fail' # Test first mask. band = ds.GetRasterBand(1) if band.GetMaskFlags() != gdal.GMF_ALPHA + gdal.GMF_PER_DATASET: gdaltest.post_reason('Did not get expected mask.') return 'fail' cs = band.GetMaskBand().Checksum() if cs != 10807: gdaltest.post_reason('Got wrong mask checksum') print(cs) return 'fail' # Verify second and third same as first. band_2 = ds.GetRasterBand(2) band_3 = ds.GetRasterBand(3) # We have commented the following tests as SWIG >= 1.3.37 is buggy ! # or str(band_2.GetMaskBand()) != str(band.GetMaskBand()) \ # or str(band_3.GetMaskBand()) != str(band.GetMaskBand()) if band_2.GetMaskFlags() != band.GetMaskFlags() \ or band_3.GetMaskFlags() != band.GetMaskFlags(): gdaltest.post_reason('Band 2 or 3 does not seem to match first mask') return 'fail' # Verify alpha has no mask. band = ds.GetRasterBand(4) if band.GetMaskFlags() != gdal.GMF_ALL_VALID: gdaltest.post_reason('Did not get expected mask for alpha.') return 'fail' cs = band.GetMaskBand().Checksum() if cs != 36074: gdaltest.post_reason('Got wrong alpha mask checksum') print(cs) return 'fail' return 'success'
def mask_12(): gdal.SetConfigOption('GDAL_TIFF_INTERNAL_MASK_TO_8BIT', 'FALSE') ds = gdal.Open('data/test3_with_mask_1bit_and_ovr.tif') if ds is None: gdaltest.post_reason('Failed to open test dataset.') return 'fail' for i in (1, 2, 3): band = ds.GetRasterBand(i) # Let's fetch the mask if band.GetMaskFlags() != gdal.GMF_PER_DATASET: gdaltest.post_reason('Did not get expected mask.') return 'fail' cs = band.GetMaskBand().Checksum() if cs != 100: gdaltest.post_reason('Got wrong mask checksum') print(cs) return 'fail' # Let's fetch the overview band = ds.GetRasterBand(i).GetOverview(0) cs = band.Checksum() if cs != 1126: gdaltest.post_reason('Got wrong overview checksum') print(cs) return 'fail' # Let's fetch the mask of the overview if band.GetMaskFlags() != gdal.GMF_PER_DATASET: gdaltest.post_reason('Did not get expected mask.') return 'fail' cs = band.GetMaskBand().Checksum() if cs != 25: gdaltest.post_reason( 'Got wrong checksum for the mask of the overview') print(cs) return 'fail' # Let's fetch the overview of the mask == the mask of the overview band = ds.GetRasterBand(i).GetMaskBand().GetOverview(0) cs = band.Checksum() if cs != 25: gdaltest.post_reason( 'Got wrong checksum for the overview of the mask') print(cs) return 'fail' gdal.SetConfigOption('GDAL_TIFF_INTERNAL_MASK_TO_8BIT', 'TRUE') return 'success'
def mask_and_ovr(order, method): src_ds = gdal.Open('data/byte.tif') if src_ds is None: gdaltest.post_reason('Failed to open test dataset.') return 'fail' drv = gdal.GetDriverByName('GTiff') ds = drv.CreateCopy('tmp/byte_with_ovr_and_mask.tif', src_ds) src_ds = None if order == 1: gdal.SetConfigOption('GDAL_TIFF_INTERNAL_MASK', 'YES') ds.CreateMaskBand(gdal.GMF_PER_DATASET) gdal.SetConfigOption('GDAL_TIFF_INTERNAL_MASK', 'NO') ds.BuildOverviews(method, overviewlist=[2, 4]) gdal.SetConfigOption('GDAL_TIFF_INTERNAL_MASK', 'YES') ds.GetRasterBand(1).GetOverview(0).CreateMaskBand(gdal.GMF_PER_DATASET) ds.GetRasterBand(1).GetOverview(1).CreateMaskBand(gdal.GMF_PER_DATASET) gdal.SetConfigOption('GDAL_TIFF_INTERNAL_MASK', 'NO') elif order == 2: gdal.SetConfigOption('GDAL_TIFF_INTERNAL_MASK', 'YES') ds.BuildOverviews(method, overviewlist=[2, 4]) ds.CreateMaskBand(gdal.GMF_PER_DATASET) ds.GetRasterBand(1).GetOverview(0).CreateMaskBand(gdal.GMF_PER_DATASET) ds.GetRasterBand(1).GetOverview(1).CreateMaskBand(gdal.GMF_PER_DATASET) gdal.SetConfigOption('GDAL_TIFF_INTERNAL_MASK', 'NO') elif order == 3: gdal.SetConfigOption('GDAL_TIFF_INTERNAL_MASK', 'YES') ds.BuildOverviews(method, overviewlist=[2, 4]) ds.GetRasterBand(1).GetOverview(0).CreateMaskBand(gdal.GMF_PER_DATASET) ds.GetRasterBand(1).GetOverview(1).CreateMaskBand(gdal.GMF_PER_DATASET) ds.CreateMaskBand(gdal.GMF_PER_DATASET) gdal.SetConfigOption('GDAL_TIFF_INTERNAL_MASK', 'NO') elif order == 4: gdal.SetConfigOption('GDAL_TIFF_INTERNAL_MASK', 'YES') ds.CreateMaskBand(gdal.GMF_PER_DATASET) ds.GetRasterBand(1).GetMaskBand().Fill(1) # The overview for the mask will be implicitly created and computed. ds.BuildOverviews(method, overviewlist=[2, 4]) gdal.SetConfigOption('GDAL_TIFF_INTERNAL_MASK', 'NO') if order < 4: ds = None ds = gdal.Open('tmp/byte_with_ovr_and_mask.tif', gdal.GA_Update) ds.GetRasterBand(1).GetMaskBand().Fill(1) # The overview of the mask will be implicitly recomputed. ds.BuildOverviews(method, overviewlist=[2, 4]) ds = None try: os.stat('tmp/byte_with_ovr_and_mask.tif.msk') gdaltest.post_reason('tmp/byte_with_mask.tif.msk should not exist') return 'fail' except: pass gdal.SetConfigOption('GDAL_TIFF_INTERNAL_MASK_TO_8BIT', 'FALSE') ds = gdal.Open('tmp/byte_with_ovr_and_mask.tif') if ds.GetRasterBand(1).GetMaskFlags() != gdal.GMF_PER_DATASET: gdaltest.post_reason('wrong mask flags') return 'fail' gdal.SetConfigOption('GDAL_TIFF_INTERNAL_MASK_TO_8BIT', 'TRUE') cs = ds.GetRasterBand(1).GetMaskBand().Checksum() if cs != 400: print(cs) gdaltest.post_reason('Got wrong checksum for the mask') return 'fail' cs = ds.GetRasterBand(1).GetOverview(0).GetMaskBand().Checksum() if cs != 100: print(cs) gdaltest.post_reason( 'Got wrong checksum for the mask of the first overview') return 'fail' cs = ds.GetRasterBand(1).GetOverview(1).GetMaskBand().Checksum() if cs != 25: print(cs) gdaltest.post_reason( 'Got wrong checksum for the mask of the second overview') return 'fail' ds = None drv.Delete('tmp/byte_with_ovr_and_mask.tif') return 'success'
def mask_5(): # This crashes with libtiff 3.8.2, so skip it md = gdal.GetDriverByName('GTiff').GetMetadata() if md['DMD_CREATIONOPTIONLIST'].find('BigTIFF') == -1: return 'skip' ds = gdal.Open('tmp/mask_4.pnm', gdal.GA_Update) if ds is None: gdaltest.post_reason('Failed to open test dataset.') return 'fail' # So that we instantiate the mask band before. ds.GetRasterBand(1).GetMaskFlags() ds.BuildOverviews(overviewlist=[2, 4]) # confirm mask flags on overview. ovr = ds.GetRasterBand(1).GetOverview(1) if ovr.GetMaskFlags() != gdal.GMF_PER_DATASET: gdaltest.post_reason('did not get expected mask flags') print(ovr.GetMaskFlags()) return 'fail' msk = ovr.GetMaskBand() cs = msk.Checksum() expected_cs = 20505 if cs != expected_cs: gdaltest.post_reason('Did not get expected checksum') print(cs) return 'fail' ovr = None msk = None ds = None # Reopen and confirm we still get same results. ds = gdal.Open('tmp/mask_4.pnm') # confirm mask flags on overview. ovr = ds.GetRasterBand(1).GetOverview(1) if ovr.GetMaskFlags() != gdal.GMF_PER_DATASET: gdaltest.post_reason('did not get expected mask flags') print(ovr.GetMaskFlags()) return 'fail' msk = ovr.GetMaskBand() cs = msk.Checksum() expected_cs = 20505 if cs != expected_cs: gdaltest.post_reason('Did not get expected checksum') print(cs) return 'fail' ovr = None msk = None ds = None gdal.GetDriverByName('PNM').Delete('tmp/mask_4.pnm') return 'success'
def mask_13(): gdal.SetConfigOption('GDAL_TIFF_INTERNAL_MASK', 'NO') src_ds = gdal.Open('data/byte.tif') if src_ds is None: gdaltest.post_reason('Failed to open test dataset.') return 'fail' drv = gdal.GetDriverByName('GTiff') ds = drv.CreateCopy('tmp/byte_with_mask.tif', src_ds) src_ds = None ds.CreateMaskBand(gdal.GMF_PER_DATASET) cs = ds.GetRasterBand(1).GetMaskBand().Checksum() if cs != 0: print(cs) gdaltest.post_reason('Got wrong checksum for the mask') return 'fail' ds.GetRasterBand(1).GetMaskBand().Fill(1) cs = ds.GetRasterBand(1).GetMaskBand().Checksum() if cs != 400: print(cs) gdaltest.post_reason('Got wrong checksum for the mask') return 'fail' ds = None try: os.stat('tmp/byte_with_mask.tif.msk') except: gdaltest.post_reason('tmp/byte_with_mask.tif.msk is absent') return 'fail' ds = gdal.Open('tmp/byte_with_mask.tif') if ds.GetRasterBand(1).GetMaskFlags() != gdal.GMF_PER_DATASET: gdaltest.post_reason('wrong mask flags') return 'fail' cs = ds.GetRasterBand(1).GetMaskBand().Checksum() if cs != 400: print(cs) gdaltest.post_reason('Got wrong checksum for the mask') return 'fail' ds = None drv.Delete('tmp/byte_with_mask.tif') try: os.stat('tmp/byte_with_mask.tif.msk') gdaltest.post_reason('tmp/byte_with_mask.tif.msk is still there') return 'fail' except: pass return 'success'