Function that converts raster and SpatialPolygon* objects to a list of cells that fall within such a region.

regionMask(
  region,
  proj = crs(region),
  id = NULL,
  z_fix = NULL,
  precision = 2,
  ...
)

Arguments

region

An object of class SpatRaster, Raster*, SpatVector, or SpatialPolygon* to convert to "x,y"

proj

A crs object or character string representing the output projection. Default is proj = crs(region) unless proj or z_fix are provided in which case the latter takes precedence.

id

A character string indicating which column in a Spatial* or Spat* contains each feature's unique ID. Otherwise ignored

z_fix

A SpatRaster with the same origin and resolution as the z_fix used to generate the 'world' with makeWorld. Do not modify this parameter if you didn't modify it when you ran makeWorld.

precision

An integer representing the number of decimals to retain in the x and y directions. For grid sizes with nice, round numbers precisions can be low. This factor is controled by rast and must be the same as the one used to generate the 'world' with makeWorld. Default is 2.

...

Additional arguments to pass to fix_z.

Value

A character vector containing all cells that fall in the same location as the input 'region'. If id is provided, a data.table.

Examples

# Generate a DEM
n <- 5
dem <- expand.grid(list(x = 1:(n * 100),
                        y = 1:(n * 100))) / 100
dem <- as.data.table(dem)
dem[, z := 250 * exp(-(x - n/2)^2) + 
      250 * exp(-(y - n/2)^2)]
#>            x    y         z
#>      1: 0.01 0.01 1.0146139
#>      2: 0.02 0.01 1.0404641
#>      3: 0.03 0.01 1.0675194
#>      4: 0.04 0.01 1.0958300
#>      5: 0.05 0.01 1.1254477
#>     ---                    
#> 249996: 4.96 5.00 1.0711366
#> 249997: 4.97 5.00 1.0428260
#> 249998: 4.98 5.00 1.0157707
#> 249999: 4.99 5.00 0.9899205
#> 250000: 5.00 5.00 0.9652271
dem <- rast(dem)
ext(dem) <- c(10000, 20000, 30000, 40000)
crs(dem) <- "+proj=lcc +lat_1=48 +lat_2=33 +lon_0=-100 +datum=WGS84"

# Generate a polygon that falls within the DEM's extent
region <- ext(c(12500,12600,32500,32700))
region <- as.polygons(region)
crs(region) <- crs(dem)

maskedCells <- regionMask(region = region, res = res(dem))