Skip to content

Instantly share code, notes, and snippets.

@mdsumner
Last active July 17, 2024 14:50
Show Gist options
  • Save mdsumner/0a0a15c75ce7c467c41291762117ee07 to your computer and use it in GitHub Desktop.
Save mdsumner/0a0a15c75ce7c467c41291762117ee07 to your computer and use it in GitHub Desktop.
spec_tile2 <- function(dimension, extent = NULL, blocksize, overlap = 0) {
  if (is.null(extent)) extent <- c(0, dimension[1L], 0, dimension[2L])
  ## note the strange reversal of blocksize x/y and how st_tile names its arguments
  ## see an easier to grok impl in hypertidy/grout
  tiles <- stars::st_tile(dimension[1], dimension[2], 
                          x_window = blocksize[2], 
                          y_window = blocksize[1], overlap = overlap)
  resx <- diff(extent[1:2]) / dimension[1L]
  resy <- diff(extent[3:4]) / dimension[2L]

  xmin <- extent[1L] + (tiles[,"nXOff", drop = TRUE] - 1)  * resx
  xmax <- extent[1L] +  ((tiles[,"nXOff", drop = TRUE]-1) + tiles[, "nXSize", drop = TRUE]) * resx
  ## note minus y
  ymax <- extent[4L] + (tiles[,"nYOff", drop = TRUE] - 1)  * -resy
  ymin <- extent[4L]  + (tiles[, "nYOff", drop = TRUE] -1 + tiles[,"nYSize"])  * -resy
  
  cbind(tiles, xoff = tiles[,"nXOff", drop = TRUE] - 1, yoff = tiles[,"nYOff", drop = TRUE] - 1, xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment