Skip to contents

Divide a SpatRaster into "tiles". The cells of another SpatRaster (normally with a much lower resolution) or a SpatVector with polygon geometry can be used to define the tiles. You can also provide one or two numbers to indicate the number of rows and columns per tile.

getTileExtents returns the extents of the (virtual) tiles, while makeTiles creates files for the tiles and returns their filenames.

Usage

# S4 method for class 'SpatRaster'
makeTiles(x, y, filename="tile_.tif", extend=FALSE,
    na.rm=FALSE, buffer=0, overwrite=FALSE, ...)

# S4 method for class 'SpatRaster'
getTileExtents(x, y, extend=FALSE, buffer=0)

Arguments

x

SpatRaster

y

SpatRaster or SpatVector defining the zones; or numeric specifying the number of rows and columns for each zone (1 or 2 numbers if the number of rows and columns is not the same)

filename

character. Output filename template. Filenames will be altered by adding the tile number for each tile

extend

logical. If TRUE, the extent of y is expanded to assure that it covers all of x

na.rm

logical. If TRUE, tiles with only missing values are ignored

buffer

integer. The number of additional rows and columns added to each tile. Can be a single number, or two numbers to specify a separate number of rows and columns. This allows for creating overlapping tiles that can be used for computing spatial context dependent values with e.g. focal. The expansion is only inside x, no rows or columns outside of x are added

overwrite

logical. If TRUE, existing tiles are overwritten; otherwise they are skipped (without error or warning)

...

additional arguments for writing files as in writeRaster

Value

character (filenames) or matrix (extents)

See also

vrt to create a virtual raster from tiles and crop for sub-setting arbitrary parts of a SpatRaster.

Examples

r <- rast(ncols=100, nrows=100)
values(r) <- 1:ncell(r)
x <- rast(ncols=2, nrows=2)

getTileExtents(r, x)
#>               xmin         xmax          ymin          ymax
#> [1,] -1.800000e+02 4.440892e-15 -2.220446e-15  9.000000e+01
#> [2,]  4.440892e-15 1.800000e+02 -2.220446e-15  9.000000e+01
#> [3,] -1.800000e+02 4.440892e-15 -9.000000e+01 -2.220446e-15
#> [4,]  4.440892e-15 1.800000e+02 -9.000000e+01 -2.220446e-15
getTileExtents(r, x, buffer=3)
#>        xmin  xmax  ymin ymax
#> [1,] -190.8  10.8  -5.4 95.4
#> [2,]  -10.8 190.8  -5.4 95.4
#> [3,] -190.8  10.8 -95.4  5.4
#> [4,]  -10.8 190.8 -95.4  5.4


filename <- paste0(tempfile(), "_.tif")
ff <- makeTiles(r, x, filename)
ff
#> [1] "/var/folders/t_/mmhnh941511_hp2lwh383bp00000gn/T//RtmpS6ajai/file3ba219030f74_1.tif"
#> [2] "/var/folders/t_/mmhnh941511_hp2lwh383bp00000gn/T//RtmpS6ajai/file3ba219030f74_2.tif"
#> [3] "/var/folders/t_/mmhnh941511_hp2lwh383bp00000gn/T//RtmpS6ajai/file3ba219030f74_3.tif"
#> [4] "/var/folders/t_/mmhnh941511_hp2lwh383bp00000gn/T//RtmpS6ajai/file3ba219030f74_4.tif"

vrt(ff)
#> class       : SpatRaster 
#> dimensions  : 100, 100, 1  (nrow, ncol, nlyr)
#> resolution  : 3.6, 1.8  (x, y)
#> extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326) 
#> source      : spat_3ba26ba412c4_15266_CjcR62DykkoqBId.vrt 
#> name        : spat_3ba26ba412c4_15266_CjcR62DykkoqBId 
#> min value   :                                       1 
#> max value   :                                   10000