raster2: Create a Raster Object from Mask or Dsurface

rasterR Documentation

Create a Raster Object from Mask or Dsurface

Description

Methods to convert secr object to a RasterLayer object (package raster) or a SpatRaster object (package terra).

Usage


## S4 method for signature 'mask'
raster(x, covariate, values = 1, crs = NA)

## S4 method for signature 'Dsurface'
raster(x, covariate, values = 1, crs = NA)

## S4 method for signature 'mask'
rast(x, covariate, values = 1, crs = "")

## S4 method for signature 'Dsurface'
rast(x, covariate, values = 1, crs = "")

Arguments

x

mask or Dsurface object

covariate

character name of covariate to provide values for RasterLayer

values

numeric values for RasterLayer

crs

character or object of class CRS. Optional PROJ.4 type description of a Coordinate Reference System (map projection).

Details

There are two ways to specify the values to be used. If covariate is provided then the values of the corresponding covariate of the mask or Dsurface are used. Otherwise, values is duplicated to the required number of rows.

The resulting RasterLayer may optionally include a PROJ.4 map projection defined via crs. The specification may be very simple (as in the example below) or complex, including an explicit datum and other arguments. Projections are used by sf, terra, raster, sp and other packages. See raster for further explanation and links.

The S3 classes ‘mask’ and ‘Dsurface’ are defined in secr as virtual S4 classes. This enables these extensions to the list of S4 methods defined in raster and terra.

raster and terra are not loaded by secr (loading them is slow). The methods for raster and rast are registered when the package concerned is loaded, so call library(raster) or library(terra) (or raster::raster, terra::rast) before using them. It is also necessary to load the package to do much with the result (e.g., plot it).

Value

RasterLayer (raster)
SpatRaster (rast)

Note

Prior to secr 2.9.5 these methods could fail unpredictably because an intermediate array was badly dimensioned due to truncation of a floating point value.

The requirement to manually load raster and terra was added in secr 5.6.1.

See Also

raster, rast

Examples


## Not run: 

library(raster)
# Dsurface to Rasterlayer
shorePossums <- predictDsurface(possum.model.Ds)
tmp <- raster(shorePossums, covariate = "D.0")
plot(tmp, useRaster = FALSE)

## alternative with same result
tmp <- raster(shorePossums, values = covariates(shorePossums)$D.0)

## set the projection
## here the crs PROJ.4 spec refers simply to the old NZ metric grid
tmp <- raster(shorePossums, "D.0", crs = "+proj=nzmg")
## check the projection
proj4string(tmp)
     
library(terra)
# mask to SpatRaster
dtsrast <- rast(possummask, covariate = 'd.to.shore')
plot(dtsrast)

# Dsurface to SpatRaster
class(shorePossums) <- c('mask', 'data.frame') # or change "mask" to "Dsurface" above
Drast <- rast(shorePossums, covariate = 'D.0')
plot(Drast)


## End(Not run)


secr documentation built on Nov. 25, 2026, 9:06 a.m.