Skip to contents

This function allows to directly predict mlr3 learners on various spatial objects.

Usage

predict_spatial(
  task,
  learner,
  chunksize = 200L,
  format = "terra",
  filename = NULL
)

Arguments

task

(Task). Task with DataBackendRaster.

learner

(Learner). Learner with trained model.

chunksize

(integer(1))
The chunksize determines in how many subparts the prediction task will be split into. The value can be roughly thought of as megabyte of a raster file on disk. For example, if a prediction on a 1 GB file would be carried out with chunksize = 100L, the prediction would happen in 10 chunks.

The default of chunksize = 1000L might be a good compromise between speed and memory usage. If you find yourself running out of memory, reduce this value.

format

(character(1))
Output class of the resulting object. Accepted values are "raster", "stars" and "terra" if the input is a DataBackendRaster. Note that when choosing something else than "terra", the spatial object is converted into the respective format which might cause overhead both in runtime and memory allocation.

filename

(character(1))
Path where the spatial object should be written to.

Value

Spatial object of class given in argument format.

Examples

library(terra, exclude = "resample")
#> terra 1.6.7

# fit rpart on training points
task_train = tsk("leipzig")
learner = lrn("classif.rpart")
learner$train(task_train)

# load raster and convert to task
stack = rast(system.file("extdata", "leipzig_raster.tif", package = "mlr3spatial"))
task_predict = as_task_unsupervised(stack, id = "leipzig")

# predict land cover classes
pred = predict_spatial(task_predict, learner, chunksize = 1L)