Skip to contents

Runs the chunk plan returned by warp_plan(): reads each chunk's source window with read_fn, warps it with rwarp's kernels, and places it in the output. The loop runs in Rust and calls read_fn once per chunk.

Usage

warp_exec(
  plan,
  read_fn,
  src_crs,
  src_gt,
  dst_crs,
  dst_gt,
  dst_size = attr(plan, "dst_size"),
  nbands = attr(plan, "nbands"),
  resample = attr(plan, "resample"),
  src_nodata = NA,
  dst_nodata = NA,
  band_nodata = c("partial", "unified", "per_band"),
  mask_band = NULL,
  mode_ties = c("first", "min", "max"),
  max_error = 0.125,
  output = c("array", "nara"),
  verbose = FALSE,
  nodata = NULL
)

Arguments

plan

Chunk plan as returned by warp_plan(). A chunk without scale_x/scale_y (a plan made by hand) uses a kernel scale of 1.

read_fn

A function f(xoff, yoff, xsize, ysize) returning the source window as one vector of length xsize * ysize * nbands: each band row-major (as GDALRaster$read() gives it), bands one after another. Integer, double or raw. Can call GDAL, a byte-ref tile reader, a Zarr chunk fetcher, or anything else.

src_crs, src_gt

Source CRS and geotransform (as passed to warp_plan()).

dst_crs, dst_gt

Destination CRS and geotransform of the full grid (as passed to warp_plan()).

dst_size

Integer c(ncol, nrow) of the planned window. Default: the plan's.

nbands

Number of bands read_fn returns. Default: the plan's nbands (see warp_plan()), or 1L.

resample

Resampling method (see warp_plan()). Default: the method the plan was made for. Planning for one method and warping with a wider one can clip kernels at window edges.

src_nodata

Source nodata, one value or one per band. NA (the default) means R's NA for integer data; NaN is always nodata in double data; raw has none. NULL for none.

dst_nodata

Value written where no source contributes, one value or one per band. NA (the default) writes NA for integer data, NaN for double (is.na() is TRUE for both) and 0 for raw. NULL writes 0.

band_nodata

How nodata works across bands (gdalwarp UNIFIED_SRC_NODATA): "partial" (gdalwarp's default), "unified", or "per_band".

mask_band

A band (1-based) whose zeros mark source pixels invalid, such as an alpha band. NULL for none.

mode_ties

For resample = "mode", how ties break: "first", "min" or "max" (gdalwarp -wo MODE_TIES=).

max_error

Approx-transformer pixel error bound. Default 0.125 (gdalwarp's -et).

output

"array" (default): a matrix c(nrow, ncol) for one band, an array c(nrow, ncol, nbands) for more. "nara": a nativeRaster for raw data of 1 (grey), 3 (RGB) or 4 (RGBA) bands, ready for plot(), grid::grid.raster() or png::writePNG(). Without an alpha band, pixels whose bands all equal dst_nodata are transparent.

verbose

Print a line per chunk. Default FALSE.

nodata

Deprecated: sets both src_nodata and dst_nodata.

Value

See output. The matrix/array is in standard R orientation (row 1 at the top).

Details

The data type follows what read_fn returns: an integer vector warps as 32-bit integers, a double vector as 64-bit floats, a raw vector as bytes. The output is the same type.

Examples

## a smooth global field held in memory, warped to south polar
## stereographic: read_fn returns any window of it, row-major
src_gt <- c(-180, 1, 0, 90, 0, -1)
lon <- seq(-179.5, 179.5)
lat <- seq(89.5, -89.5)
z <- outer(lat, lon, function(la, lo)
  cos(la * pi / 180) * (1 + 0.3 * sin(3 * lo * pi / 180)))
read_fn <- function(xoff, yoff, xsize, ysize) {
  as.vector(t(z[yoff + seq_len(ysize), xoff + seq_len(xsize), drop = FALSE]))
}

dst_gt <- c(-5e6, 50000, 0, 5e6, 0, -50000)
plan <- warp_plan("EPSG:4326", src_gt, c(360L, 180L), "EPSG:3031", dst_gt,
                  dst_size = c(200L, 200L), resample = "bilinear")
x <- warp_exec(plan, read_fn, "EPSG:4326", src_gt, "EPSG:3031", dst_gt)
image(t(x[nrow(x):1, ]), asp = 1, axes = FALSE, col = hcl.colors(64))


if (FALSE) { # \dontrun{
## a remote COG, read with gdalraster
library(gdalraster)
library(vaster)

## Sentinel-2 Band 4 COG
url <- paste0(
  "/vsicurl/https://e84-earth-search-sentinel-data.s3.us-west-2.",
  "amazonaws.com/sentinel-2-c1-l2a/55/G/CM/2026/2/",
  "S2C_T55GCM_20260227T000650_L2A/B04.tif"
)
ds <- new(GDALRaster, url, read_only = TRUE)

src_crs <- ds$getProjectionRef()
src_gt  <- ds$getGeoTransform()
src_dim <- c(ds$getRasterXSize(), ds$getRasterYSize())

dst_crs  <- "EPSG:4326"
dst_ext  <- reproj::reproj_extent(ds$bbox()[c(1,3,2,4)], "EPSG:4326", source = src_crs)
dst_dim  <- c(512L, 512L)
dst_gt   <- extent_dim_to_gt(dst_ext, dst_dim)

read_fn <- function(xoff, yoff, xsize, ysize) {
  as.integer(ds$read(1L, xoff, yoff, xsize, ysize, xsize, ysize))
}

plan   <- warp_plan(src_crs, src_gt, src_dim, dst_crs, dst_gt,
                    dst_size = dst_dim, resample = "cubic")
result <- warp_exec(plan, read_fn, src_crs, src_gt, dst_crs, dst_gt)

ximage::ximage(result, dst_ext, col = grey.colors(256))
ds$close()
} # }