Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compare this tiling scheme #9

Open
mdsumner opened this issue May 5, 2022 · 0 comments
Open

compare this tiling scheme #9

mdsumner opened this issue May 5, 2022 · 0 comments

Comments

@mdsumner
Copy link
Member

mdsumner commented May 5, 2022

started a blog post, compare with .tilescheme


raster_url <- "ftp://ftp.data.pgc.umn.edu/elev/dem/setsm/REMA/mosaic/v1.1/200m/REMA_200m_dem.tif"

readfrom_file <- tempfile(pattern = "readfrom", fileext = ".tif")
writeto_file <- tempfile(pattern = "writeto", fileext = ".tif")
## file size is 1.3Gb
curl::curl_download(raster_url, readfrom_file)

## this now makes a copy of the 1.3Gb file, so we have two of them
fs::file_copy(readfrom_file, writeto_file)

Now we want the tiling

info <- vapour::vapour_raster_info(readfrom_file)
tiling <- list(dimension = info$dimXY, tiles = info$tilesXY)
fac <- 100
fac * tiling$tiles
if (tiling$tiles[2] == 1) {
  ## let's take fac scanlines at a time
  tiling$tiles[2] <- fac
}
calc_steps <- function(dimension, tiles) {
  bounds_x <- seq(0, dimension, by = tiles)
  steps_x <-  rep(tiles, length.out = length(bounds_x)-1)
  dangle_x <- sum(steps_x) -dimension
  if (dangle_x > 0) steps_x[length(steps_x)] <- steps_x[length(steps_x)] - dangle_x

  list(head(bounds_x, -1), steps_x)
}
x_step <- calc_steps(tiling$dimension[1], tiling$tiles[1])
y_step <- calc_steps(tiling$dimension[2], tiling$tiles[2])
y_step
system.time({
for (i in seq_along(x_step[[1]])) {
  startx <- x_step[[1]][i]
  countx <- x_step[[2]][i]
  for (j in seq_along(y_step[[1]])) {
  starty <- y_step[[1]][j]
  county <- y_step[[2]][j]

  ## now read
  offset <- c(startx, starty)
  dimension <- c(countx, county)
  vals <- vapour:::vapour_read_raster_block(readfrom_file, offset = offset, dimension = dimension, band_output_type = info$datatype, band = 1)[[1L]]

  ## do something to the values
  if (any(na.omit(vals) > info$nodata_value)) {
    vals <- vals * -1
    vapour:::vapour_write_raster_block(writeto_file, vals, offset, dimension, band = 1, overwrite = TRUE)
  }
  }
}

@mdsumner mdsumner changed the title compare this tiling schemed compare this tiling scheme May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant