helper function used to quickly create a rasterStack object with specified dimensions (useful for testing purposes).

build_testraster(nrows = 100, ncols = 100, nbands = 1,
  with_nodata = TRUE, to_file = FALSE, out_file = NULL, ...)

Arguments

nrows

numeric number of rows, Default: 100

ncols

numeric number of columns, Default: 100

nbands

numeric number of bands, Default: 1

with_nodata

PARAM_DESCRIPTION, Default: TRUE

to_file

logical if TRUE, the rasterStack is saved to a TIFF file, Default: FALSE

out_file

character filename where the test raster should be saved (ignored if to_file = NULL). If out_file is NULL, the file is saved in R temporary folder, otherwise the filename specified in out_file is used. In both cases, the name of the created TIFF file is returned instead than the rasterStack itself, Default: NULL.

...

Any other arguments to be passed to raster::raster while creating the output raster.

Value

Either the "test" raster in rasterStack format, or the filename where it was saved (if tofile = TRUE)

Note

  • Values of the test raster are integers between 1 and 250.

  • If out_file is selected, the file is saved as a 8-bit unsigned integer tiff to save space.

  • By default, the raster is created using a WGS84 lat/lon reference system, with extent corresponding to the entire globe (bbox = -180, 180; -90, 90). To specify a different projection and extent, specify them by passing the crs and ext arguments to `raster (see examples).

Examples

# create a rasterstack of 100*100 cells and 10 bands and put it in test_rast test_rast <- build_testraster(100,100,10) test_rast
#> class : RasterStack #> dimensions : 100, 100, 10000, 10 (nrow, ncol, ncell, nlayers) #> resolution : 3.6, 1.8 (x, y) #> extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax) #> coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 #> names : band_1, band_2, band_3, band_4, band_5, band_6, band_7, band_8, band_9, band_10 #> min values : 1, 2, 2, 2, 2, 2, 2, 2, 2, 2 #> max values : 50, 100, 100, 100, 100, 100, 100, 100, 100, 100 #>
# create a rasterstack of 100*100 cells and 20 bands and save it to a temporary TIFF file test_rast <- build_testraster(100,100,20, to_file = TRUE) test_rast
#> [1] "/tmp/Rtmpzfhi9J/file7852106142ee.tif"
# create a rasterstack of 100*100 cells and 10 in epsg:3857 projection on a given extent build_testraster(100,100,10, crs = "+init=epsg:3857", ext = raster::extent(c(0, 200000, 0, 200000)))
#> class : RasterStack #> dimensions : 100, 100, 10000, 10 (nrow, ncol, ncell, nlayers) #> resolution : 2000, 2000 (x, y) #> extent : 0, 2e+05, 0, 2e+05 (xmin, xmax, ymin, ymax) #> coord. ref. : +init=epsg:3857 +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs #> names : band_1, band_2, band_3, band_4, band_5, band_6, band_7, band_8, band_9, band_10 #> min values : 1, 2, 2, 2, 2, 2, 2, 2, 2, 2 #> max values : 50, 100, 100, 100, 100, 100, 100, 100, 100, 100 #>
test_rast
#> [1] "/tmp/Rtmpzfhi9J/file7852106142ee.tif"