Skip to contents

Creates a terra::SpatRaster from a maxentcpp GridFloat external pointer.

Usage

maxent_grid_to_terra(grid, crs = "EPSG:4326")

Arguments

grid

External pointer to a GridFloat C++ object.

crs

Character: coordinate reference system string (default "EPSG:4326", i.e. WGS 84 longitude/latitude).

Value

A single-layer terra::SpatRaster.

Details

Requires the terra package (listed in Suggests).

For the raster package, an equivalent workflow is:


  library(raster)
  info <- maxent_grid_info(grid)
  mat  <- maxent_grid_to_matrix(grid)
  r <- raster(mat,
              xmn = info$xll,
              xmx = info$xll + info$ncols * info$cellsize,
              ymn = info$yll,
              ymx = info$yll + info$nrows * info$cellsize,
              crs = "+proj=longlat +datum=WGS84")

Examples

# \donttest{
if (requireNamespace("terra", quietly = TRUE)) {
  stack_path <- system.file("extdata", "stack_1_12_crop.rds",
                           package = "maxentcpp")
  r <- terra::unwrap(readRDS(stack_path))
  g <- maxent_grid_from_terra(r[[1]])
  r2 <- maxent_grid_to_terra(g)
}
# }