function to automatically re-cast between Raster object and raster files on disk

cast_rast(object, to)

# S3 method for default
cast_rast(object, to)

# S3 method for Raster
cast_rast(object, to)

# S3 method for character
cast_rast(object, to)

Arguments

object

either an R object of class sf, sfc or Spatial, or a character string corresponding to a filename (with full path)

to

character indicating to which type of object the input should be re-casted. It can be "rastobject" (re-cast to Raster) or "rastfile" (recast to raster file).In the second case, recasting is achieved by identifying the raster file associated with the Raster object and returning its path. If no file is associated (i.e., the object is in-memory), the Raster object is written to disk on a temporary file, which path is returned

Value

returns the same object, casted to the "class" specified by to (or the exact same object in case object is already of "class" to)

Details

If object is a valid R spatial object, it is automatically converted to an object of a different class if needed (e.g., from sf to Spatial and viceversa, or from sf to a vector file through sprawl::write_shape). If it is a character string, the function checks if it corresponds to a valid raster file and reads it to a Raster object through rasterr::stack()

Examples

#EXAMPLE1