knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(vaster)
The vaster package provides pure raster grid logic without requiring actual raster data. It separates the abstract mathematical operations of raster grids—dimension, extent, cell indexing, coordinate transformations—from data formats and file I/O.
This is useful when you need to:
A raster grid in vaster is defined by just two components:
c(ncol, nrow)c(xmin, xmax, ymin, ymax)## a 10x5 grid covering [0, 100] x [0, 50] dimension <- c(10, 5) extent <- c(0, 100, 0, 50)
From these two pieces of information, all grid properties can be derived:
n_cell(dimension) x_res(dimension, extent) y_res(dimension, extent)
There is no data, no file format, no CRS—just the logical grid structure. This matches how GDAL and other tools internally represent grids.
Cells are numbered from 1, starting at the top-left corner and proceeding row-wise (left-to-right, top-to-bottom). This matches the convention used by the raster and terra packages.
## cell numbers for a 4x3 grid dimension <- c(4, 3) extent <- c(0, 4, 0, 3) ## total cells n_cell(dimension) ## cell 1 is top-left xy_from_cell(dimension, extent, 1) ## cell 4 is top-right xy_from_cell(dimension, extent, 4) ## cell 12 is bottom-right xy_from_cell(dimension, extent, 12)
The cell centre coordinates reflect this ordering:
## all cell centres cells <- seq_len(n_cell(dimension)) xy_from_cell(dimension, extent, cells)
Resolution is the cell size in x and y directions:
dimension <- c(10, 5) extent <- c(0, 100, 0, 50) x_res(dimension, extent) y_res(dimension, extent)
The origin is the point where cell boundaries align. By default, vaster uses the bottom-left corner of the extent:
origin(dimension, extent)
Understanding the origin is important for aligning multiple grids—covered in detail in vignette("extent-alignment").
Extract individual extent components, dimension is obviously irrelevant for extent components but is included for consistency with other functions.
extent <- c(-180, 180, -90, 90) x_min(c(1, 1), extent) x_max(c(1, 1), extent) y_min(c(1, 1), extent) y_max(c(1, 1), extent) xlim(c(1, 1), extent) ylim(c(1, 1), extent)
Generate coordinate vectors for all columns or rows:
dimension <- c(4, 3) extent <- c(0, 4, 0, 3) ## x coordinates of cell centres (one per column) x_centre(dimension, extent) ## y coordinates of cell centres (one per row) y_centre(dimension, extent) ## x coordinates of cell corners/edges (ncol + 1 values) x_corner(dimension, extent) ## y coordinates of cell corners/edges (nrow + 1 values) y_corner(dimension, extent)
For a full matrix of all cell centre coordinates:
xy(dimension, extent)
The vaster approach differs from raster and terra in that there is no object to create or manage. Grid operations are pure functions:
## terra approach library(terra) r <- rast(ncol = 10, nrow = 5, xmin = 0, xmax = 100, ymin = 0, ymax = 50) res(r) ncell(r) xyFromCell(r, 1) ## vaster approach dimension <- c(10, 5) extent <- c(0, 100, 0, 50) c(x_res(dimension, extent), y_res(dimension, extent)) n_cell(dimension) xy_from_cell(dimension, extent, 1)
This functional approach is useful when you need to perform many grid calculations without the overhead of object creation, or when working in contexts where raster packages aren't available.
vignette("cell-operations"): Converting between cells, coordinates, and row/col indicesvignette("extent-alignment"): Aligning and cropping extents to gridsvignette("gdal-interop"): Working with GDAL geotransforms and RasterIOAny scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.