Skip to contents

Wrappers around terra::getTileExtents() that return a list of named numeric vectors describing the extents of tiles rather than SpatExtent objects. While these may have general use, they are intended primarily for supplying to the tile_fun argument of tar_terra_tiles().

Usage

tile_grid(raster, ncol, nrow)

tile_blocksize(raster)

tile_n(raster, n)

Arguments

raster

a SpatRaster object

ncol

integer; number of columns to split the SpatRaster into

nrow

integer; number of rows to split the SpatRaster into

n

integer; total number of tiles to split the SpatRaster into

Value

list of named numeric vectors with xmin, xmax, ymin, and ymax values that can be coerced to SpatExtent objects with terra::ext().

Details

tile_blocksize() creates extents using the raster's native blocksize (see terra::fileBlocksize()), which should be more memory efficient. tile_grid() allows specification of a number of rows and columns to split the raster into. E.g. nrow = 2 and ncol = 2 would create 4 tiles (because it specifies a 2x2 matrix, which has 4 elements).

Author

Eric Scott

Examples

f <- system.file("ex/elev.tif", package="terra")
r <- terra::rast(f)
tile_grid(r, ncol = 2, nrow = 2)
#> [[1]]
#>      xmin      xmax      ymin      ymax 
#>  5.741667  6.141667 49.816667 50.191667 
#> 
#> [[2]]
#>      xmin      xmax      ymin      ymax 
#>  6.133333  6.533333 49.816667 50.191667 
#> 
#> [[3]]
#>      xmin      xmax      ymin      ymax 
#>  5.741667  6.141667 49.441667 49.816667 
#> 
#> [[4]]
#>      xmin      xmax      ymin      ymax 
#>  6.133333  6.533333 49.441667 49.816667 
#> 
tile_blocksize(r)
#> [[1]]
#>      xmin      xmax      ymin      ymax 
#>  5.741667  6.533333 49.833333 50.191667 
#> 
#> [[2]]
#>      xmin      xmax      ymin      ymax 
#>  5.741667  6.533333 49.475000 49.833333 
#> 
#> [[3]]
#>      xmin      xmax      ymin      ymax 
#>  5.741667  6.533333 49.441667 49.475000 
#> 
tile_n(r, 8)
#> creating 2 * 4 = 8 tile extents
#> [[1]]
#>      xmin      xmax      ymin      ymax 
#>  5.741667  5.941667 49.816667 50.191667 
#> 
#> [[2]]
#>      xmin      xmax      ymin      ymax 
#>  5.941667  6.133333 49.816667 50.191667 
#> 
#> [[3]]
#>      xmin      xmax      ymin      ymax 
#>  6.141667  6.333333 49.816667 50.191667 
#> 
#> [[4]]
#>      xmin      xmax      ymin      ymax 
#>  6.333333  6.533333 49.816667 50.191667 
#> 
#> [[5]]
#>      xmin      xmax      ymin      ymax 
#>  5.741667  5.941667 49.441667 49.816667 
#> 
#> [[6]]
#>      xmin      xmax      ymin      ymax 
#>  5.941667  6.133333 49.441667 49.816667 
#> 
#> [[7]]
#>      xmin      xmax      ymin      ymax 
#>  6.141667  6.333333 49.441667 49.816667 
#> 
#> [[8]]
#>      xmin      xmax      ymin      ymax 
#>  6.333333  6.533333 49.441667 49.816667 
#> 

if (FALSE) { # \dontrun{
#usage with tar_terra_tiles
list(
    tar_terra_rast(
        my_map,
        terra::rast(system.file("ex/logo.tif", package = "terra"))
    ),
    tar_terra_tiles(
        name = rast_split,
        raster = my_map,
        tile_fun = tile_blocksize
    ),
    tar_terra_tiles(
        name = rast_split_grid,
        raster = my_map,
        tile_fun = \(x) tile_grid(x, ncol = 2, nrow = 2)
    ),
    tar_terra_tiles(
        name = rast_split_n,
        raster = my_map,
        tile_fun = \(x) tile_n(x, n = 6)
    )
)
} # }