Skip to contents

Computes all source windows required to fill a destination grid window, accounting for antimeridian splits and minimum fill ratios. The returned plan can be inspected before any I/O is performed, and the execution step warp_exec() accepts any read function — GDAL, byte-ref COG tiles, Zarr, etc.

Usage

warp_plan(
  src_crs,
  src_gt,
  src_dim,
  dst_crs,
  dst_gt,
  dst_off = c(0L, 0L),
  dst_size,
  resample_padding = NULL,
  min_fill_ratio = 0,
  min_dst_size = 1L,
  resample = "near",
  scaling = c("widen", "point", "fixed"),
  scale = NULL,
  split_antimeridian = TRUE,
  nbands = 1L,
  working_type = "Byte",
  src_nodata = FALSE,
  dst_nodata = src_nodata,
  memory_limit = 64,
  scale_origin = c("chunk", "output")
)

Arguments

src_crs, src_gt, src_dim

CRS, geotransform, and c(ncol, nrow) of the source raster.

dst_crs, dst_gt

CRS and geotransform of the destination raster.

dst_off

c(col_off, row_off) origin of the destination window (0-based). Default c(0L, 0L).

dst_size

c(ncol, nrow) of the destination window. Sizes and offsets are whole numbers, integer or double (as GDALRaster$getRasterXSize() gives them).

resample_padding

Extra source pixels added to every side of each source window (gdalwarp's -wo SOURCE_EXTRA). NULL (the default) is gdalwarp's rule: none, or 10 when a sample point fails to transform. The kernel's own radius is always added, from resample.

min_fill_ratio

Split a chunk with fill above 0 and below this fraction. Default 0 (never split on fill).

min_dst_size

Only split a chunk with a side longer than this, in pixels. Default 1L.

resample

Resampling method the plan is for: one of "near", "bilinear", "cubic", "cubicspline", "lanczos", "average", "mode", "min", "max", "med", "rms" (gdalwarp's -r names). Source windows are padded by the kernel's radius, times the kernel scale when downsampling. warp_exec() uses it by default.

scaling

How the kernel is widened when downsampling: "widen" (gdalwarp's default: by the destination/source scale, estimated per chunk), "point" (never: gdalwarp -wo XSCALE=1 -wo YSCALE=1) or "fixed" (by scale, as gdalwarp -wo XSCALE= -wo YSCALE=).

scale

With scaling = "fixed", c(x, y): destination pixels per source pixel (below 1 is downsampling).

split_antimeridian

When a chunk's source window spans the whole source width (gdalwarp's 90% rule, as when the destination crosses the antimeridian of a global longitude/latitude source), split the chunk where source x jumps, into compact windows either side. That reads the two edges of the source instead of a full-width strip (the few columns at the jump read a small window at each edge). gdalwarp does not split. Default TRUE; FALSE is gdalwarp's plan.

nbands, working_type

The number of bands and the data type the warp works in (gdalwarp's -wt; by default GDAL takes the widest of the source and destination types): one of "Byte", "Int8", "UInt16", "Int16", "UInt32", "Int32", "UInt64", "Int64", "Float16", "Float32", "Float64". With src_nodata, dst_nodata and memory_limit they set how much working memory a chunk takes, as gdalwarp counts it, and so where the destination splits. Default 1L and "Byte". warp_exec() takes nbands from the plan by default.

src_nodata, dst_nodata

Whether the source and destination have nodata, which gives each band a validity mask in the working memory: TRUE or FALSE, or the value given to warp_exec() (NULL is none, any value is nodata). dst_nodata defaults to src_nodata, as gdalwarp gives the destination the source's nodata. Default FALSE.

memory_limit

Working memory a chunk may take before it is split, in MB (gdalwarp's -wm). Default 64.

scale_origin

Where each chunk's kernel scale is estimated: "chunk" (the default) over the chunk itself, "output" as gdalwarp does, over a window of the chunk's size at the destination's top-left corner. It matters only when the destination splits.

Value

A list holding one chunk list, itself a list of chunk objects with fields: dst_xoff, dst_yoff, dst_xsize, dst_ysize, src_xoff, src_yoff, src_xsize, src_ysize, fill_ratio, scale_x, scale_y (the kernel scale for the chunk). Attributes src_dim, dst_off, dst_size, nbands, resample and scaling record what it was planned for.

fill_ratio is GDAL's: the area of the source window (whole pixels, clamped to the source) over the area of the unrounded extent the destination needs, both padded for the kernel. A low value means much of that extent lies outside the source. It can be a little above 1, because the window is rounded out to whole pixels, and further above 1 when GDAL's rule takes the whole source width or height for a window that needs more than 90% of it (1.06 for a whole-world Mercator destination from a global longitude/latitude source, which needs 170 of 180 degrees of latitude). Returns NULL when no valid chunks are found (source does not overlap the destination window).

Planning as gdalwarp does

The chunks matter to the output, not only to memory: with scaling = "widen" each chunk widens the kernel by its own estimate. To get gdalwarp's chunks, and so its values, describe the warp as gdalwarp sees it and use its rules: nbands, working_type, src_nodata and memory_limit as for the gdalwarp call, min_fill_ratio = 0.5, min_dst_size = 100L, scale_origin = "output" and split_antimeridian = FALSE. The defaults instead plan for one byte per pixel, never split on fill ratio and estimate each chunk's scale over the chunk.

Examples

## a global 0.01 degree source onto one 512 x 512 Web Mercator tile:
## geometry only, nothing is read
src_gt <- c(-180, 0.01, 0, 90, 0, -0.01)
w      <- 20037508.34
dst_gt <- c(-w, 2 * w / 512, 0, w, 0, -2 * w / 512)

plan <- warp_plan(
  src_crs  = "EPSG:4326",
  src_gt   = src_gt,
  src_dim  = c(36000L, 18000L),
  dst_crs  = "EPSG:3857",
  dst_gt   = dst_gt,
  dst_size = c(512L, 512L)
)
length(plan[[1]])          # chunks
#> [1] 12
str(plan[[1]][[1]])        # source window, fill ratio, kernel scale
#> List of 11
#>  $ dst_xoff  : int 0
#>  $ dst_yoff  : int 0
#>  $ dst_xsize : int 256
#>  $ dst_ysize : int 128
#>  $ src_xoff  : int 0
#>  $ src_yoff  : int 494
#>  $ src_xsize : int 18000
#>  $ src_ysize : int 1855
#>  $ fill_ratio: num 1
#>  $ scale_x   : num 0.0142
#>  $ scale_y   : num 0.0851

## as gdalwarp would plan it for three Float32 bands with nodata
plan_g <- warp_plan("EPSG:4326", src_gt, c(36000L, 18000L), "EPSG:3857",
                    dst_gt, dst_size = c(512L, 512L), nbands = 3L,
                    working_type = "Float32", src_nodata = TRUE,
                    min_fill_ratio = 0.5, min_dst_size = 100L,
                    scale_origin = "output", split_antimeridian = FALSE)
length(plan_g[[1]])
#> [1] 152