def dump_raster(rast, rast_path, xoff=0, yoff=0, driver='GTiff', nodata=None):
'''
Creates a raster file from a given GDAL dataset (raster). Arguments:
rast A gdal.Dataset; does NOT accept NumPy array
rast_path The path of the output raster file
xoff Offset in the x-direction; should be provided when clipped
yoff Offset in the y-direction; should be provided when clipped
driver The name of the GDAL driver to use (determines file type)
nodata The NoData value; defaults to -9999.
'''
driver = gdal.GetDriverByName(driver)
sink = driver.Create(rast_path, rast.RasterXSize, rast.RasterYSize,
rast.RasterCount, rast.GetRasterBand(1).DataType)
assert sink is not None, 'Cannot create dataset; there may be a problem with the output path you specified'
sink.SetGeoTransform(rast.GetGeoTransform())
sink.SetProjection(rast.GetProjection())
for b in range(1, rast.RasterCount + 1):
dat = rast.GetRasterBand(b).ReadAsArray()
sink.GetRasterBand(b).WriteArray(dat)
sink.GetRasterBand(b).SetStatistics(*map(np.float64,
[dat.min(), dat.max(), dat.mean(), dat.std()]))
if nodata is None:
nodata = rast.GetRasterBand(b).GetNoDataValue()
if nodata is None:
nodata = -9999
sink.GetRasterBand(b).SetNoDataValue(np.float64(nodata))
sink.FlushCache()
评论列表
文章目录