Convert vector data into a raster using GDAL
Source:R/terra_gdal_rasterize.R
terra_gdal_rasterize.Rd
This function is a wrapper for gdalUtilities::gdal_rasterize()
for use with
terra objects.
Usage
terra_gdal_rasterize(
x,
sf,
burn = 1,
init = 0,
invert = FALSE,
update = FALSE,
touches = FALSE,
n_threads = 1,
filename = tempfile(fileext = ".tif"),
sf_filename = tempfile(fileext = ".gpkg"),
datatype = "FLT4S",
cache_limit = 200,
tiled = FALSE,
bigtiff = FALSE,
nbits = NULL,
compress = "LZW",
NAflag = NULL,
verbose = TRUE,
output_raster = TRUE
)
Arguments
- x
terra::rast()
Raster object with source data.- sf
sf::st_sf()
Spatial object to rasterize.- burn
numeric
Value for encoding the vector data. Defaults to 1.- init
numeric
Value for encoding background cells that do not overlap with the vector data. Defaults to 0.- invert
logical
Should the burn process be inverted? Defaults toFALSE
.- update
logical
Should the result by producing by updating the argument tox
? IfFALSE
then the argument tox
is only used to specify the spatial properties of the resulting raster (i.e., values have on the result), Defaults toFALSE
.- touches
logical
Should cells ofx
that are overlap with any part ofsf
be treated as covered bysf
? Defaults toFALSE
, such that only cells that have their centroid covered bysf
are treated as covered.- n_threads
integer
Number of computational threads to use for data processing. To reduce run time, it is strongly recommended to set this parameter based on available resources (see Examples section below). Defaults to 1.- filename
character
Filename for output raster. Defaults totempfile(fileext = ".tif")
.- sf_filename
character
File name to temporarily save argument tosf
. Defaults to a temporary (geopackage) file.- datatype
character
Value indicating the data type for saving data. Defaults to"FLT4S"
.- cache_limit
integer
Number of MB to use for GDAL caching. Defaults to 200.- tiled
logical
Value indicating if GeoTIFF files should be tiled. Defaults toFALSE
.- bigtiff
logical
Value indicating the data should be stored in BIGTIFF format. Defaults toFALSE
.- nbits
integer
Number of bits for output data. Defaults toNULL
such that the number of bits is automatically determined.- compress
character
Value indicating compression format. Available options include"LZW"
and"DEFLATE"
. Defaults to"LZW"
.- NAflag
numeric
Value for representing missing (NA
) values. A"none"
value can also be used to indicate that no flag should be set. Defaults toNULL
such that the value is determined automatically.- verbose
logical
Should information be displayed during processing? Defaults toTRUE
.- output_raster
logical
Should a raster (terra::rast()
) object be returned? IfFALSE
then the file path for the resulting file is returned. Defaults toTRUE
.
Value
A terra::rast()
raster object.
Examples
# please ensure that the gdalUtilities package is installed
# to run this example
# import vector data
f <- system.file("ex/lux.shp", package = "terra")
sf <- read_sf(f)
# create template raster
x <- rast(vect(sf), ncols = 75, nrows = 100)
x <- terra::setValues(x, runif(terra::ncell(x)))
# rasterize vector data
z <- terra_gdal_rasterize(x, sf, burn = 5)
# plot result
plot(z)