Package 'vaster'

Title: Raster Grid Logic and Alignment
Description: Provides raster grid logic, operations that describe a discretized rectangular domain and do not require access to materialized data. Grids are arrays with dimension and extent, and many operations are functions of dimension only: number of columns, number of rows, or they are a combination of the dimension and the extent the range in x and the range in y in that order. Here we provide direct access to this logic without need for connection to any materialized data or formats. Grid logic includes functions that relate the cell index to row and column, or row and column to cell index, row, column or cell index to position. These methods are described in Loudon, Wheeler, Andrew (1980) <doi:10.1016/0098-3004(80)90015-1>, and implementations were in part derived from Hijmans (2024) <doi:10.32614/CRAN.package.terra>.
Authors: Michael Sumner [aut, cre, cph] (ORCID: <https://orcid.org/0000-0002-2471-7511>), Robert Hijmans [ctb] (Wrote original versions of abstract cell operations in raster package)
Maintainer: Michael Sumner <[email protected]>
License: MIT + file LICENSE
Version: 0.7.0
Built: 2026-10-15 23:18:00 UTC
Source: https://github.com/hypertidy/vaster

Help Index


vaster: Tools for Raster Grid Logic

Description

Provides raster grid logic without requiring materialized data. Most raster operations are purely functions of dimension (ncol, nrow) and extent (xmin, xmax, ymin, ymax) - vaster provides these operations as simple functions on numbers, independent of any file format or geospatial library.

Core concepts

A raster grid is defined by two properties:

  • dimension: c(ncol, nrow) - the number of columns and rows

  • extent: c(xmin, xmax, ymin, ymax) - the bounding box

From these six numbers, all grid geometry can be computed: cell indices, coordinates, resolutions, and spatial queries.

Cell indexing

Cells are numbered from 1, starting at the top-left corner, proceeding right then down (row-major order). This matches the convention used by the raster and terra packages.

Main function families

Cell operations (cells):

  • cell_from_xy(), cell_from_row_col(), cell_from_rowcol_combine() - cell index from position

  • cell_from_row(), cell_from_col() - all cells in a row or column

  • cell_from_extent(), extent_from_cell() - cell/extent conversion

  • xy_from_cell(), x_from_cell(), y_from_cell() - coordinates from cell

  • rowcol_from_cell(), row_from_cell(), col_from_cell() - row/column from cell

Coordinate operations (coordinates):

  • x_from_col(), y_from_row() - coordinate from index

  • col_from_x(), row_from_y() - index from coordinate

  • x_corner(), y_corner() - corner coordinates of all cells

  • x_centre(), y_centre() - centre coordinates of all cells

  • xy() - centre coordinates of all cells as a matrix

Grid properties (grid):

  • x_res(), y_res() - cell resolution

  • n_cell(), n_row(), n_col() - counts

  • xlim(), ylim() - extent as 2-element ranges

  • x_min(), x_max(), y_min(), y_max() - individual extent edges

  • origin() - grid origin (alignment anchor)

Grid modification:

  • vcrop() - crop or extend a grid, snapped to alignment

  • align_extent() - snap an extent to grid origin

  • extent_dimension() - dimension for an aligned extent

  • intersect_extent() - overlapping extent of two grids

  • snap_extent() / buffer_extent() - snap extent to resolution

Cell adjacency (adjacency):

  • adjacency() - neighbour cell indices (queen, rook, or bishop)

GDAL interoperability:

  • geo_transform0(), geo_world0() - create geotransform / world file vectors

  • world_to_geotransform(), geotransform_to_world() - convert between formats

  • extent_dim_to_gt(), gt_dim_to_extent() - convert extent/dimension to/from geotransform

  • rasterio0(), rasterio_idx(), raster_sfio() - GDAL RasterIO parameters

  • rasterio_to_sfio(), sfio_to_rasterio() - convert between RasterIO formats

  • ts_te(), gdal_ts(), gdal_te() - format dimension/extent for GDAL command line

Output and display:

  • vaster_long() - cell coordinates as long-form matrix

  • vaster_listxyz() - grid as x, y, z list for graphics::image()

  • vaster_boundary(), vaster_boundary_cell() - boundary coordinates

  • plot_extent(), draw_extent() - plot and interactively draw extents

  • extent_vrt() - tile extents from VRT files

Utilities:

  • from_xyz() - derive grid dimension and extent from XYZ points

  • fit_dims() - compute dimension from aspect ratio and target size

Extent convention

vaster uses extent as c(xmin, xmax, ymin, ymax), which differs from the bbox convention c(xmin, ymin, xmax, ymax). Both represent the same information in different order.

If extent is not provided, the default is c(0, ncol, 0, nrow) - matching the convention adopted by stars, terra, and an improvement on base R's image() which scales to 0-1.

Author(s)

Maintainer: Michael Sumner [email protected] (ORCID) [copyright holder]

Authors:

Other contributors:

  • Robert Hijmans (Wrote original versions of abstract cell operations in raster package) [contributor]

See Also

Useful links:


Cell adjacency

Description

Find neighbouring cells by index, using only the grid dimension.

Usage

adjacency(dimension, cell, directions = "queen")

Arguments

dimension

integer vector of ncol, nrow

cell

integer vector of cell indices (1-based)

directions

character, one of "queen", "rook", or "bishop"

Details

Given cell indices into a grid of known dimension, return the indices of neighbouring cells. Out-of-bounds neighbours (at grid edges) are NA.

The directions argument controls which neighbours are returned:

  • "queen" (default): all 8 neighbours (rook + bishop)

  • "rook": 4 cardinal neighbours (up, down, left, right)

  • "bishop": 4 diagonal neighbours

Column order for "queen" is: up, down, left, right, upleft, upright, downleft, downright. For "rook": up, down, left, right. For "bishop": upleft, upright, downleft, downright. Direction names refer to the raster convention where cell 1 is at the top-left.

Value

integer matrix with one row per cell and one column per neighbour direction. Column names indicate the direction. Out-of-bounds neighbours are NA.

Corner vertex values from area-based rasters

A key use case is converting area-based cell values to corner vertex values for mesh generation. Each corner vertex is shared by up to four cells. For example, the top-left corner of a cell is shared with the cell above, the cell to the left, and the cell diagonally above-left:

  upleft  |  up
  --------x------     vertex 'x' = mean of upleft, up, left, self
  left    | self

The queen-connected neighbours give all the cells needed to compute every corner vertex. Averaging the appropriate neighbours provides weighted corner values from flat pixel areas, which is the basis for constructing continuous meshes from raster data (as used by quadmesh and anglr).

Examples

## 4x3 grid (4 columns, 3 rows), 12 cells
adjacency(c(4, 3), cell = 6)
adjacency(c(4, 3), cell = 6, directions = "rook")
adjacency(c(4, 3), cell = 6, directions = "bishop")

## corner cell has fewer valid neighbours
adjacency(c(4, 3), cell = 1)

## multiple cells at once
adjacency(c(4, 3), cell = 1:12)

## ------------------------------------------------
## Corner vertex interpolation from area values
## ------------------------------------------------
dm <- c(4, 3)
elev <- c(10, 12, 14, 16, 11, 13, 15, 17, 10, 11, 13, 14)

## The top-left corner of each cell is shared by self, the
## cell above, the cell to the left, and the cell diagonally
## above-left. Average these for the vertex value:
nb <- adjacency(dm, seq_along(elev))
vals <- cbind(elev, elev[nb[, "up"]],
              elev[nb[, "left"]], elev[nb[, "upleft"]])
corner <- rowMeans(vals, na.rm = TRUE)
matrix(corner, nrow = 3, byrow = TRUE)

Crop an extent, snapped to the grain

Description

A crop (or extend), it snaps the input extent to the origin of the input extent (based on the dimension) #' Note that snap is modelled on the behaviour of the raster package, and is different from projwin in GDAL (WIP to illustrate).

Usage

align_extent(x, dimension, extent = NULL, snap = c("out", "near", "in"))

Arguments

x

extent

dimension

integer ncol, nrow

extent

numeric extent xmin,xmax,ymin,ymax

snap

out by default, may be near or in

Value

aligned extent

Examples

align_extent(c(4.5, 5.6, 2, 4), c(10, 5), c(0, 10, 0, 5))

Cells

Description

Functions that work with cells.

Usage

cell_from_xy(dimension, extent = NULL, xy)

cell_from_extent(dimension, extent = NULL, x_extent)

extent_from_cell(dimension, extent = NULL, cell)

rowcol_from_cell(dimension, extent = NULL, cell)

xy_from_cell(dimension, extent = NULL, cell)

x_from_cell(dimension, extent = NULL, cell)

y_from_cell(dimension, extent = NULL, cell)

col_from_cell(dimension, cell)

row_from_cell(dimension, cell)

cell_from_row(dimension, row)

cell_from_col(dimension, col)

cell_from_row_col(dimension, row, col)

cell_from_rowcol_combine(dimension, row, col)

Arguments

dimension

integer ncol, nrow

extent

numeric extent xmin,xmax,ymin,ymax

xy

matrix of coordinates

x_extent

extent to find cells of

cell

cells to find extent, or row,col, or xy of

row

row to find cell of

col

column to find cell of

Details

The cell is indexed from the top left corner and proceeds to the right, and then down scanning by rows. The n cell is a the bottom right corner. Orientation is different to R's native matrix order, but see (WiP doc and helpers for conversion).

Value

cell index

cells of extent

extent of cells

row,col of cells

xy from cells

x of cells

y of cells

col of cells

row of cells

cell of rows

cell of cols

cell of row,col

cell of row,col combined

Examples

cell_from_xy(c(10, 5), extent = c(0, 10, 0, 5), cbind(5, 4))
cell_from_extent(c(10, 5), c(0, 10, 0, 5), c(6, 7, 2, 3))
extent_from_cell(c(10, 5), c(0, 10, 0, 5), c(4, 5))
rowcol_from_cell(c(10, 5), c(0, 10, 0, 5), 3:5)
xy_from_cell(c(10, 5), c(0, 10, 0, 5), 4:6)
x_from_cell(c(10, 5), c(0, 10, 0, 5), 4:7)
y_from_cell(c(10, 5), c(0, 10, 0, 5), 4:7)
col_from_cell(c(10, 5),  4:7)
row_from_cell(c(10, 5),  4:7)
cell_from_row(c(10, 5), 4:7)
cell_from_col(c(10, 5), 4:7)
cell_from_row_col(c(10, 5), 1:4, 4:7)
cell_from_rowcol_combine(c(10, 5), 1:4, 4:7)

Coordinates

Description

Functions that work with coordinates.

Usage

x_corner(dimension, extent = NULL)

y_corner(dimension, extent = NULL)

x_centre(dimension, extent = NULL)

y_centre(dimension, extent = NULL)

x_center(dimension, extent = NULL)

y_center(dimension, extent = NULL)

x_from_col(dimension, extent = NULL, col)

y_from_row(dimension, extent = NULL, row)

col_from_x(dimension, extent = NULL, x)

row_from_y(dimension, extent = NULL, y)

xy(dimension, extent = NULL)

Arguments

dimension

integer ncol, nrow

extent

numeric extent xmin,xmax,ymin,ymax

col

column index

row

row index

x

x coordinate

y

y coordinate

Details

x_center() and y_center() are US-spelling aliases for x_centre() and y_centre().

Value

x coordinate of corners

y coordinate of corners

x coordinate of centres

y coordinate of centres

x coordinate of col (centre)

y coordinate of row (centre)

col of x coordinate

y coordinate (centre) of row

xy coordinate (centre) of grid

Examples

x_corner(c(10, 5), c(0, 10, 0, 5))
y_corner(c(10, 5), c(0, 10, 0, 5))
x_centre(c(10, 5), c(0, 10, 0, 5))
y_centre(c(10, 5), c(0, 10, 0, 5))
x_center(c(10, 5), c(0, 10, 0, 5))
y_center(c(10, 5), c(0, 10, 0, 5))
x_from_col(c(10, 5), c(0, 10, 0, 5), 2:3)
y_from_row(c(10, 5), c(0, 10, 0, 5), 2:3)
col_from_x(c(10, 5), c(0, 10, 0, 5), 3.5 + 1:2)
row_from_y(c(10, 5), c(0, 10, 0, 5), 2:3)
xy(c(10, 5), c(0, 10, 0, 5))

Draw extent

Description

Draw an extent with two clicks

Usage

draw_extent(show = TRUE, ...)

Arguments

show

the drawn extent

...

arguments pass to graphics::rect()

Value

an extent, numeric vector of xmin,xmax,ymin,ymax

Examples

if (interactive()) {
plot(1)
draw_extent(show = TRUE)  ## click twice on the plot
}

Create geotransform from extent and dimension

Description

Create the geotransform (see geo_transform0()) from extent and dimension.

Usage

extent_dim_to_gt(x, dimension)

Arguments

x

extent parameters, c(xmin,xmax,ymin,ymax)

dimension

dimensions x,y of grid (ncol,nrow)

Details

The dimension is always ncol, nrow.

Value

6-element geo_transform0()

Examples

extent_dim_to_gt(c(0, 5, 0, 10), c(5, 10))

Dimension for an aligned extent

Description

input is the output of align_extent

Usage

extent_dimension(x, dimension, extent = NULL, snap = "out")

Arguments

x

and aligned extent

dimension

dimension of parent

extent

of parent

snap

out by default, may be near or in

Value

dimension

Examples

extent_dimension(c(.2, .8, 1.8, 3.2), c(10, 5), c(0, 10, 0, 5))

Extents from VRT (virtual raster data set of GDAL)

Description

Get extent from index values in VRT text, these are the individual footprints of raster windows in a VRT. These can be arbitrary to a specific grid, but generally are used for tiled mosaics.

Usage

extent_vrt(x)

Arguments

x

url, file path, or text connection to a VRT file

Details

Each VRT raster element records it's relative position within the grid, so we grid logic to expand the actual extent of each element, and return those as a matrix of xmin,xmax,ymin,ymax.

The VRT XML is parsed with the xml2 package, which must be installed (it is a suggested dependency).

Value

a matrix of 4 columns, xmin,xmax,ymin,ymax

Examples

#src <- "https://opentopography.s3.sdsc.edu/raster/NASADEM/NASADEM_be.vrt"
src <- gzfile(system.file("extdata/NASADEM_be.vrt.gz", package = "vaster"), "rt")
## read VRT from a URL or file (we use a connection here to keep package example small)
ex <- extent_vrt(src)
close(src)
plot_extent(ex)

Aspect ratio of dimension conflated with bbox

Description

Generate an appropriate dimension (shape, ncol,nrow) from an input width(height). If height not specified we have a square.

Usage

fit_dims(size = 1024L, wh = c(size, size))

Arguments

size

seed dimension size

wh

distance across dimension span/s

Value

dimension c(ncol, nrow)

Examples

fit_dims(256, c(10, 20))
fit_dims(1024, c(102723, 1e5))

Derive a grid from XYZ points

Description

This function is very liberal, it simply finds unique x values and unique y values, sorts them and finds the minimum difference between the points, then checks that rounded ratio of differences to this minimum is 1.

Usage

from_xyz(xyz, digits = 5)

Arguments

xyz

set of points xy or xyz (matrix or data frame)

digits

argument passed to round()

Details

The points can be the full grid set, a partial set, or a superset of the grid. The resolution will simply be the smallest actual difference found. (Zero is not possible because we sort(unique(...))).

The z-column if present is ignored.

Value

list with elements 'dimension', 'extent'

Examples

from_xyz(vaster_long(c(10, 5), c(0, 10, 0, 5)))

Geo transform parameter creator

Description

Basic function to create a geotransform as used by GDAL.

Usage

geo_transform0(px, ul, sh = c(0, 0))

Arguments

px

pixel resolution (XY, Y-negative)

ul

grid offset, top-left corner

sh

affine shear (XY)

Value

vector of parameters xmin, xres, yskew, ymax, xskew, yres

See Also

geo_world0() which uses the same parameters in a different order

Examples

geo_transform0(px = c(1, -1), ul = c(0, 0))

World file parameter creator

Description

Basic function to create a 'world file' as used by various non-geo image formats

Reformat to world vector.

Usage

geo_world0(px, ul, sh = c(0, 0))

geotransform_to_world(x)

Arguments

px

pixel resolution (XY, Y-negative)

ul

grid offset, top-left corner

sh

affine shear (XY)

x

geotransform parameters, as per geo_transform0()

Details

Note that xmin/xmax are centre_of_cell (of top-left cell) unlike the geotransform which is top-left corner_of_cell. The parameters are otherwise the same, but in a different order.

Value

vector of parameters xres, yskew, xskew, yres, xmin, ymax

world vector, as per geo_world0()

See Also

geo_transform0

Examples

geo_world0(px = c(1, -1), ul = c(0, 0))
(gt <- geo_transform0(px = c(1, -1), ul = c(0, 0)))
wf <- geotransform_to_world(gt)
world_to_geotransform(wf)

Grid

Description

Basic grid tools, cell, resolution, dimension, extent.

Usage

n_cell(dimension)

x_res(dimension, extent = NULL)

y_res(dimension, extent = NULL)

n_row(dimension)

n_col(dimension)

xlim(dimension, extent = NULL)

ylim(dimension, extent = NULL)

x_min(dimension, extent = NULL)

x_max(dimension, extent = NULL)

y_min(dimension, extent = NULL)

y_max(dimension, extent = NULL)

Arguments

dimension

integer ncol, nrow

extent

numeric extent xmin,xmax,ymin,ymax

Value

number of cells

x resolution (width of cell)

y resolution (height of cell)

number of rows

number of cols

x extent (corner to corner)

y extent (corner to corner)

x minimum (left edge)

x maximum (right edge)

y minimum (bottom edge)

y maximum (top edge)

Examples

n_cell(c(10, 5))
x_res(c(10, 5), c(0, 10, 0, 5))
y_res(c(10, 5), c(0, 10, 0, 5))
n_row(c(10, 5))
n_col(c(10, 5))
xlim(c(10, 5), c(0, 10, 0, 5))
ylim(c(10, 5), c(0, 10, 0, 5))
x_min(c(10, 5), c(0, 10, 0, 5))
x_max(c(10, 5), c(0, 10, 0, 5))
y_min(c(10, 5), c(0, 10, 0, 5))
y_max(c(10, 5), c(0, 10, 0, 5))

Integer offset between two grids of equal resolution

Description

The column and row offset from the top-left cell of the parent grid to the top-left cell of the child grid. Both grids must have the same resolution and share a lattice. The offset may be negative or exceed the parent dimension: alignment is not containment.

Usage

grid_offset(
  dimension,
  extent = NULL,
  dimension2,
  extent2 = NULL,
  tol = lattice_tol()
)

Arguments

dimension

integer ncol, nrow

extent

numeric extent xmin,xmax,ymin,ymax

dimension2

dimension of the second grid (ncol, nrow)

extent2

extent of the second grid (xmin, xmax, ymin, ymax), NULL for unit cell space

tol

relative tolerance for whole-number tests

Value

named integer vector c(col = , row = ), 0-based

Examples

## child starts 2 cells in and 3 cells down
grid_offset(c(10L, 10L), c(0, 10, 0, 10), c(5L, 5L), c(2, 7, 2, 7))

Partition a grid into tiles

Description

Pure index arithmetic: how many tiles fit and what their extents are. Tiles are enumerated left to right, then top to bottom, matching cell order. Edge tiles may be smaller than tile_dim. Every tile is aligned to the parent and grid_offset() of a tile is its origin in parent cells.

Usage

grid_tiles(dimension, extent = NULL, tile_dim = c(256L, 256L))

Arguments

dimension

integer ncol, nrow

extent

numeric extent xmin,xmax,ymin,ymax

tile_dim

tile dimension (ncol, nrow), recycled if length 1

Value

list of lists, each with dimension and extent, plus offset (the tile's 0-based c(col, row) origin in the parent)

Examples

tl <- grid_tiles(c(10L, 7L), c(0, 10, 0, 7), c(4L, 4L))
length(tl)
tl[[6]]

Determine extent from geotransform vector and dimension

Description

Create the extent (xlim, ylim) from the geotransform and dimensions of the grid.

Usage

gt_dim_to_extent(x, dim)

Arguments

x

geotransform parameters, as per geo_transform0()

dim

dimensions x,y of grid (ncol,nrow)

Details

The extent is c(xmin, xmax, ymin, ymax).

Value

4-element extent c(xmin,xmax,ymin,ymax)

Examples

gt_dim_to_extent(geo_transform0(c(1, -1), c(0, 10)), c(5, 10))

Intersect extent

Description

Return the overlapping extent.

Usage

intersect_extent(x, dimension, extent = NULL)

Arguments

x

extent to intersect

dimension

integer ncol, nrow

extent

numeric extent xmin,xmax,ymin,ymax

Value

extent, a numeric vector of xmin,xmax,ymin,ymax

Examples

intersect_extent(c(0.5, 2.3, 1.2, 5), c(10, 5), c(0, 10, 0, 5))

Lattice alignment of two grids

Description

Two grids share a lattice when their cell boundaries coincide: the resolutions are commensurate (one is an exact integer multiple of the other, in x and in y) and the origins differ by a whole number of the finer cells. When this holds every index operation between the grids is exact integer arithmetic. The test is symmetric.

Usage

is_aligned(
  dimension,
  extent = NULL,
  dimension2,
  extent2 = NULL,
  tol = lattice_tol()
)

Arguments

dimension

integer ncol, nrow

extent

numeric extent xmin,xmax,ymin,ymax

dimension2

dimension of the second grid (ncol, nrow)

extent2

extent of the second grid (xmin, xmax, ymin, ymax), NULL for unit cell space

tol

relative tolerance for whole-number tests

Details

Note that alignment is not containment: a grid may be aligned to another and lie entirely outside it. Use intersect_extent() for overlap.

Value

logical

See Also

grid_offset(), snap_grid(), resize_grid(), grid_tiles()

Examples

is_aligned(c(10L, 10L), c(0, 10, 0, 10), c(5L, 5L), c(2, 7, 2, 7))
is_aligned(c(10L, 10L), c(0, 10, 0, 10), c(5L, 5L), c(2.5, 7.5, 2, 7))
## coarser grid on the same lattice
is_aligned(c(10L, 10L), c(0, 10, 0, 10), c(2L, 2L), c(0, 10, 0, 10))

magick geometry string for a grid crop

Description

Format a crop window as an ImageMagick "geometry" string "WIDTHxHEIGHT+XOFF+YOFF", suitable for magick::image_crop(). This is a sibling of rasterio0(): magick addresses an image as a pixel grid with a top-left origin and 0-based offsets, the same convention GDAL RasterIO uses, so the window is pure grid logic with no pixels read.

Usage

magick_geometry(dimension, extent = NULL, crop = NULL, snap = "out")

Arguments

dimension

integer ncol, nrow

extent

numeric extent xmin,xmax,ymin,ymax

crop

extent c(xmin, xmax, ymin, ymax) to crop to, or NULL for the whole grid

snap

how to snap the crop to cell boundaries, passed to vcrop()

Details

The crop extent is snapped to the grid with vcrop() (so the result always lands on cell boundaries), and the pixel offset is the grid_offset() of the snapped window within the parent grid. With crop = NULL the geometry covers the whole grid.

Value

length-1 character string "WIDTHxHEIGHT+XOFF+YOFF"

See Also

rasterio0(), vcrop(), grid_offset()

Examples

## whole grid
magick_geometry(c(360, 180), c(-180, 180, -90, 90))
## a cropped window (Australia-ish)
magick_geometry(c(360, 180), c(-180, 180, -90, 90), c(110, 155, -45, -10))

Origin of grid alignment

Description

Origin of grid alignment

Usage

origin(dimension, extent = NULL)

Arguments

dimension

integer ncol, nrow

extent

numeric extent xmin,xmax,ymin,ymax

Value

coordinate of grid origin

Examples

origin(c(10, 5), c(0, 10, 0, 5))

Plot an extent

Description

Plot an extent

Usage

plot_extent(x, ..., asp = 1, add = FALSE, border = "black")

Arguments

x

extent xmin,xmax,ymin,ymax

...

arguments passed to graphics::rect()

asp

aspect ratio (1 by default)

add

add to plot or initiated one, FALSE by default

border

colour of lines of extent

Value

nothing, used for plot side effect

Examples

plot_extent(c(-180, 180, -90, 90))
plot_extent(c(100, 150, -60, -30), add = TRUE, border = "firebrick")

RasterIO index window

Description

The sf RasterIO is the RasterIO window in a list format used by the sf package, it contains the same information, and is created by raster_sfio().

Usage

rasterio_idx(dimension, extent)

raster_sfio(dimension, fact = 1, resample = "Nearest")

Arguments

dimension

ncols, nrows

extent

this is ignored

fact

a resizing factor

resample

resample algorithm for GDAL RasterIO

Value

RasterIO window vector 'c(x0, y0, nx0, ny0, nx, y)' see Details

Examples

rasterio_idx(dim(volcano))

The sf/stars RasterIO list

Description

We create the list as used by the stars/sf GDAL IO function 'gdal_read(, RasterIO_parameters)'.

Usage

rasterio_to_sfio(x)

Arguments

x

rasterio params as from rasterio0()

Details

Note that the input is a 4 or 6 element vector, with offset 0-based and output dimensions optional (will use the source window). The resample argument uses the syntax identical to that used in GDAL itself.

Value

list in sf RasterIO format

Examples

rio <- rasterio0(c(0L, 0L), src_dim = c(24L, 10L))
rasterio_to_sfio(rio)

GDAL RasterIO parameter creator

Description

Basic function to create the window parameters as used by GDAL RasterIO.

Usage

rasterio0(
  src_offset,
  src_dim,
  out_dim = src_dim,
  resample = "NearestNeighbour"
)

Arguments

src_offset

index offset (0-based, top left)

src_dim

source dimension (XY)

out_dim

output dimension (XY, optional src_dim will be used if not set)

resample

resampling algorithm for GDAL see details

Details

Resampling algorithm is one of 'NearestNeighbour' (default), 'Average', 'Bilinear', 'Cubic', 'CubicSpline', 'Gauss', 'Lanczos', 'Mode', but more may be available given the version of GDAL in use.

Value

numeric vector of values specifying offset, source dimension, output dimension

Examples

rasterio0(c(0L, 0L), src_dim = c(24L, 10L))

Resize a grid by a factor, keeping the extent

Description

Change resolution by a factor that yields whole-number dimensions. The new lattice nests exactly within the old one (factor > 1) or the old within the new (factor < 1), so resize_grid(resize_grid(g, k), 1/k) returns g.

Usage

resize_grid(dimension, extent = NULL, factor, tol = lattice_tol())

Arguments

dimension

integer ncol, nrow

extent

numeric extent xmin,xmax,ymin,ymax

factor

positive scaling factor, applied to the dimension; a single value or c(xfactor, yfactor)

tol

relative tolerance for whole-number tests

Value

list with dimension and extent

Examples

resize_grid(c(10L, 10L), c(0, 10, 0, 10), 2)
resize_grid(c(10L, 10L), c(0, 10, 0, 10), 0.5)
try(resize_grid(c(10L, 10L), c(0, 10, 0, 10), 0.3))

sf package RasterIO from RasterIO window vector

Description

Basic function to create the window parameters as used by GDAL RasterIO, in format used by sf, in 'gdal_read(,RasterIO_parameters)'.

Usage

sfio_to_rasterio(x)

Arguments

x

a RasterIO parameter list

Value

a sf-RasterIO parameter list

Examples

sfio_to_rasterio(rasterio_to_sfio(rasterio0(c(0L, 0L), src_dim = c(24L, 10L))))

Snap extent to resolution (buffer extent)

Description

Whole grain buffers.

Usage

snap_extent(x, res)

buffer_extent(x, res)

Arguments

x

extent (xmin, xmax, ymin, ymax)

res

resolution (a grain to align to)

Value

extent, snapped to the resolution

Examples

snap_extent(sort(rnorm(4)), 0.01)

Snap a grid onto a parent lattice

Description

Moves the extent of the first grid so that its cell boundaries fall on cell boundaries of the parent grid, keeping its own resolution. The two resolutions must be commensurate. The snap is to the coarser of the two resolutions, anchored at the parent's top-left origin, so the result is always aligned to the parent (and the parent to it).

Usage

snap_grid(
  dimension,
  extent = NULL,
  dimension2,
  extent2 = NULL,
  tol = lattice_tol()
)

Arguments

dimension

dimension of the grid to snap (ncol, nrow)

extent

extent of the grid to snap

dimension2

dimension of the parent grid

extent2

extent of the parent grid

tol

relative tolerance for whole-number tests

Details

This differs from align_extent() / vcrop(), which snap an arbitrary extent to a grid and take that grid's resolution. Here the grid being snapped keeps its resolution and only its origin moves.

Value

list with dimension (unchanged) and extent (snapped)

Examples

## 2x2 grid at res 2, slightly off a unit lattice
snap_grid(c(2L, 2L), c(0.4, 4.4, 0.4, 4.4), c(10L, 10L), c(0, 10, 0, 10))

Target size and extent for GDAL command line

Description

Format grid properties for GDAL command line options (-ts for target size, -te for target extent).

Usage

ts_te(dimension, extent)

gdal_te(extent)

gdal_ts(dimension)

Arguments

dimension

integer vector of ncol, nrow (target size)

extent

numeric vector of xmin, xmax, ymin, ymax (target extent)

Details

These functions generate the string arguments used by GDAL utilities like gdalwarp and gdal_translate. The gdal_ts() function is named after the GDAL -ts flag and gdal_te() after the GDAL -te flag.

Value

A character string formatted for GDAL command line:

  • ts_te(): combined -ts and -te arguments

  • gdal_ts(): ⁠-ts ncol nrow⁠ string

  • gdal_te(): ⁠-te xmin ymin xmax ymax⁠ string (note: reordered for GDAL)

See Also

vcrop() for computing aligned extents

Examples

ts_te(c(10, 100), 1:4)

gdal_ts(c(10, 100))

gdal_te(1:4)

## use in a GDAL command (not run)
## Not run: 
cmd <- sprintf("gdalwarp %s %s input.tif output.tif",
               gdal_ts(c(1000, 500)), gdal_te(c(-180, 180, -90, 90)))

## End(Not run)

Grid boundary in native resolution

Description

Generates the coordinates around a grid boundary, with a coordinate for each grid cell corner.

Usage

vaster_boundary(dimension, extent = NULL)

Arguments

dimension

integer ncol, nrow

extent

numeric extent xmin,xmax,ymin,ymax

Details

The orientation is starts along the bottom and goes counter-clockwise.

Value

matrix of xy coordinates

Examples

vaster_boundary(c(3, 4))

plot(vaster_boundary(c(36, 18), c(-180, 180, -90, 90)))

Grid boundary cell index

Description

This is for indexing coordinate arrays to get their values (the cell index of the outer row and columns).

Usage

vaster_boundary_cell(dimension)

Arguments

dimension

integer ncol, nrow

Details

The orientation is the same as for vaster_boundary().

Value

a matrix of xy coordinates

Examples

vaster_boundary_cell(c(3, 4))
cell <- vaster_boundary_cell(c(3, 4))
plot(vaster_boundary(c(3, 4)))
text(xy <- xy_from_cell(c(3, 4), cell = vaster_boundary_cell(c(3, 4))), lab = cell)
lines(xy)

Image xyz list

Description

Generate list of x and y rectilinear coordinates with z matrix.

Usage

vaster_listxyz(dimension, extent = NULL, data = NULL, corner = FALSE)

Arguments

dimension

integer ncol, nrow

extent

numeric extent xmin,xmax,ymin,ymax

data

data values (length of the product of 'dimension')

corner

return cell corner coordinates rather than cell centres

Details

The rectilinear coordinates are degenerate (just a product of extent/dimension).

By default x and y are the cell centres (length ncol and nrow). With corner = TRUE they are the cell corners (length ncol + 1 and nrow + 1), the other form graphics::image() accepts, where x/y are the cell boundaries. The z matrix is the same in both cases.

Value

list with elements x,y,z as per graphics::image

Examples

vaster_listxyz(c(10, 5), c(0, 10, 0, 5))
## cell corners (boundaries) instead of centres
vaster_listxyz(c(10, 5), c(0, 10, 0, 5), corner = TRUE)
## see https://gist.github.com/mdsumner/b844766f28910a3f87dc2c8a398a3a13

Convert to long form coordinates

Description

Matrix of xyz values in raster order.

Usage

vaster_long(dimension, extent = NULL, data = NULL, raster_order = TRUE)

Arguments

dimension

integer ncol, nrow

extent

numeric extent xmin,xmax,ymin,ymax

data

data values

raster_order

use raster order or native R matrix order

Details

Use 'raster_order = FALSE' for traditional R matrix x,y order

Value

matrix of coordinates x,y

Examples

vaster_long(c(10, 5), c(0, 10, 0, 5))
# see https://gist.github.com/mdsumner/b844766f28910a3f87dc2c8a398a3a13

Virtual grid modification

Description

To modify a grid is to align an extent to the grid origin. Modification includes reducing or extending the area covered in either dimension. This implies a new extent, snapped to the grain of the origin grid, and a new dimension (ncol, nrow).

Usage

vcrop(x, dimension, extent = NULL, ..., snap = "out")

Arguments

x

extent of candidate grid (vector of xmin, xmax, ymin, ymax)

dimension

integer ncol, nrow

extent

numeric extent xmin,xmax,ymin,ymax

...

ignored

snap

one of "out" (default), "near", or "in"

Details

This works for any grid: the input extent can be within the original, an extension of the original, or completely non-intersecting the original grid.

Value

A list with two components:

extent

numeric vector (xmin, xmax, ymin, ymax) - the new extent, snapped to grid alignment

dimension

integer vector (ncol, nrow) - the dimension of the modified grid

See Also

align_extent() for just the extent snapping, extent_dimension() for just the dimension calculation

Examples

## any arbitrary extent
(x <- c(sort(runif(2, -180, 180)), sort(runif(2, -90, 90))))

vcrop(x, c(360, 180), c(-180, 180, -90, 90))

## crop to a smaller region
vcrop(c(0, 10, 0, 10), c(360, 180), c(-180, 180, -90, 90))

## extend beyond original (snapped to grid)
vcrop(c(-200, 200, -100, 100), c(360, 180), c(-180, 180, -90, 90))

Create geotransform from world vector

Description

Convert world vector (centre offset) and x,y spacing to geotransform format.

Usage

world_to_geotransform(x)

Arguments

x

worldfile parameters, as per geo_world0()

Value

geotransform vector, see geo_transform0()

Examples

(wf <- geo_world0(px = c(1, -1), ul = c(0, 0)))
gt <- world_to_geotransform(wf)
geotransform_to_world(gt)