Learn R Programming

raster (version 2.1-25)

writeRaster: Write raster data to a file

Description

Write an entire Raster* object to a file, using one of the many supported formats. See writeValues for writing in chunks (e.g. by row). When writing a file to disk, the file format is determined by the 'format=' argument if supplied, or else by the file extension (if the extension is known). If other cases the default format is used. The default format is 'raster', but this setting can be changed (see rasterOptions).

Usage

## S3 method for class 'RasterLayer,character':
writeRaster(x, filename, format, ...)

## S3 method for class 'RasterStackBrick,character':
writeRaster(x, filename, format, bylayer, suffix='numbers', ...)

Arguments

x
Raster* object
filename
Output filename
format
Character. Output file type. See writeFormats. If this argument is not provided, it is attempted to infer it from the filename extension. If that fails, the default format is used. The default fo
...
Additional arguments: datatype{Character. Output data type (e.g. 'INT2S' or 'FLT4S'). See dataType. If no datatype is specified, 'FLT4S' is used, unless this default value was chang
bylayer
if TRUE, write a seperate file for each layer
suffix
'numbers' or 'names' to determine the suffix that each file gets when bylayer=TRUE; either a number between 1 and nlayers(x) or names(x)

Value

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

Details

See writeFormats for supported file types ("formats", "drivers"). The rgdal package is needed, except for these file formats: 'raster', 'BIL', 'BIP', 'BSQ', 'SAGA', 'ascii', 'IDRISI', and 'CDF'. Some of these formats can be used with or without rgdal (idrisi, SAGA, ascii). You need the 'ncdf' library for the 'CDF' format. In multi-layer files (i.e. files saved from RasterStack or RasterBrick objects), in the native 'raster' format, the band-order can be set to BIL ('Bands Interleaved by Line'), BIP ('Bands Interleaved by Pixels') or BSQ ('Bands SeQuential'). Note that bandorder is not the same as filetype here. Supported file types include: llllr{ File type Long name default extension Multiband support raster 'Native' raster package format .grd Yes ascii ESRI Ascii .asc No SAGA SAGA GIS .sdat No IDRISI IDRISI .rst No CDF netCDF (requires ncdf) .nc Yes GTiff GeoTiff (requires rgdal) .tif Yes ENVI ENVI .hdr Labelled .envi Yes EHdr ESRI .hdr Labelled .bil Yes HFA Erdas Imagine Images (.img) .img Yes }

See Also

writeFormats, writeValues

Examples

Run this code
r <- raster(system.file("external/test.grd", package="raster"))

# take a small part
r <- crop(r, extent(179880, 180800, 329880, 330840) )

# write to an integer binary file
rf <- writeRaster(r, filename="allint.grd", datatype='INT4S', overwrite=TRUE)

# make a brick and save multi-layer file
b <- brick(r, sqrt(r))
bf <- writeRaster(b, filename="multi.grd", bandorder='BIL', overwrite=TRUE)

# write to a new geotiff file (depends on rgdal)
if (require(rgdal)) {
  rf <- writeRaster(r, filename="test.tif", format="GTiff", overwrite=TRUE)
  bf <- writeRaster(b, filename="multi.tif", options="INTERLEAVE=BAND", overwrite=TRUE)
}
 
# write to netcdf 
if (require(ncdf)) {	
 rnc <- writeRaster(r, filename='netCDF.nc', format="CDF", overwrite=TRUE)   
}

Run the code above in your browser using DataLab