Learn R Programming

terra (version 1.6-17)

writeRaster: Write raster data to a file

Description

Write a SpatRaster object to a file.

Usage

# S4 method for SpatRaster,character
writeRaster(x, filename, overwrite=FALSE, ...)

Value

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

Arguments

x

SpatRaster

filename

character. Output filename. Can be a single filename, or as many filenames as nlyr(x) to write a file for each layer

overwrite

logical. If TRUE, filename is overwritten

...

additional arguments for for writing files. See Details

Details

In writeRaster, and in other methods that generate SpatRaster objects, options for writing raster files to disk can be provided as additional arguments or, in a few cases, as the wopt argument (a named list) if the additional arguments are already used for a different purpose. The following options are available:

namedescription
datatypevalues for datatype are "INT1U", "INT2U", "INT2S", "INT4U", "INT4S", "FLT4S", "FLT8S". The first three letters indicate whether the datatype is integer (whole numbers) of a real number (decimal numbers), the fourth character indicates the number of bytes used (allowing for large numbers and/or more precision), and the "S" or "U" indicate whether the values are signed (both negative and positive) or unsigned (positive values only).
filetypefile format expresses as GDAL driver names. If this argument is not supplied, the driver is derived from the filename.
gdalGDAL driver specific datasource creation options. See the GDAL documentation. For example, with the GeoTiff file format you can use gdal=c("COMPRESS=DEFLATE", "TFW=YES").
tempdirthe path where temporary files are to be written to.
progresspositive integer. If the number of chunks is larger, a progress bar is shown.
memfracnumeric between 0 and 0.9 (higher values give a warning). The fraction of available RAM that terra is allowed to use.
memmaxmemmax - the maximum amount of RAM (in GB) that terra can use when processing a raster dataset. Should be less than what is detected (see mem_info, and higher values are ignored. Set it to a negative number or NA to ignore this value).
namesoutput layer names.
NAflagnumeric. value to represent missing (NA or NaN) values. See note
verboselogical. If TRUE debugging information is printed.
stepspositive integers. In how many steps (chunks) do you want to process the data (for debugging)
todisklogical. If TRUE processing operates as if the dataset is very large and needs to be written to a temporary file (for debugging).

See Also

see writeCDF for writing NetCDF files.

Examples

Run this code
r <- rast(nrows=5, ncols=5, vals=1:25)

# create a temporary filename for the example
f <- file.path(tempdir(), "test.tif")

writeRaster(r, f, overwrite=TRUE)

writeRaster(r, f, overwrite=TRUE, gdal=c("COMPRESS=NONE", "TFW=YES"), datatype='INT1U')

## Or with a wopt argument:

writeRaster(r, f, overwrite=TRUE, wopt= list(gdal=c("COMPRESS=NONE"), datatype='INT1U'))

## remove the file
unlink(f)

Run the code above in your browser using DataLab