Skip to contents

This function allows to directly predict mlr3 learners on various spatial objects (see section "Supported Spatial Classes"). It returns an mlr3::Prediction object and (optionally) the same object that was used for the prediction.


  chunksize = 200L,
  format = "terra",
  filename = NULL







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.


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. For a DataBackendVector, the output class will always be sf::sf.


Path where the spatial object should be written to.


Spatial object of class given in argument format.


When parallelizing the prediction via future, plan "multisession" will not work due to external pointers within the spatial object. If the execution platform is UNIX-based, plan("multicore") is recommended. For Windows users, plan(future.callr::callr) might be an alternative.


stack = demo_stack_spatraster(size = 1)
value = data.table::data.table(ID = c(0, 1), y = c("negative", "positive"))
terra::set.cats(stack, layer = "y", value = value)

# create backend
backend = as_data_backend(stack)
task = as_task_classif(backend, target = "y", positive = "positive")
# train
learner = lrn("classif.featureless")
learner$train(task, row_ids = sample(1:task$nrow, 50))
ras = predict_spatial(task, learner)
#> class       : SpatRaster 
#> dimensions  : 223, 223, 1  (nrow, ncol, nlyr)
#> resolution  : 1, 1  (x, y)
#> extent      : 0, 223, 0, 223  (xmin, xmax, ymin, ymax)
#> coord. ref. :  
#> source      : filec47a3d47153.tif 
#> name        : lyr.1 
#> min value   :     1 
#> max value   :     1