The plan/execute split
A conventional warp pipeline reads source data and reprojects it in one step. rwarp separates these into two explicit phases:
-
warp_plan()— pure geometry; no I/O. Returns the source windows and destination sub-windows needed. Inspect, cache, or parallelise this. -
warp_exec()— the actual pixel work, driven by a user-suppliedread_fn(orwarp_buffer(), for a window you have already read).
This means you can:
- Inspect fill ratios before deciding whether to fetch data at all.
- Cache plans when the same destination grid is reused across many dates.
- Swap the reader between GDAL, HTTP byte-refs, Zarr, or in-memory buffers without touching the warp logic.
- Parallelise by distributing chunks across workers.
Anatomy of a plan
library(rwarp)
library(vaster)
## A 10-degree-per-pixel global source
src_gt <- c(-180, 10, 0, 90, 0, -10) # 36 x 18 grid
src_dim <- c(36L, 18L)
## A Web Mercator destination at ~156 km/pixel (zoom 1)
dst_gt <- c(-20037508, 156543, 0, 20037508, 0, -156543)
dst_dim <- c(256L, 256L)
plan <- warp_plan(
src_crs = "EPSG:4326", src_gt = src_gt, src_dim = src_dim,
dst_crs = "EPSG:3857", dst_gt = dst_gt,
dst_off = c(0L, 0L), dst_size = dst_dim
)
cat("Length of plan:", length(plan), "\n")
#> Length of plan: 1
cat("Chunks in first group:", length(plan[[1]]), "\n")
#> Chunks in first group: 1
ch <- plan[[1]][[1]]
cat("\nChunk fields:\n")
#>
#> Chunk fields:
str(ch)
#> List of 11
#> $ dst_xoff : int 0
#> $ dst_yoff : int 0
#> $ dst_xsize : int 256
#> $ dst_ysize : int 256
#> $ src_xoff : int 0
#> $ src_yoff : int 0
#> $ src_xsize : int 36
#> $ src_ysize : int 18
#> $ fill_ratio: num 1.06
#> $ scale_x : num 7.11
#> $ scale_y : num 38.8Fill ratio
The fill ratio tells you what fraction of the destination window has a valid source pixel. Values below 1 indicate that the source does not fully cover the destination.
## A high-latitude destination that only partially overlaps the source
## (global geographic source, destination tile near the North Pole in LAEA)
src_gt2 <- c(-180, 1, 0, 90, 0, -1)
src_dim2 <- c(360L, 180L)
dst_gt2 <- c(-500000, 5000, 0, 500000, 0, -5000) # 1000 km at North Pole
dst_dim2 <- c(200L, 200L)
plan2 <- warp_plan(
src_crs = "EPSG:4326",
src_gt = src_gt2,
src_dim = src_dim2,
dst_crs = "+proj=laea +lat_0=90 +datum=WGS84 +type=crs",
dst_gt = dst_gt2,
dst_off = c(0L, 0L), dst_size = dst_dim2
)
if (!is.null(plan2)) {
cat("Fill ratio:", plan2[[1]][[1]]$fill_ratio, "\n")
} else {
cat("No overlap (NULL plan)\n")
}
#> Fill ratio: 1.105106Antimeridian splitting
A destination across the antimeridian of a global longitude/latitude
source needs source pixels at both of its edges, 180 W and 180 E. One
window holding both is a strip the whole width of the source, which is
what gdalwarp reads. warp_plan() instead splits the chunk
where source x jumps, so each side reads a compact window at its own
edge (split_antimeridian = TRUE, the default;
FALSE is gdalwarp’s plan).
## Lambert azimuthal equal-area centred on 30 S, 180 E, 4000 km across
pac_crs <- "+proj=laea +lat_0=-30 +lon_0=180 +datum=WGS84 +type=crs"
pac_gt <- c(-2e6, 20000, 0, 2e6, 0, -20000)
glob_gt <- c(-180, 1, 0, 90, 0, -1)
plan3 <- warp_plan("EPSG:4326", glob_gt, c(360L, 180L), pac_crs, pac_gt,
dst_size = c(200L, 200L))
plan3_g <- warp_plan("EPSG:4326", glob_gt, c(360L, 180L), pac_crs, pac_gt,
dst_size = c(200L, 200L), split_antimeridian = FALSE)
windows <- function(p) do.call(rbind, lapply(p[[1]], as.data.frame))
w <- windows(plan3)
wg <- windows(plan3_g)
w[c("dst_xoff", "dst_xsize", "src_xoff", "src_yoff", "src_xsize", "src_ysize")]
#> dst_xoff dst_xsize src_xoff src_yoff src_xsize src_ysize
#> 1 0 99 334 100 26 39
#> 2 99 2 0 100 2 40
#> 3 99 2 358 100 2 40
#> 4 101 99 0 100 26 39
c(split = sum(w$src_xsize * w$src_ysize), one_window = sum(wg$src_xsize * wg$src_ysize))
#> split one_window
#> 2188 14040The two halves of the destination each read 26 columns at one edge. The two destination columns that straddle the jump are chunks of their own, reading two columns at each edge. Together that’s about a sixth of the pixels in the full-width strip:
lon <- function(x) glob_gt[1] + x * glob_gt[2]
lat <- function(y) glob_gt[4] + y * glob_gt[6]
box <- function(d, ...) {
rect(lon(d$src_xoff), lat(d$src_yoff + d$src_ysize),
lon(d$src_xoff + d$src_xsize), lat(d$src_yoff), ...)
}
op <- par(mar = c(2.5, 2.5, 2, 0.5))
plot(NA, xlim = c(-180, 180), ylim = c(-90, 90), asp = 1, xlab = "", ylab = "",
main = "What each plan reads from the global source")
rect(-180, -90, 180, 90, border = "grey60")
abline(v = c(-180, 180), col = "grey60")
box(wg, border = "grey30", lty = 2, lwd = 1.5)
box(w, col = adjustcolor("firebrick", 0.45), border = "firebrick")
legend("top", c("split (default)", "one window (gdalwarp)"), horiz = TRUE,
col = c("firebrick", "grey30"), lty = c(1, 2), lwd = c(8, 1.5), bty = "n")
par(op)Tiled execution loop
For large destinations, tile the output and call
warp_plan() + warp_exec() per tile. This caps
memory usage and enables parallel execution:
tile_size <- 512L
ncol_full <- 4000L; nrow_full <- 3000L
results <- vector("list", ceiling(ncol_full / tile_size) * ceiling(nrow_full / tile_size))
k <- 1L
for (tx in seq(0L, ncol_full - 1L, by = tile_size)) {
for (ty in seq(0L, nrow_full - 1L, by = tile_size)) {
tw <- min(tile_size, ncol_full - tx)
th <- min(tile_size, nrow_full - ty)
plan_tile <- warp_plan(
src_crs = src_crs, src_gt = src_gt, src_dim = as.integer(src_dim),
dst_crs = dst_crs, dst_gt = dst_gt,
dst_off = c(tx, ty),
dst_size = c(tw, th)
)
if (is.null(plan_tile)) next
tile_result <- warp_exec(
plan_tile, read_fn,
src_crs, src_gt, dst_crs, dst_gt,
dst_size = c(tw, th),
nodata = nodata
)
results[[k]] <- list(result = tile_result, xoff = tx, yoff = ty, w = tw, h = th)
k <- k + 1L
}
}