% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/grid_io.R
\name{maxent_grid_from_terra}
\alias{maxent_grid_from_terra}
\title{Convert a terra SpatRaster to a maxentcpp GridFloat}
\usage{
maxent_grid_from_terra(r, name = NULL)
}
\arguments{
\item{r}{A single-layer \code{terra::SpatRaster} object.}

\item{name}{Character: grid name (default: layer name from the raster).}
}
\value{
External pointer to a GridFloat C++ object.
}
\description{
Converts a single-layer \code{terra::SpatRaster} into a maxentcpp
GridFloat external pointer. The raster must have exactly one layer and
square cells (equal x and y resolution).
}
\details{
\code{NA} values in the raster are stored as NODATA (sentinel \code{-9999}).

Requires the \pkg{terra} package (listed in Suggests).

For the \pkg{raster} package, an equivalent workflow is:
\preformatted{
  library(raster)
  r <- raster("bio1.tif")
  mat <- as.matrix(r)
  e <- extent(r)
  g <- maxent_grid_from_matrix(mat, xll = e@xmin, yll = e@ymin,
                               cellsize = res(r)[1],
                               name = names(r))
}
}
\examples{
\donttest{
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]])
maxent_grid_info(g)
}
}
