| writeRaster,GRaster,character-method | R Documentation |
This function saves a GRaster to disk directly from a GRASS session. It is faster than using rast(), then saving the output of that to disk (because rast() actually save the raster to disk, anyway).
The function will attempt to ascertain the file type to be ascertained from the file extension, but you can specify the format using the format argument (see entry for ...). You can see a list of supported formats by simply using this function with no arguments, as in writeRaster(), or by consulting the online help page for the GRASS module r.out.gdal (see grassHelp("r.out.gdal")). Only the GeoTIFF file format is guaranteed to work for multi-layered rasters.
The function will attempt to optimize the datatype argument, but this can take a long time. You can speed this up by setting datatype manually. Note that if you are saving a "stack" of GRasters with different datatypes, the one with the highest information density will be used (e.g., low-bit integer < high-bit integer < floating-point < double-floating point). This can make rasters with lower datatypes much larger on disk. In these cases, it make be best to save rasters with similar datatypes together.
## S4 method for signature 'GRaster,character'
writeRaster(
x,
filename,
overwrite = FALSE,
datatype = NULL,
byLayer = FALSE,
names = TRUE,
levelsExt = NULL,
compress = "LZW",
warn = TRUE,
...
)
## S4 method for signature 'missing,missing'
writeRaster(x, filename)
x |
A | |||||||||||||||||||||||||||||||||||||||||
filename |
Character: Path and file name. | |||||||||||||||||||||||||||||||||||||||||
overwrite |
Logical: If | |||||||||||||||||||||||||||||||||||||||||
datatype |
| |||||||||||||||||||||||||||||||||||||||||
byLayer |
Logical: If | |||||||||||||||||||||||||||||||||||||||||
names |
Logical: If | |||||||||||||||||||||||||||||||||||||||||
levelsExt |
Character, logical, or
| |||||||||||||||||||||||||||||||||||||||||
compress |
Character: Type of compression to use for GeoTIFF files:
| |||||||||||||||||||||||||||||||||||||||||
warn |
Logical: If | |||||||||||||||||||||||||||||||||||||||||
... |
Additional arguments. These can include:
|
A GRaster (invisibly). A raster is also saved to disk.
terra::writeRaster(), GRASS module r.out.gdal (see grassHelp("r.out.gdal"))
if (grassStarted()) {
# Setup
library(terra)
# Example data
madElev <- fastData("madElev")
madChelsa <- fastData("madChelsa")
### What raster formats can we attempt to write?
writeRaster()
### Save GRaster to disk (using temporary file)
elev <- fast(madElev)
filename <- tempfile(fileext = ".tif")
writeRaster(elev, filename)
# Load raster from disk
elev2 <- fast(filename)
elev2
### Save multi-layer GRaster to disk in one file (using temporary file)
chelsa <- fast(madChelsa)
filename <- tempfile(fileext = ".tif")
writeRaster(chelsa, filename)
# Load raster from disk
chelsa2 <- fast(filename)
chelsa2
### Save multi-layer GRaster to disk layer-by-layer (using temporary file)
chelsa <- fast(madChelsa)
filename <- tempfile(fileext = ".tif")
writeRaster(chelsa, filename, byLayer = TRUE)
# Load one of the rasters from disk
filename2 <- sub(filename, pattern = ".tif", replacement = "_bio1.tif")
chelsaBio1 <- fast(filename2)
chelsaBio1
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.