raster (version 1.0.0-1)

writeRaster: Write raster data to a file

Description

Write raster values to a file. Either as binary ('raster') format, Ascii (ESRI Arc Ascii) format, and other GDAL supported formats. You can write an entire raster dataset at once, or by row.

Usage

writeRaster(x, filename, ...)

Arguments

x
Raster* object
filename
Output filename
...
Additional arguments. See below, under Methods

Value

  • This function is used for the side-effect of writing values to a file.

Methods

Full function call when x is a RasterLayer object writeRaster(x, filename='', ...) rll{ x RasterLayer object filename Output filename ... Additional arguments. See below } Full function call when x is a RasterBrick or RasterStack object writeRaster(x, filename='', bandorder='BIL', ...) rll{ x RasterLayer object filename Output filename bandorder Character. 'BIL', 'BIP', or 'BSQ' .. Additional arguments. See below } Additional arguments rll{ format Character. Output file type. See writeFormats datatype Character. Output data type. See dataType overwrite Logical. If TRUE, "filename" will be overwritten if it exists }

Details

Values in a RasterLayer object are written to a file. Relatively small datasets can be loaded into memory, manipulated, and saved to disk in their entirety. Large datasets can be read and written row by row. When writing by row, you must write all rows, and you must write them in sequence. Start at row=1, and end at row=nrow(raster). Only when all rows have been appended to the data file on disk, the file becomes readable. You cannot overwrite an isolated single row in an existing raster file. In multi-band files (i.e. files saved from RasterStack or RasterBrick objects), the bandorder can be set to BIL ('Bands Interleaved by Line'), BIP ('Bands Interleaved by Pixels') or BSQ ('Bands SeQuential') Note that the values of a RasterBrick and RasterLayer objects can be written to a single file in a row by row fashion; but the values of a RasterStack can only be saved to a file in one go. See writeFormats for supported file types ("formats", "drivers"); the rgdal package is needed for this function, exxcept for the 'raster', ascii, and 'CDF' formats. Supported file types include: lllr{ File type Long name Multiband support raster 'Native' raster package format Yes ascii ESRI Ascii No SAGA SAGA GIS No IDRISI IDRISI No CDF netCDF (requires RNetCDF) pending GTiff GeoTiff (requires rgdal) Yes ENVI ENVI .hdr Labelled Yes EHdr ESRI .hdr Labelled Yes HFA Erdas Imagine Images (.img) Yes }

See Also

writeFormats

Examples

Run this code
rst <- raster(system.file("external/test.grd", package="raster"))
 
# read all data
rst <- readAll(rst)

# write all to a new binary file
rst <- writeRaster(rst, filename="binall.grd", overwrite=TRUE)

# write all to a new ERDAS .img file
rst <- writeRaster(rst, filename="binall.img", format="HFA", overwrite=TRUE)

# write all to an integer binary file
rst <- writeRaster(rst, filename="binallint.grd", datatype='INT4S', overwrite=TRUE)
 
# write all to ascii file
rst <- writeRaster(rst, filename="ascall.asc", format='ascii', overwrite=TRUE)

# make a brick and save multi-band file
b <- brick(rst, sqrt(rst))
rst <- writeRaster(b, filename="mutli.grd", bandorder='BIL', overwrite=TRUE)
 
# read and write row by row; write to ascii file
rst <- raster(system.file("external/test.grd", package="raster"))
ascras <- raster(rst)
for (r in 1:nrow(rst)) {
	rst <- readRow(rst, r)
	v <- values(rst) * 10
	ascras <- setValues(ascras, v, r)
	ascras <- writeRaster(ascras, filename='ascrow.asc', format='ascii', overwrite=TRUE) 
}
	
# read and write row by row; write to binary file
binras <- raster(rst)
for (r in 1:nrow(rst)) {
	rst <- readRow(rst, r)
	binras <- setValues(binras, values(rst), r)
	binras <- writeRaster(binras, 'binbyrow', overwrite=TRUE) 
}

# read and write row by row; write to GeoTiff binary file
binras <- raster(rst)
for (r in 1:nrow(rst)) {
	rst <- readRow(rst, r)
	binras <- setValues(binras, values(rst), r)
	binras <- writeRaster(binras, 'binbyrow2.tif', format="GTiff", overwrite=TRUE) 
}

# write to netcdf 
if (require(RNetCDF)) {	
    rst <- readAll(rst)
    writeRaster(rst, filename='netCDF.nc', format="CDF", overwrite=TRUE)   
}

Run the code above in your browser using DataCamp Workspace