Skip to contents

Implements GDAL's GenImgProjTransform: for every cell centre of the destination grid, compute the corresponding fractional (sub-pixel) position in the source raster via the three-step pipeline dst pixel -> dst geo -> PROJ -> src geo -> src pixel.

Usage

pixel_transform(src_crs, src_gt, dst_crs, dst_gt, dst_dim)

Arguments

src_crs

CRS of the source raster (WKT, EPSG code, PROJ string with +type=crs, ...).

src_gt

Geotransform of the source raster: a 6-element numeric vector c(xmin, xres, 0, ymax, 0, -yres) (north-up convention, as returned by GDALRaster$getGeoTransform() or vaster::extent_dim_to_gt()).

dst_crs

CRS of the destination raster.

dst_gt

Geotransform of the destination raster.

dst_dim

c(ncol, nrow) of the destination grid: whole numbers, integer or double.

Value

A list with numeric vectors src_x and src_y of length prod(dst_dim), in row-major order. NA where the destination point transforms to outside the PROJ domain or behind the horizon.

Details

This is the low-level building block used by warp_plan() and warp_resample(). It is exposed here so you can inspect the coordinate mapping directly — useful for debugging CRS strings, checking extent coverage, and understanding fill ratios.

Examples

## a global 0.1 degree source and a 256 x 256 Web Mercator grid
src_gt <- c(-180, 0.1, 0, 90, 0, -0.1)
w      <- 20037508.34
dst_gt <- c(-w, 2 * w / 256, 0, w, 0, -2 * w / 256)

coords <- pixel_transform(
  src_crs = "EPSG:4326", src_gt = src_gt,
  dst_crs = "EPSG:3857", dst_gt = dst_gt,
  dst_dim = c(256L, 256L)
)
# Proportion of destination pixels with valid source coordinates
mean(!is.na(coords$src_x))
#> [1] 1
# the source row each destination row samples: Mercator stretches the
# poles, so rows near the top and bottom sample far apart in latitude
plot(coords$src_y[seq(1, by = 256, length.out = 256)], type = "l",
     xlab = "destination row", ylab = "source row")