% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/hex_extract.R
\name{hex_extract}
\alias{hex_extract}
\title{Extract Raster Values at Hex Cell Centers}
\usage{
hex_extract(raster, grid, cells = NULL, boundary = NULL)
}
\arguments{
\item{raster}{A \code{terra::SpatRaster} object.}

\item{grid}{A HexGridInfo or HexData object specifying the grid.}

\item{cells}{Optional cell IDs to extract. If \code{NULL} (default), extracts
at all cell centers from \code{grid} (only works for HexData objects or
if a \code{boundary} is provided).}

\item{boundary}{Optional sf polygon to limit extraction extent.}
}
\value{
A data.frame with columns \code{cell_id}, plus one column per raster
layer.
}
\description{
Samples raster values at hexagonal cell centers. Faster than
\code{\link[=hex_zonal]{hex_zonal()}} because it only queries cell center points, not full
polygons.
}
\details{
Requires the \code{terra} package (in Suggests). The function:
\enumerate{
\item Generates cell centers for the raster extent
\item Calls \code{terra::extract(raster, cell_center_matrix)}
\item Attaches cell IDs
}

For full zonal statistics (aggregating all pixels within each hex polygon),
use \code{\link[=hex_zonal]{hex_zonal()}} instead.
}
\examples{
\donttest{
if (requireNamespace("terra", quietly = TRUE)) {
  # Create a small synthetic raster
  r <- terra::rast(nrows = 10, ncols = 10,
                   xmin = -10, xmax = 10, ymin = 40, ymax = 55)
  terra::values(r) <- runif(100)
  names(r) <- "temperature"

  # Extract at hex cell centers
  g <- hex_grid(area_km2 = 500)
  df <- data.frame(lon = c(0, 5), lat = c(45, 50))
  hd <- hexify(df, lon = "lon", lat = "lat", grid = g)
  hex_extract(r, hd)
}
}
}
\seealso{
\code{\link[=hex_zonal]{hex_zonal()}} for polygon-based zonal statistics,
\code{\link[=hexify]{hexify()}} for creating HexData objects
}
