def isRaster(source): """ Test if loadRaster fails for the given input Parameters: ----------- source : str The path to the raster file to load Returns: -------- bool -> True if the given input is a raster """ if isinstance(source, gdal.Dataset): try: if source.GetLayerCount() == 0: return True # This should always be true? else: return False except: return False elif isinstance(source, str): d = gdal.IdentifyDriver(source) meta = d.GetMetadata() if meta.get("DCAP_RASTER", False) == "YES": return True else: return False else: return False
def netcdf_23(): #don't skip if netcdf is not enabled in GDAL #if gdaltest.netcdf_drv is None: # return 'skip' #if not gdaltest.netcdf_drv_has_hdf4: # return 'skip' #skip test if Hdf4 is not enabled in GDAL if gdal.GetDriverByName( 'HDF4' ) is None and \ gdal.GetDriverByName( 'HDF4Image' ) is None: return 'skip' ifile = 'data/hdifftst2.hdf' #test with Open() ds = gdal.Open(ifile) if ds is None: gdaltest.post_reason('GDAL did not open hdf4 file') return 'fail' else: name = ds.GetDriver().GetDescription() ds = None #return fail if opened with the netCDF driver if name == 'netCDF': gdaltest.post_reason('netcdf driver opened hdf4 file') return 'fail' # test with Identify() name = gdal.IdentifyDriver(ifile).GetDescription() if name == 'netCDF': gdaltest.post_reason('netcdf driver was identified for hdf4 file') return 'fail' return 'success'
def isVector(source): """ Test if loadVector fails for the given input Parameters: ----------- source : str The path to the vector file to load Returns: -------- bool -> True if the given input is a vector """ if isinstance(source, gdal.Dataset): if source.GetLayerCount() > 0: return True else: return False elif isinstance(source, str): d = gdal.IdentifyDriver(source) meta = d.GetMetadata() if meta.get("DCAP_VECTOR", False) == "YES": return True else: return False else: return False
def netcdf_18(): if gdaltest.netcdf_drv is None: return 'skip' ifile = 'data/trmm-nc4c.nc' if gdaltest.netcdf_drv_has_nc4: # test with Open() ds = gdal.Open(ifile) if ds is None: return 'fail' else: name = ds.GetDriver().GetDescription() ds = None #return fail if did not open with the netCDF driver (i.e. HDF5Image) if name != 'netCDF': return 'fail' # test with Identify() name = gdal.IdentifyDriver(ifile).GetDescription() if name != 'netCDF': return 'fail' else: return 'skip' return 'success'
def netcdf_17(): if gdaltest.netcdf_drv is None: return 'skip' ifile = 'data/u8be.h5' #skip test if Hdf5 is not enabled if gdal.GetDriverByName( 'HDF5' ) is None and \ gdal.GetDriverByName( 'HDF5Image' ) is None: return 'skip' if gdaltest.netcdf_drv_has_nc4: #test with Open() ds = gdal.Open( ifile ) if ds is None: return 'fail' else: name = ds.GetDriver().GetDescription() ds = None #return fail if opened with the netCDF driver if name == 'netCDF': return 'fail' # test with Identify() name = gdal.IdentifyDriver( ifile ).GetDescription() if name == 'netCDF': return 'fail' else: return 'skip' return 'success'
def identify_3(): if gdaltest.IdentifyDriver is None: return 'skip' dr = gdal.IdentifyDriver( 'data' ) if dr is not None: gdaltest.post_reason( 'Got a driver for data directory!' ) return 'fail' return 'success'
def identify_2(): if gdaltest.IdentifyDriver is None: return 'skip' file_list = gdal.ReadDir( 'data' ) dr = gdal.IdentifyDriver( 'data/byte.pnm.aux.xml', file_list ) if dr is not None: gdaltest.post_reason( 'Got a driver for byte.pnm.aux.xml!' ) return 'fail' return 'success'
def identify_1(): try: gdaltest.IdentifyDriver = gdal.IdentifyDriver except: gdaltest.IdentifyDriver = None return 'skip' file_list = gdal.ReadDir( 'data' ) dr = gdal.IdentifyDriver( 'data/byte.tif', file_list ) if dr is None or dr.GetDescription() != 'GTiff': gdaltest.post_reason( 'Got wrong driver for byte.tif' ) return 'fail' return 'success'
def netcdf_17(): if gdaltest.netcdf_drv is None: return 'skip' ifile = 'data/groups.h5' #skip test if Hdf5 is not enabled if gdal.GetDriverByName( 'HDF5' ) is None and \ gdal.GetDriverByName( 'HDF5Image' ) is None: return 'skip' if gdaltest.netcdf_drv_has_nc4: #test with Open() ds = gdal.Open(ifile) if ds is None: gdaltest.post_reason('GDAL did not open hdf5 file') return 'fail' else: name = ds.GetDriver().GetDescription() ds = None #return fail if opened with the netCDF driver if name == 'netCDF': gdaltest.post_reason('netcdf driver opened hdf5 file') return 'fail' # test with Identify() name = gdal.IdentifyDriver(ifile).GetDescription() if name == 'netCDF': gdaltest.post_reason('netcdf driver was identified for hdf5 file') return 'fail' else: return 'skip' return 'success'
def gdal_api_proxy_sub(): src_ds = gdal.Open('data/byte.tif') src_cs = src_ds.GetRasterBand(1).Checksum() src_gt = src_ds.GetGeoTransform() src_prj = src_ds.GetProjectionRef() src_data = src_ds.ReadRaster(0, 0, 20, 20) src_md = src_ds.GetMetadata() src_ds = None drv = gdal.IdentifyDriver('data/byte.tif') if drv.GetDescription() != 'API_PROXY': gdaltest.post_reason('fail') return 'fail' ds = gdal.GetDriverByName('GTiff').Create('tmp/byte.tif', 1, 1, 3) ds = None src_ds = gdal.Open('data/byte.tif') ds = gdal.GetDriverByName('GTiff').CreateCopy('tmp/byte.tif', src_ds, options=['TILED=YES']) got_cs = ds.GetRasterBand(1).Checksum() if src_cs != got_cs: gdaltest.post_reason('fail') return 'fail' ds = None ds = gdal.Open('tmp/byte.tif', gdal.GA_Update) ds.SetGeoTransform([1, 2, 3, 4, 5, 6]) got_gt = ds.GetGeoTransform() if src_gt == got_gt: gdaltest.post_reason('fail') return 'fail' ds.SetGeoTransform(src_gt) got_gt = ds.GetGeoTransform() if src_gt != got_gt: gdaltest.post_reason('fail') return 'fail' if ds.GetGCPCount() != 0: gdaltest.post_reason('fail') return 'fail' if ds.GetGCPProjection() != '': print(ds.GetGCPProjection()) gdaltest.post_reason('fail') return 'fail' if len(ds.GetGCPs()) != 0: gdaltest.post_reason('fail') return 'fail' gcps = [gdal.GCP(0, 1, 2, 3, 4)] ds.SetGCPs(gcps, "foo") got_gcps = ds.GetGCPs() if len(got_gcps) != 1: gdaltest.post_reason('fail') return 'fail' if got_gcps[0].GCPLine != gcps[0].GCPLine or \ got_gcps[0].GCPPixel != gcps[0].GCPPixel or \ got_gcps[0].GCPX != gcps[0].GCPX or \ got_gcps[0].GCPY != gcps[0].GCPY: gdaltest.post_reason('fail') return 'fail' if ds.GetGCPProjection() != 'foo': gdaltest.post_reason('fail') print(ds.GetGCPProjection()) return 'fail' ds.SetGCPs([], "") if len(ds.GetGCPs()) != 0: gdaltest.post_reason('fail') return 'fail' ds.SetProjection('') got_prj = ds.GetProjectionRef() if src_prj == got_prj: gdaltest.post_reason('fail') return 'fail' ds.SetProjection(src_prj) got_prj = ds.GetProjectionRef() if src_prj != got_prj: gdaltest.post_reason('fail') print(src_prj) print(got_prj) return 'fail' ds.GetRasterBand(1).Fill(0) got_cs = ds.GetRasterBand(1).Checksum() if 0 != got_cs: gdaltest.post_reason('fail') return 'fail' ds.GetRasterBand(1).WriteRaster(0, 0, 20, 20, src_data) got_cs = ds.GetRasterBand(1).Checksum() if src_cs != got_cs: gdaltest.post_reason('fail') return 'fail' ds.GetRasterBand(1).Fill(0) got_cs = ds.GetRasterBand(1).Checksum() if 0 != got_cs: gdaltest.post_reason('fail') return 'fail' ds.WriteRaster(0, 0, 20, 20, src_data) got_cs = ds.GetRasterBand(1).Checksum() if src_cs != got_cs: gdaltest.post_reason('fail') return 'fail' # Not bound to SWIG # ds.AdviseRead(0,0,20,20,20,20) got_data = ds.ReadRaster(0, 0, 20, 20) if src_data != got_data: gdaltest.post_reason('fail') return 'fail' got_data = ds.GetRasterBand(1).ReadRaster(0, 0, 20, 20) if src_data != got_data: gdaltest.post_reason('fail') return 'fail' got_data_weird_spacing = ds.ReadRaster(0, 0, 20, 20, buf_pixel_space=1, buf_line_space=32) if len(got_data_weird_spacing) != 32 * (20 - 1) + 20: gdaltest.post_reason('fail') print(len(got_data_weird_spacing)) return 'fail' if got_data[20:20 + 20] != got_data_weird_spacing[32:32 + 20]: gdaltest.post_reason('fail') return 'fail' got_data_weird_spacing = ds.GetRasterBand(1).ReadRaster(0, 0, 20, 20, buf_pixel_space=1, buf_line_space=32) if len(got_data_weird_spacing) != 32 * (20 - 1) + 20: gdaltest.post_reason('fail') print(len(got_data_weird_spacing)) return 'fail' if got_data[20:20 + 20] != got_data_weird_spacing[32:32 + 20]: gdaltest.post_reason('fail') return 'fail' got_block = ds.GetRasterBand(1).ReadBlock(0, 0) if len(got_block) != 256 * 256: gdaltest.post_reason('fail') return 'fail' if got_data[20:20 + 20] != got_block[256:256 + 20]: gdaltest.post_reason('fail') return 'fail' ds.FlushCache() ds.GetRasterBand(1).FlushCache() got_data = ds.GetRasterBand(1).ReadRaster(0, 0, 20, 20) if src_data != got_data: gdaltest.post_reason('fail') return 'fail' if len(ds.GetFileList()) != 1: gdaltest.post_reason('fail') return 'fail' if ds.AddBand(gdal.GDT_Byte) == 0: gdaltest.post_reason('fail') return 'fail' got_md = ds.GetMetadata() if src_md != got_md: gdaltest.post_reason('fail') print(src_md) print(got_md) return 'fail' if ds.GetMetadataItem('AREA_OR_POINT') != 'Area': gdaltest.post_reason('fail') return 'fail' if ds.GetMetadataItem('foo') is not None: gdaltest.post_reason('fail') return 'fail' ds.SetMetadataItem('foo', 'bar') if ds.GetMetadataItem('foo') != 'bar': gdaltest.post_reason('fail') return 'fail' ds.SetMetadata({'foo': 'baz'}, 'OTHER') if ds.GetMetadataItem('foo', 'OTHER') != 'baz': gdaltest.post_reason('fail') return 'fail' ds.GetRasterBand(1).SetMetadata({'foo': 'baw'}, 'OTHER') if ds.GetRasterBand(1).GetMetadataItem('foo', 'OTHER') != 'baw': gdaltest.post_reason('fail') return 'fail' if ds.GetMetadataItem('INTERLEAVE', 'IMAGE_STRUCTURE') != 'BAND': gdaltest.post_reason('fail') return 'fail' if len(ds.GetRasterBand(1).GetMetadata()) != 0: gdaltest.post_reason('fail') return 'fail' if ds.GetRasterBand(1).GetMetadataItem('foo') is not None: gdaltest.post_reason('fail') return 'fail' ds.GetRasterBand(1).SetMetadataItem('foo', 'baz') if ds.GetRasterBand(1).GetMetadataItem('foo') != 'baz': gdaltest.post_reason('fail') return 'fail' ds.GetRasterBand(1).SetMetadata({'foo': 'baw'}) if ds.GetRasterBand(1).GetMetadataItem('foo') != 'baw': gdaltest.post_reason('fail') return 'fail' if ds.GetRasterBand(1).GetColorInterpretation() != gdal.GCI_GrayIndex: gdaltest.post_reason('fail') return 'fail' ds.GetRasterBand(1).SetColorInterpretation(gdal.GCI_Undefined) ct = ds.GetRasterBand(1).GetColorTable() if ct is not None: gdaltest.post_reason('fail') return 'fail' ct = gdal.ColorTable() ct.SetColorEntry(0, (1, 2, 3)) if ds.GetRasterBand(1).SetColorTable(ct) != 0: gdaltest.post_reason('fail') return 'fail' ct = ds.GetRasterBand(1).GetColorTable() if ct is None: gdaltest.post_reason('fail') return 'fail' if ct.GetColorEntry(0) != (1, 2, 3, 255): gdaltest.post_reason('fail') return 'fail' ct = ds.GetRasterBand(1).GetColorTable() if ct is None: gdaltest.post_reason('fail') return 'fail' if ds.GetRasterBand(1).SetColorTable(None) != 0: gdaltest.post_reason('fail') return 'fail' ct = ds.GetRasterBand(1).GetColorTable() if ct is not None: gdaltest.post_reason('fail') return 'fail' rat = ds.GetRasterBand(1).GetDefaultRAT() if rat is not None: gdaltest.post_reason('fail') return 'fail' if ds.GetRasterBand(1).SetDefaultRAT(None) != 0: gdaltest.post_reason('fail') return 'fail' ref_rat = gdal.RasterAttributeTable() if ds.GetRasterBand(1).SetDefaultRAT(ref_rat) != 0: gdaltest.post_reason('fail') return 'fail' rat = ds.GetRasterBand(1).GetDefaultRAT() if rat is None: gdaltest.post_reason('fail') return 'fail' if ds.GetRasterBand(1).SetDefaultRAT(None) != 0: gdaltest.post_reason('fail') return 'fail' rat = ds.GetRasterBand(1).GetDefaultRAT() if rat is not None: gdaltest.post_reason('fail') return 'fail' if ds.GetRasterBand(1).GetMinimum() is not None: gdaltest.post_reason('fail') return 'fail' got_stats = ds.GetRasterBand(1).GetStatistics(0, 0) if got_stats[3] >= 0.0: gdaltest.post_reason('fail') return 'fail' got_stats = ds.GetRasterBand(1).GetStatistics(1, 1) if got_stats[0] != 74.0: gdaltest.post_reason('fail') return 'fail' if ds.GetRasterBand(1).GetMinimum() != 74.0: gdaltest.post_reason('fail') return 'fail' if ds.GetRasterBand(1).GetMaximum() != 255.0: gdaltest.post_reason('fail') return 'fail' ds.GetRasterBand(1).SetStatistics(1, 2, 3, 4) got_stats = ds.GetRasterBand(1).GetStatistics(1, 1) if got_stats != [1, 2, 3, 4]: print(got_stats) gdaltest.post_reason('fail') return 'fail' ds.GetRasterBand(1).ComputeStatistics(0) got_stats = ds.GetRasterBand(1).GetStatistics(1, 1) if got_stats[0] != 74.0: gdaltest.post_reason('fail') return 'fail' minmax = ds.GetRasterBand(1).ComputeRasterMinMax() if minmax != (74.0, 255.0): gdaltest.post_reason('fail') print(minmax) return 'fail' if ds.GetRasterBand(1).GetOffset() != 0.0: gdaltest.post_reason('fail') return 'fail' if ds.GetRasterBand(1).GetScale() != 1.0: gdaltest.post_reason('fail') return 'fail' ds.GetRasterBand(1).SetOffset(10.0) if ds.GetRasterBand(1).GetOffset() != 10.0: gdaltest.post_reason('fail') return 'fail' ds.GetRasterBand(1).SetScale(2.0) if ds.GetRasterBand(1).GetScale() != 2.0: gdaltest.post_reason('fail') return 'fail' ds.BuildOverviews('NEAR', [2]) if ds.GetRasterBand(1).GetOverviewCount() != 1: gdaltest.post_reason('fail') return 'fail' if ds.GetRasterBand(1).GetOverview(-1) is not None: gdaltest.post_reason('fail') return 'fail' if ds.GetRasterBand(1).GetOverview(0) is None: gdaltest.post_reason('fail') return 'fail' if ds.GetRasterBand(1).GetOverview(0) is None: gdaltest.post_reason('fail') return 'fail' got_hist = ds.GetRasterBand(1).GetHistogram() if len(got_hist) != 256: gdaltest.post_reason('fail') return 'fail' (minval, maxval, nitems, got_hist2) = ds.GetRasterBand(1).GetDefaultHistogram() if minval != -0.5: gdaltest.post_reason('fail') return 'fail' if maxval != 255.5: gdaltest.post_reason('fail') return 'fail' if nitems != 256: gdaltest.post_reason('fail') return 'fail' if got_hist != got_hist2: gdaltest.post_reason('fail') return 'fail' ds.GetRasterBand(1).SetDefaultHistogram(1, 2, [3]) (minval, maxval, nitems, got_hist3) = ds.GetRasterBand(1).GetDefaultHistogram() if minval != 1: gdaltest.post_reason('fail') return 'fail' if maxval != 2: gdaltest.post_reason('fail') return 'fail' if nitems != 1: gdaltest.post_reason('fail') return 'fail' if got_hist3[0] != 3: gdaltest.post_reason('fail') return 'fail' got_nodatavalue = ds.GetRasterBand(1).GetNoDataValue() if got_nodatavalue is not None: gdaltest.post_reason('fail') return 'fail' ds.GetRasterBand(1).SetNoDataValue(123) got_nodatavalue = ds.GetRasterBand(1).GetNoDataValue() if got_nodatavalue != 123: gdaltest.post_reason('fail') return 'fail' if ds.GetRasterBand(1).GetMaskFlags() != 8: gdaltest.post_reason('fail') return 'fail' if ds.GetRasterBand(1).GetMaskBand() is None: gdaltest.post_reason('fail') return 'fail' ds.CreateMaskBand(0) if ds.GetRasterBand(1).GetMaskFlags() != 2: print(ds.GetRasterBand(1).GetMaskFlags()) gdaltest.post_reason('fail') return 'fail' if ds.GetRasterBand(1).GetMaskBand() is None: gdaltest.post_reason('fail') return 'fail' ds.GetRasterBand(1).CreateMaskBand(0) if ds.GetRasterBand(1).HasArbitraryOverviews() != 0: gdaltest.post_reason('fail') return 'fail' ds.GetRasterBand(1).SetUnitType('foo') if ds.GetRasterBand(1).GetUnitType() != 'foo': gdaltest.post_reason('fail') return 'fail' if ds.GetRasterBand(1).GetCategoryNames() is not None: gdaltest.post_reason('fail') return 'fail' ds.GetRasterBand(1).SetCategoryNames(['foo']) if ds.GetRasterBand(1).GetCategoryNames() != ['foo']: gdaltest.post_reason('fail') return 'fail' ds.GetRasterBand(1).SetDescription('bar') ds = None gdal.GetDriverByName('GTiff').Delete('tmp/byte.tif') return 'success'
#%% Set cmap if SCM if cmap_name.startswith('SCM'): if cmap_name.endswith('_r'): exec("cmap = {}.reversed()".format(cmap_name[:-2])) else: exec("cmap = {}".format(cmap_name)) elif cmap_name == 'insar': cdict = tools_lib.cmap_insar() plt.register_cmap( cmap=mpl.colors.LinearSegmentedColormap('insar', cdict)) cmap = 'insar' else: cmap = cmap_name #%% Get info and Read data if gdal.IdentifyDriver(infile): ## If Geotiff or grd geotiff = gdal.Open(infile) data = geotiff.ReadAsArray() if data.ndim > 2: print('\nERROR: {} has multiple bands and cannot be displayed.\n'. format(infile), file=sys.stderr) sys.exit(2) length, width = data.shape lon_w_p, dlon, _, lat_n_p, _, dlat = geotiff.GetGeoTransform() lat_s_p = lat_n_p + dlat * length lon_e_p = lon_w_p + dlon * width if data.dtype == np.float32: data[data == nodata] = np.nan