Skip to contents

This function creates a grid of square or hexagonal polygons over the extent of a source raster. It then iterates through each grid cell, cropping and masking the source raster to create individual raster tiles.

Usage

tile_raster(
  raster_layer,
  grid_area,
  grid_shape,
  out_dir = NULL,
  plot_grid = FALSE,
  prompt_user = TRUE,
  acceptable_coverage = 0.5,
  write_grid = TRUE
)

Arguments

raster_layer

A SpatRaster object from the terra package to be tiled.

grid_area

Numeric. The target area for each grid cell in the square meters of the raster's CRS.

grid_shape

Character. The shape of the grid cells. Must be either "square" or "hexagon".

out_dir

Character. Optional path to a directory where the output raster tiles and grid geopackage will be saved. If NULL, no files are written.

plot_grid

Logical. If TRUE, displays a plot of the generated grid overlaid on the raster for review before processing.

prompt_user

Logical. If TRUE and plot_grid is TRUE, the function will pause and ask for user confirmation before proceeding to tile the raster.

acceptable_coverage

Numeric. A value between 0 and 1 representing the minimum proportional overlap a grid cell must have with the raster's extent to be processed. Defaults to 0.5 (50%).

write_grid

Logical. If TRUE and out_dir is specified, the grid polygons will be saved as a geopackage in the output directory.

Value

A list containing two elements:

raster_tiles

A named list of SpatRaster objects, where each element is a tiled portion of the input raster. The names of the list elements correspond to the unique grid_id of each cell.

grid

An sf object containing the grid polygons used for tiling.

Examples

if (FALSE) { # \dontrun{
if (requireNamespace("lidR", quietly = TRUE)) {

  # Create and tile a 1m CHM from the Megaplot.laz
 output <- system.file("extdata", "Megaplot.laz", package = "lidR") %>%
   lidR::readLAS() %>%
   lidR::rasterize_canopy(res = 1, algorithm = lidR::p2r(subcircle = 0.25)) %>%
   tile_raster(
     grid_area   = 900,         # 30x30m tiles
     grid_shape  = "square",
     acceptable_coverage = 0.95,
     out_dir     = NULL,        # Don't write files to disk
     plot_grid   = FALSE,       # Don't show a plot
     write_grid  = FALSE,
     prompt_user = FALSE
   )

  # Check the raster output (a list of SpatRasters)
  print(output$raster_tiles[[1]])

  # Check the grid polygon output
  print(head(output$grid))
}
} # }