Skip to contents

ck_read() is ck_warp() that hands back pixels instead of writing a file: it streams only the tiles the request touches, warps them with GDAL (the same engine, sanitiser and defaults as ck_warp()), and returns the result as a base-R matrix or array. Use it for quick extraction and inspection without round-tripping through a file on disk.

Usage

ck_read(
  src,
  t_srs = NULL,
  te = NULL,
  te_srs = NULL,
  tr = NULL,
  ts = NULL,
  tap = TRUE,
  r = c("near", "bilinear", "cubic", "cubicspline", "lanczos", "average", "rms", "mode",
    "max", "min", "med", "q1", "q3", "sum"),
  bands = NULL,
  cl_arg = character(0),
  num_threads = "ALL_CPUS",
  warp_memory = "auto",
  cache_max = "auto",
  config = NULL,
  skip_nosource = TRUE,
  overview = NULL,
  margin = 8L,
  io_concurrency = 16L,
  max_bytes = NULL,
  sanitise = TRUE
)

Arguments

src

Path/URL to a source GeoTIFF / COG, a cog_source() handle, or a character vector of several sources to mosaic (each tile's overlapping window is fetched and the set is reprojected/mosaicked in one warp; non-overlapping tiles are skipped). Passing a cog_source() reuses its already-open handle, skipping the metadata re-read – worthwhile when warping many AOIs from the same source.

t_srs

Target SRS (e.g. "EPSG:3857"). If NULL, the source CRS is used (no reprojection).

te

Target extent c(xmin, ymin, xmax, ymax), in te_srs if given, otherwise in t_srs. When supplied this is the AOI that drives the fetch.

te_srs

SRS of te (defaults to t_srs).

tr

Target resolution c(xres, yres) in target CRS units. Takes precedence over ts: if both are given, ts is ignored (with a warning).

ts

Target size c(width, height) in pixels. Ignored if tr is also supplied.

tap

If TRUE (default) and tr is given, align output pixel boundaries to the tr grid (gdalwarp -tap), anchored at the CRS origin, so outputs at the same resolution share a grid and stack cleanly. This snaps the output extent outward, so it is no longer exactly te. No effect with ts or on the copy path. An explicit -tap in cl_arg is always honoured. (warp_remote() never adds this; pass -tap yourself.)

r

Resampling method, one of "near" (default), "bilinear", "cubic", "cubicspline", "lanczos", "average", "rms", "mode", "max", "min", "med", "q1", "q3", "sum". Matched with rlang::arg_match(), so a typo reports the valid set. (A method added by a newer GDAL than this list knows can still be passed via cl_arg.)

bands

1-based source bands to read (default: all). Subsetting happens during the fetch, so only those bands' bytes are streamed.

cl_arg

Character vector of extra raw gdalwarp flags, forwarded verbatim to gdalraster::warp() (e.g. c("-et", "0")). These are merged with the flags cptkirk builds from the named arguments above.

num_threads

Value for GDAL's warp NUM_THREADS warp option and the GDAL_NUM_THREADS config (default "ALL_CPUS"), parallelising the resampling computation and GeoTIFF (de)compression. NULL sets neither, deferring to the ambient GDAL_NUM_THREADS (env / session).

warp_memory

Warp working memory in MB (GDAL -wm). "auto" (default) uses ~25% of system RAM, clamped to 256-2048 MB (bigger means fewer, larger chunks); NULL defers to GDAL's own default; a number sets it explicitly.

cache_max

GDAL block cache in MB for this call. "auto" (default) uses ~25% of RAM, clamped to 256-2000 MB; NULL defers to the ambient GDAL_CACHEMAX (env / gdalraster::set_config_option()); a number sets it explicitly. Any value is restored to the previous setting afterwards.

config

Named character vector / list of extra GDAL config options to set for the duration of the call (restored on exit).

skip_nosource

If TRUE (default), pass SKIP_NOSOURCE=YES + INIT_DEST so the warper skips output chunks with no source coverage (e.g. nodata margins from reprojection). No effect when output is fully covered. Ignored on the copy fast-path.

overview

Force a 1-based IFD/overview level instead of auto-selecting from the output resolution. 1 = full resolution.

margin

Source-pixel margin added around the computed window to cover the resampling kernel and reprojection slop (default 8).

io_concurrency

Number of concurrent tile reads – the width of the single global fetch pool shared across all source tiles. Default 16, which suits object stores that throttle around that many simultaneous range requests (e.g. S3 / source.coop). Raise (24-32) on a fast, stable link; lower if a store rate-limits.

max_bytes

Safety ceiling (bytes) on the returned array, sized as nrow * ncol * nbands * 8 (R stores numerics as f64). NULL (default) uses ~1/3 of system RAM. Coarsen tr/ts, narrow te, select fewer bands, or raise this to read a larger window.

sanitise

If TRUE (default), validate the warp arguments against a tiny metadata-derived stand-in before fetching, so a bad CRS, resampling method, creation option or unknown flag fails in milliseconds instead of after a remote read. Set FALSE to skip the check.

Value

A numeric matrix (single band) or array with dimensions [nrow, ncol, nband] (multi-band), carrying geotransform, crs and (when set) nodata attributes so it is self-describing and convertible to other raster classes. The R type follows the output data type (integer or double).

Details

Two internal paths, chosen automatically:

  • Warp (the usual case – a reprojection / resolution change is requested): the result is materialised in an uncompressed /vsimem GeoTIFF and read back with gdalraster.

  • Native window (no t_srs/tr/ts, single source): the fetched native bytes are decoded straight to an R array, with no GDAL round-trip at all.

See also

ck_warp() to write a warped file instead.