% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/spatial_more.R
\name{polygonize}
\alias{polygonize}
\title{Vectorise a raster into polygons}
\usage{
polygonize(
  x,
  band = 1L,
  dissolve = TRUE,
  na_rm = TRUE,
  values = "value",
  crs = NA,
  flush_rows = NULL
)
}
\arguments{
\item{x}{A \code{vectra_raster} (from \code{\link[=vec_open_raster]{vec_open_raster()}}) or a path to a \code{.vec}
raster.}

\item{band}{Band to vectorise (1-based). Default 1.}

\item{dissolve}{If \code{TRUE} (default) merge cells of equal value into one polygon
per value; if \code{FALSE} emit one square polygon per cell.}

\item{na_rm}{Drop nodata cells (\code{TRUE}, default) or vectorise them as a value.}

\item{values}{Name of the output value column. Default \code{"value"}.}

\item{crs}{Coordinate reference system recorded on the node. Defaults to the
raster's EPSG, else unknown.}

\item{flush_rows}{Rows buffered before a spill flush. Defaults to
\code{getOption("vectra.spatial_flush", 5e5)}.}
}
\value{
A \code{vectra_node} with the value column and a hex-WKB \code{geometry} column,
materialise it with \code{\link[=collect_sf]{collect_sf()}}.
}
\description{
Converts a \code{.vec} raster into polygon features, the inverse of \code{\link[=rasterize]{rasterize()}}.
The raster is read one tile-row strip at a time; within each strip equal-valued
cells along a row collapse to a rectangle, and (with \code{dissolve = TRUE}) the
rectangles are then merged by value through \code{\link[=spatial_dissolve]{spatial_dissolve()}} so each
distinct value becomes a single polygon spanning the whole raster. The result
is a lazy \code{vectra_node} carrying a value column and hex-WKB geometry.
}
\details{
Extraction is the \emph{monoid fold} tier (one strip at a time); the by-value
dissolve rides the \emph{sort / partition} tier of \code{\link[=spatial_dissolve]{spatial_dissolve()}}, localising
each value to a shard before the union. Geometry assembly is delegated to
\pkg{sf} (an optional dependency).
}
\examples{
\dontshow{if (requireNamespace("sf", quietly = TRUE)) withAutoprint(\{ # examplesIf}
m <- matrix(c(1, 1, 2, 2, 1, 1, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3), 4, 4,
            byrow = TRUE)
f <- tempfile(fileext = ".vec")
vec_write_raster(m, f, dtype = "f64", extent = c(0, 0, 4, 4))

polys <- polygonize(f)
collect_sf(polys)
unlink(f)
\dontshow{\}) # examplesIf}
}
\seealso{
\code{\link[=rasterize]{rasterize()}} for the inverse, \code{\link[=contours]{contours()}} for iso-lines,
\code{\link[=collect_sf]{collect_sf()}} to materialise as \code{sf}.
}
