Skip to contents

Creates a random sample of grid cells from a raster. Uses the input WhiteboxRaster to determine grid dimensions and georeference information for the output.

The output grid will contain the specified number of non-zero grid cells, randomly distributed throughout the raster. Each sampled cell will have a unique value from 1 to num_samples, with background cells set to zero.

Usage

wbw_random_sample(x, num_samples = 1000L)

Arguments

x

Raster object of class WhiteboxRaster. See wbw_read_raster() for more details.

num_samples

integer, number of random samples. Must not exceed the total number of valid cells in the input raster (see num_cells()).

Value

WhiteboxRaster object

Details

This tool is useful for statistical analyses of raster data where a random sampling approach is needed. The sampling process only considers valid, non-NoData cells from the input raster.

Examples

f <- system.file("extdata/dem.tif", package = "wbw")
wbw_read_raster(f) |>
  wbw_random_sample(num_samples = 100)
#> +-----------------------------------------------+ 
#> | WhiteboxRaster                                |
#> | dem.tif                                       |
#> |...............................................| 
#> | bands       : 1                               |
#> | dimensions  : 726, 800  (nrow, ncol)          |
#> | resolution  : 5.002392, 5.000243  (x, y)      |
#> | EPSG        : 2193  (Linear_Meter)            |
#> | extent      : 1925449 1929446 5582091 5585717 |
#> | min value   : 0.000000                        |
#> | max value   : 100.000000                      |
#> +-----------------------------------------------+