Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

writeRaster

Write raster data to a file


Description

Write a SpatRaster object to a file.

Usage

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

Arguments

x

SpatRaster

filename

character. Output filename

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:

name description
datatype values 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).
filetype file format expresses as GDAL driver names. If this argument is not supplied, the driver is derived from the filename.
gdal GDAL 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").
tempdir the path where temporary files are to be written to.
progress positive integer. If the number of chunks is larger, a progress bar is shown.
memfrac numeric between 0.1 and 0.9. The fraction of available RAM that terra is allowed to use.
names output layer names.
NAflag numeric. value to represent missing (NA or NaN) values. See note
verbose logical. If TRUE debugging information is printed.
todisk logical. If TRUE processing operates as if the dataset is very large and needs to be written to a temporary file (for debugging).

Value

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

Note

GeoTiff files are, by default, written with LZW compression. If you do not want compression, use gdal="COMPRESS=NONE".

When writing integer values the lowest available value (given the datatype) is used for signed types, and the highest value is used for unsigned values. This can be a problem with byte data (between 0 and 255) as the value 255 is reserved for NA. To keep the value 255, you need to set another value as NAflag, or do not set a NAflag (with NAflag=NA)

See Also

see writeCDF for writing NetCDF files.

Examples

library(terra)
r <- rast(nrow=5, ncol=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=LZW", "TFW=YES","of=COG"), datatype='INT1U')

## Or with a wopt argument:

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


## remove the file
unlink(f)

terra

Spatial Data Analysis

v1.2-10
GPL (>= 3)
Authors
Robert J. Hijmans [cre, aut] (<https://orcid.org/0000-0001-5872-2872>), Roger Bivand [ctb] (<https://orcid.org/0000-0003-2392-6140>), Karl Forner [ctb], Jeroen Ooms [ctb] (<https://orcid.org/0000-0002-4035-0289>), Edzer Pebesma [ctb] (<https://orcid.org/0000-0001-8049-7069>)
Initial release
2021-05-12

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.