## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----setup--------------------------------------------------------------------
library(vaster)

## -----------------------------------------------------------------------------
## define a simple 4x3 grid
dimension <- c(4, 3)   # 4 columns, 3 rows
extent <- c(0, 4, 0, 3) # xmin=0, xmax=4, ymin=0, ymax=3

n_cell(dimension)
n_col(dimension)
n_row(dimension)

## -----------------------------------------------------------------------------
## single cell
xy_from_cell(dimension, extent, 1)

## multiple cells
xy_from_cell(dimension, extent, c(1, 4, 9, 12))

## all cells
xy_from_cell(dimension, extent, seq_len(n_cell(dimension)))

## -----------------------------------------------------------------------------
x_from_cell(dimension, extent, 1:4)
y_from_cell(dimension, extent, c(1, 5, 9))

## -----------------------------------------------------------------------------
## coordinates at cell centres
xy <- cbind(c(0.5, 1.5, 2.5), c(2.5, 1.5, 0.5))
cell_from_xy(dimension, extent, xy)

## coordinates outside the grid return NA
xy_outside <- cbind(c(-1, 5), c(1.5, 1.5))
cell_from_xy(dimension, extent, xy_outside)

## coordinates on cell boundaries go to the cell below/right
## except at the grid edge where they go to the last cell
xy_edge <- cbind(c(1.0, 4.0), c(2.0, 0.0))
cell_from_xy(dimension, extent, xy_edge)

## -----------------------------------------------------------------------------
## get row and column for cells
rowcol_from_cell(dimension, extent, 1:12)

## -----------------------------------------------------------------------------
row_from_cell(dimension, 1:12)
col_from_cell(dimension, 1:12)

## -----------------------------------------------------------------------------
## single cell at row 2, column 3
cell_from_row_col(dimension, row = 2, col = 3)

## multiple cells (vectorized, with recycling)
cell_from_row_col(dimension, row = 1:3, col = 1:3)  # diagonal cells

## -----------------------------------------------------------------------------
## all cells in row 2
cell_from_row(dimension, 2)

## all cells in column 3
cell_from_col(dimension, 3)

## -----------------------------------------------------------------------------
## all cells in rows 1-2 AND columns 2-3
cell_from_rowcol_combine(dimension, row = 1:2, col = 2:3)

## -----------------------------------------------------------------------------
## x coordinate of each column (centre)
x_from_col(dimension, extent, 1:4)

## y coordinate of each row (centre)
y_from_row(dimension, extent, 1:3)

## which column contains this x coordinate
col_from_x(dimension, extent, c(0.5, 2.5, 3.9))

## which row contains this y coordinate
row_from_y(dimension, extent, c(2.5, 1.5, 0.1))

## -----------------------------------------------------------------------------
## extent of a single cell
extent_from_cell(dimension, extent, 1)

## extent covering multiple cells
extent_from_cell(dimension, extent, c(1, 4))  # top row
extent_from_cell(dimension, extent, c(1, 12)) # full grid

## -----------------------------------------------------------------------------
## cells within a sub-extent
sub_extent <- c(1, 3, 1, 2)  # xmin=1, xmax=3, ymin=1, ymax=2
cell_from_extent(dimension, extent, sub_extent)

## -----------------------------------------------------------------------------
## invalid cell numbers
xy_from_cell(dimension, extent, c(0, 13, -1))

## coordinates outside grid
cell_from_xy(dimension, extent, cbind(c(-1, 10), c(1, 1)))

## invalid row/column
cell_from_row_col(dimension, row = 0, col = 1)
cell_from_row_col(dimension, row = 4, col = 1)  # only 3 rows exist

## -----------------------------------------------------------------------------
set.seed(42)
dimension <- c(5, 4)
extent <- c(0, 10, 0, 8)

## get cell centres
centres <- xy_from_cell(dimension, extent, seq_len(n_cell(dimension)))

## add random jitter within each cell
res <- c(x_res(dimension, extent), y_res(dimension, extent))
jitter_x <- runif(n_cell(dimension), -res[1]/2, res[1]/2)
jitter_y <- runif(n_cell(dimension), -res[2]/2, res[2]/2)

sample_points <- cbind(
  x = centres[, 1] + jitter_x,
  y = centres[, 2] + jitter_y
)

head(sample_points)

## verify all points fall in expected cells
all(cell_from_xy(dimension, extent, sample_points) == seq_len(n_cell(dimension)))

