save_mland: Saves a 'MultiLand' or 'MultiLandMetrics' object

View source: R/save_mland.R

save_mlandR Documentation

Saves a 'MultiLand' or 'MultiLandMetrics' object

Description

Exports an object of class 'MultiLand' to be read in the future with load_mland(), or an object of class 'MultiLandMetrics' as if it was saved with saveRDS().

Usage

save_mland(
  x,
  name = NULL,
  gdal = c("COMPRESS=DEFLATE", "PREDICTOR=2", "ZLEVEL=9"),
  ...
)

Arguments

x

Object of class 'MultiLand' or 'MultiLandMetrics'.

name

If x is an object of class 'MultiLand', the name of the zip file or directory where files will be saved, or the name of the R file if x is an object of class 'MultiLandMetrics'. If NULL (default), the name will be 'mland_' or 'mlandmetrics_' + a large random number.

gdal

GDAL driver specific datasource creation options. See the GDAL documentation. With the GeoTiff file format, save_mland() uses the following compression options: c("COMPRESS=DEFLATE", "PREDICTOR=2", "ZLEVEL=9"). Only relevant if x is an object of class 'MultiLand'.

...

If x is an object of class 'MultiLand', ... should depict other arguments passed to terra::writeRaster, the function to write rasterlayers (from intersections and plain rasterlayers). Otherwise, if x is an object of class 'MultiLandMetrics', ... should depict other arguments passed to save(). See Details.

Details

'MultiLand' objects should be exported with this function rather than exporting as an external representation of R objects with saveRDS(). This is because objects of classes 'SpatVector' and 'SpatRaster' (from package terra) contained inside a 'MultiLand' object cannot be exported as regular R objects. The exported object will be a zip file, and can be loaded again into an R session with load_mland().

Relevant arguments can be passed to the function terra::writeRaster, which is used to write rasterlayers from a 'MultiLand' object. Particularly, in the argument gdal one can specify relevant options regarding raster compression. This may reduce raster sizes significantly. Definition of some other arguments inside terra::writeRaster may affect exportation of rasterlayer objects, in the context of a 'MultiLand' object.

Objects of class 'MultiLandMetrics', instead, do not contain 'SpatVector' or 'SpatRaster' objects and can be exported as regular R objects with saveRDS(). The user may use saveRDS() or save_mland(), and the outcome will be identical.

Value

If x is an object of class 'MultiLand', a zip file or a directory containing all information regarding the 'MultiLand' object provided in 'x'. Otherwise, if x is an object of class 'MultiLandMetrics', the function will export the R object as if it was exported as a regular R object with saveRDS().

See Also

load_mland(), mland(), metrics()

Examples

## Not run: 
# Load MultiLand object
mland_obj <- system.file("extdata", "ernesdesign.zip", package = "multilandr")
ernesdesign2 <- load_mland(mland_obj)

# Save it again
save_mland(ernesdesign2)

# Save it again but defining a higher compression for rasterlayers
save_mland(ernesdesign2, gdal = "COMPRESS=DEFLATE")

# Loads a MultiLandMetrics object previously generated with metrics()
mlm_obj <- system.file("extdata", "ed_metrics.rds", package = "multilandr")
ed_metrics2 <- load_mland(mlm_obj)

# Save it again. In this case, save_mland() is the same as using saveRDS()
save_mland(ed_metrics2)

## End(Not run)

phuais/multilandR documentation built on Feb. 11, 2024, 9:27 p.m.