Skip to contents

Warps one pre-fetched source window into a destination window with rwarp's kernels: the step warp_exec() runs for each chunk of a plan, for a buffer read some other way (a byte-reference fetch, a cache, an array in memory). Read the window warp_plan() gives for the destination, with the plan's resample, so the kernels have the pixels they need at its edges.

Usage

warp_buffer(
  src,
  src_crs,
  src_gt,
  dst_crs,
  dst_gt,
  dst_size,
  src_off = c(0L, 0L),
  src_size = NULL,
  src_dim = NULL,
  dst_off = c(0L, 0L),
  resample = "near",
  scaling = c("widen", "point", "fixed"),
  scale = NULL,
  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")
)

Arguments

src

The source window: integer, double or raw; a vector (with src_size), a matrix or an array. See Details.

src_crs, src_gt

CRS and geotransform of the full source raster.

dst_crs, dst_gt

CRS and geotransform of the full destination grid.

dst_size

Integer c(ncol, nrow) of the destination window.

src_off

Integer c(col_off, row_off) of the window in the full source raster (0-based), as src_xoff, src_yoff in a plan's chunk. Default c(0L, 0L).

src_size

Integer c(ncol, nrow) of the window, for a vector src. Taken from dim(src) for a matrix or array.

src_dim

Integer c(ncol, nrow) of the full source raster. With it, source longitudes wrap as gdalwarp wraps them (CENTER_LONG), which a source across 180 degrees needs. NULL (the default) skips that.

dst_off

Integer c(col_off, row_off) of the destination window in the full destination grid (0-based). Default c(0L, 0L).

resample

Resampling method, gdalwarp's -r names (see warp_plan()). Default "near".

scaling, scale

How the kernel widens when downsampling (see warp_plan()). "widen" (the default) estimates the scale over the destination window, as gdalwarp does per chunk, so two windows of one grid can widen differently. For a chunk of a plan that is its scale_x, scale_y; scaling = "fixed", scale = c(ch$scale_x, ch$scale_y) uses them as given.

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.

Value

As warp_exec(): a matrix c(nrow, ncol) for one band, an array c(nrow, ncol, nbands) for more (row 1 at the top), or a nativeRaster with output = "nara".

Details

src is laid out as read_fn returns it to warp_exec(): one vector, each band row-major (as GDALRaster$read() gives it), bands one after another, with src_size giving the window's c(ncol, nrow). A matrix c(nrow, ncol) or array c(nrow, ncol, nbands) in R orientation, as warp_exec() and warp_buffer() return, also works; its dim gives the size. The data type follows src: integer warps as 32-bit integers, double as 64-bit floats, raw as bytes, and the output is the same type.

See also

warp_plan() for the window to read, warp_exec() to run a whole plan with a read function.

Examples

## a 4 x 4 longitude/latitude source warped onto a 2 x 2 grid
gt  <- c(0, 1, 0, 4, 0, -1)
src <- as.double(1:16)              # row-major, as GDAL reads it
warp_buffer(src, "EPSG:4326", gt, "EPSG:4326", c(0, 2, 0, 4, 0, -2),
            dst_size = c(2L, 2L), src_size = c(4L, 4L), resample = "average")
#>      [,1] [,2]
#> [1,]  3.5  5.5
#> [2,] 11.5 13.5

if (FALSE) { # \dontrun{
## the window a plan asks for, read with gdalraster
library(gdalraster)
ds   <- new(GDALRaster, "/vsicurl/https://example.com/my.tif")
plan <- warp_plan(src_crs, src_gt, src_dim, dst_crs, dst_gt,
                  dst_size = dst_dim, resample = "cubic")
ch   <- plan[[1]][[1]]
v    <- ds$read(1L, ch$src_xoff, ch$src_yoff, ch$src_xsize, ch$src_ysize,
                ch$src_xsize, ch$src_ysize)
warp_buffer(v, src_crs, src_gt, dst_crs, dst_gt, dst_size = dst_dim,
            src_off = c(ch$src_xoff, ch$src_yoff),
            src_size = c(ch$src_xsize, ch$src_ysize),
            src_dim = src_dim, resample = "cubic")
} # }