hypertidy / vaster

grid logic, without any pesky data
https://hypertidy.github.io/vaster/
Other
7 stars 0 forks source link

focal index #2

Open mdsumner opened 2 years ago

mdsumner commented 2 years ago

for focal, we need something like

cell_kernel <- function(dimension, cell, size = 1) {
  ## offset is
  row <- row_from_cell(dimension, cell)
  col <- col_from_cell(dimension, cell)
  offset <- c(col + c(-1, 1) * size, row + c(-1, 1) * size)
  offset[c(1, 3)] <- pmax(offset[c(1, 3)], 0)  ## 0-based for vapour_read_raster
  offset[c(2, 4)] <- pmin(offset[c(2, 4)], dimension-1)

  outdim <- diff(offset)[c(1, 3)] + 1
  c(offset[c(1, 3)], outdim)
}
cell_kernel(c(100, 50), 3)

which, is called for each 1:prod(dimension) to provide offset and dimension for a given cell (size = 1 is 3x3)

the output is the input to vapour_read_raster(window = ) - 0-based index

mdsumner commented 2 years ago

then probably, need to convert these to R-order in big chunks and read big swathes of raster - with masks for the different kernels, and a group by to do the calcs

mdsumner commented 2 years ago

see here, it's actually pretty simple because we only have a matrix for the kernel to apply to - read a chunk that aligns to the kernel size, and zap out the dangle on the left and top by index out of bounds, you don't care what the extent is just use local cell logic as the data is a flat vector, we expand out by kernel group ID

https://github.com/hypertidy/vaster/blob/10ef32a3ff98ec3c27073c933e9603b8eeebfcec/R/kernel.R