Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
print("Contents of {0}".format(os.path.basename(hdfpath)))
for i, dataset_string in enumerate(subdatasets):
print(" {0} {1}".format(i, dataset_string[1]))
if i in layer_indexs:
layer_names.append(dataset_string[1])
# give metadata info for the entire layer
mdict = hdf_dataset.GetMetadata()
out_info["MasterMetadata"] = mdict
#for key in mdict:
#print key," = ", mdict[key]
# perform operation on each of the desired layers
for layer in layer_indexs:
subdataset = gdal.Open(subdatasets[layer][0])
out_info[layer] = subdataset
return out_info
def sieve(image, dst_filename, convdate):
# 1. Remove all single pixels
#First create a band in memory that's that's just 1s and 0s
src_ds = gdal.Open( image, gdal.GA_ReadOnly )
srcband = src_ds.GetRasterBand(1)
srcarray = srcband.ReadAsArray()
srcarray[srcarray > 0] = 1
mem_rast = save_raster_memory(srcarray, image)
mem_band = mem_rast.GetRasterBand(1)
#Now the code behind gdal_sieve.py
maskband = None
drv = gdal.GetDriverByName('GTiff')
dst_ds = drv.Create( dst_filename,src_ds.RasterXSize, src_ds.RasterYSize,1,
srcband.DataType )
wkt = src_ds.GetProjection()
if wkt != '':
dst_ds.SetProjection( wkt )
dst_ds.SetGeoTransform( src_ds.GetGeoTransform() )
def _create_empty(self, gdal_dataset, gdal_metadata):
lat = gdal.Open(
self._get_sub_filenames(gdal_dataset)[self._latitude_band_number(gdal_dataset)]
)
super(NetcdfCF, self).__init__(lat.RasterXSize, lat.RasterYSize, metadata=gdal_metadata)
def OpenArray( array, prototype_ds = None ):
ds = gdal.Open( GetArrayFilename(array) )
if ds is not None and prototype_ds is not None:
if type(prototype_ds).__name__ == 'str':
prototype_ds = gdal.Open( prototype_ds )
if prototype_ds is not None:
CopyDatasetInfo( prototype_ds, ds )
return ds
def getNDVar(self,varname):
if TYPENDVAR :
str2 = 'NETCDF:"'+self.path+'":'+varname
v = gdal.Open(str2)
temp1 = np.array( v.ReadAsArray() )
v = None
return temp1[::-1,:]
else:
temp = self.get1DVar(varname)
return temp
def read_pixel_statistics(path, x, y):
out = list()
raster = gdal.Open(path, GA_ReadOnly)
assert raster
for b in range(1, raster.RasterCount+1):
band = raster.GetRasterBand(b)
assert band
data = band.ReadAsArray(x, y, 1, 1)
out.append(data)
band.FlushCache()
del band
raster.FlushCache()
del raster
else:
predict_arr = gdal.Open(str(test_file + "\\stacked_image1.tif"))
img_predict = predict_arr.ReadAsArray()
img_predict = img_predict.T
B = []
count=0
for i in range(len(img_predict[1])):
class_predict = rf.predict(img_predict[:,i,:])
B.append(class_predict)
B = np.array(B)
a,b = B.shape
if v.get()==0:
inRaster=str(test_file+"\\stacked_image.tif")
else:
inRaster=str(test_file+"\\stacked_image1.tif")
inDS=gdal.Open(inRaster,1)
geoTransform = inDS.GetGeoTransform()
band=inDS.GetRasterBand(1)
datatype=band.DataType
proj = inDS.GetProjection()
outRaster = test_file + "\\test_out.tif"
driver=inDS.GetDriver()
outDS = driver.Create(outRaster , b , a , 1 , datatype)
geoTransform = inDS.GetGeoTransform()
outDS.SetGeoTransform(geoTransform)
proj = inDS.GetProjection()
outDS.SetProjection(proj)
outBand = outDS.GetRasterBand(1)
outBand.WriteArray(B,0,0)
outDS=None
if v.get()==0:
print("Water Replacing...........................")
def exec(input_dir, model_filepath, output_filepath):
ts_utils.mkdirp( ts_utils.basedir(output_filepath) )
images = ts_utils.get_filenames(input_dir, \
filter_prefix='ts_b', filter_suffix='.vrt')
model = load_model(model_filepath)
base_img = images[0]
base_ds = gdal.Open(base_img)
n_files = len(images)
img_xsize = base_ds.RasterXSize
img_ysize = base_ds.RasterYSize
block_size = 256
pad_size = 1
out_img_ds = create_output_file(base_img, output_filepath)
out_band = out_img_ds.GetRasterBand(1)
count = 0
nblocks = len(range(0, img_xsize, block_size)) * len(range(0, img_ysize, block_size))
for xoff in range(0, img_xsize, block_size):
for yoff in range(0, img_ysize, block_size):
def copy_tif_metadata_from_file_to_file(filename_in, filename_out):
""" copy TIF metadata from an image to another """
print("Copy TIF information")
img_in = gdal.Open(filename_in)
img_out = gdal.Open(filename_out, gdal.GA_Update)
metadata_domains = img_in.GetMetadataDomainList()
for domain in metadata_domains:
dico = img_in.GetMetadata_Dict(domain)
for key, val in dico.items():
img_out.SetMetadataItem(key, val, domain)
img_out.SetProjection(img_in.GetProjection())
img_out.SetGeoTransform(img_in.GetGeoTransform())
del img_out
# Write outraster to file
dsband.WriteArray(glacierraster)
dsband.FlushCache()
dsband.GetStatistics(0,1)
#Close file
dsband = None
glacierraster = None
ds = None
#Load infile again and calculate new stats (not sure how to get from raster)
driver = gdal.GetDriverByName('GTiff')
driver.Register()
ds = gdal.Open(infile, gdal.GA_Update)
dsband = ds.GetRasterBand(1)
(newmin, newmax)= dsband.ComputeRasterMinMax(0)
(newmean, newstdv) = dsband.ComputeBandStats(1)
dsband.SetStatistics(newmin, newmax,newmean, newstdv)
dsband.FlushCache()
dsband = None
ds = None