Skip to main content

Crate vaster

Crate vaster 

Source
Expand description

§vaster

Raster grid logic, without any pesky data.

Raster grids are defined by dimension (ncol, nrow) and extent (xmin, xmax, ymin, ymax). Everything else — resolution, cell centres, geotransforms, cell indexing — derives from those six numbers.

This crate provides that derivation with zero dependencies.

§Conventions

  • Extent is [xmin, xmax, ymin, ymax] — same as the R vaster package
  • Dimension is [ncol, nrow]
  • Cell indices are 0-based (the R package uses 1-based)
  • Geotransform follows GDAL convention: [origin_x, pixel_width, row_rotation, origin_y, col_rotation, pixel_height]
  • Cell (0, 0) is the top-left pixel, traversing right then down

§GDAL geotransform

The 6-element geotransform maps pixel coordinates to geographic coordinates:

x_geo = gt[0] + pixel * gt[1] + line * gt[2]
y_geo = gt[3] + pixel * gt[4] + line * gt[5]

For north-up images (no rotation): gt[2] == 0, gt[4] == 0, gt[5] < 0.

Reference: https://gdal.org/tutorials/geotransforms_tut.html

§Quick start

use vaster::*;

let extent = [0.0, 360.0, -90.0, 90.0];
let dim = [360, 180];

// Build a geotransform
let gt = extent_dim_to_gt(&extent, &dim);
assert_eq!(gt[1], 1.0); // 1 degree per pixel

// Cell centre of top-left pixel
let (x, y) = xy_from_cell(&dim, &extent, 0);
assert!((x - 0.5).abs() < 1e-10);
assert!((y - 89.5).abs() < 1e-10);

// Which cell contains a point?
let cell = cell_from_xy(&dim, &extent, 10.3, 45.7);
assert_eq!(cell, Some(10 + 44 * 360));

§Relationship to the R package

This crate is the Rust equivalent of hypertidy/vaster, an R package for raster grid logic. The API follows the same conventions with one difference: cell indices are 0-based in Rust (1-based in R).

Functions§

cell_from_row_col
Cell index from 0-based (row, col).
cell_from_xy
Cell index from geographic (x, y) coordinates.
col_from_cell
Column index (0-based) from cell index.
col_from_x
Fractional column index from geographic x-coordinate.
col_row_from_xy
Fractional (col, row) from geographic (x, y), using the full affine inverse.
crop_offset
Compute the column and row offset of an aligned sub-grid within its parent.
extent_dim_to_gt
Build a geotransform from extent and dimensions.
geotransform_to_world
Convert a GDAL geotransform to world file parameters.
gt_to_extent
Extract extent from a geotransform and dimensions.
inv_geotransform
Invert a geotransform.
ncell
Total number of cells.
row_col_from_cell
(row, col) from cell index (both 0-based).
row_from_cell
Row index (0-based) from cell index.
row_from_y
Fractional row index from geographic y-coordinate.
vcrop
Snap an extent to align with a parent grid.
world_to_geotransform
Convert a world file to a GDAL geotransform.
x_centre
X-coordinates of cell centres for each column.
x_corner
X-coordinates of cell edges (corners) for each column boundary.
x_from_col
Geographic x-coordinate of a pixel column centre.
x_res
Resolution (pixel width) in the x direction.
xy_from_cell
Geographic (x, y) of a cell centre, from extent and dimension.
xy_from_col_row
Geographic (x, y) from pixel (col, row), using the full affine transform.
y_centre
Y-coordinates of cell centres for each row.
y_corner
Y-coordinates of cell edges (corners) for each row boundary.
y_from_row
Geographic y-coordinate of a pixel row centre.
y_res
Resolution (pixel height) in the y direction.

Type Aliases§

Dimension
Raster dimensions: [ncol, nrow].
Extent
Raster extent: [xmin, xmax, ymin, ymax].
GeoTransform
A GDAL-style affine geotransform.
WorldFile
World file parameters: [x_res, y_skew, x_skew, y_res, x_centre, y_centre]