diff --git a/R/tiles.R b/R/tiles.R new file mode 100644 index 0000000..eaf4e15 --- /dev/null +++ b/R/tiles.R @@ -0,0 +1,54 @@ +## tiles +## we need row,col,cell converters for mosaics + + +## these are internal use versions that don't detect NA out of bounds +col_from_x_inf <- function(dimension, extent = NULL, x) { + extent <- extent %||% extent0(dimension) + .check_args(dimension, extent) + + colnr <- trunc((x - x_min(dimension, extent)) / x_res(dimension, extent = extent)) + 1 + colnr[ x == x_max(dimension, extent) ] <- n_col(dimension) + #colnr[ x < x_min(dimension, extent) | x > x_max(dimension, extent) ] <- NA + return(as.vector(colnr)) +} + +row_from_y_inf <- function(dimension, extent = NULL, y) { + extent <- extent %||% extent0(dimension) + .check_args(dimension, extent) + + rownr <- 1 + (trunc((y_max(dimension, extent) - y) / y_res(dimension, extent = extent))) + rownr[y == y_min(dimension, extent) ] <- n_row(dimension) +# rownr[y > y_max(dimension, extent) | y < y_min(dimension, extent)] <- NA + return(as.vector(rownr)) +} + + +## child from parent or parent from child (using _inf versions that allow out of bounds row/col numbers) +cell2cell <- function(dimension, extent, ## that's the parent + dim0, ext0, cell0 ) { ## the chid tile) { + + ## checks + orig <- origin(dimension, extent) + orig0 <- origin(dim0, ext0) + stopifnot(all.equal(orig, orig0)) + + res <- c(x_res(dimension, extent), y_res(dimension, extent)) + res0 <- c(x_res(dim0, ext0), y_res(dim0, ext0)) + stopifnot(all.equal(res, res0)) + + ## the col,row of the parent where the child starts (tr) + cr <- c(col_from_x_inf(dimension, extent, ext0[1]), + row_from_y_inf(dimension, extent, ext0[4])) + + + + # print(cr) + rc0 <- rowcol_from_cell(dim0, ext0, cell0) + + cr_global <- cbind(cr[1] + rc0[,2] - 1, + cr[2] + rc0[,1] - 1) + cell_from_row_col(dimension, cr_global[,2], cr_global[,1]) +} + + diff --git a/man/extent_vrt.Rd b/man/extent_vrt.Rd index dc71a23..b54f6e2 100644 --- a/man/extent_vrt.Rd +++ b/man/extent_vrt.Rd @@ -13,7 +13,7 @@ extent_vrt(x) Get extent from index values in VRT text. } \details{ -(I can't understand XML tech for the life of me so I hack the text as lines with strsplit) +(I can't understand XML tech so I hack the text as lines with strsplit) } \examples{ #src <- "https://opentopography.s3.sdsc.edu/raster/NASADEM/NASADEM_be.vrt"