def average(input_dir, out_dir, size):
input_dir = abspath(input_dir)
log.info('Reading tifs from {}'.format(input_dir))
tifs = glob.glob(join(input_dir, '*.tif'))
for tif in tifs:
data_source = gdal.Open(tif, gdal.GA_ReadOnly)
band = data_source.GetRasterBand(1)
# data_type = gdal.GetDataTypeName(band.DataType)
data = band.ReadAsArray()
no_data_val = band.GetNoDataValue()
averaged_data = filter_data(data, size, no_data_val)
log.info('Calculated average for {}'.format(basename(tif)))
output_file = join(out_dir, 'average_' + basename(tif))
out_ds = gdal.GetDriverByName('GTiff').Create(
output_file, data_source.RasterXSize, data_source.RasterYSize,
1, band.DataType)
out_band = out_ds.GetRasterBand(1)
out_band.WriteArray(averaged_data)
out_ds.SetGeoTransform(data_source.GetGeoTransform())
out_ds.SetProjection(data_source.GetProjection())
out_band.FlushCache() # Write data to disc
out_ds = None # close out_ds
data_source = None # close dataset
log.info('Finished converting {}'.format(basename(tif)))
评论列表
文章目录