Learn R Programming

mlr3spatial

Package website: release | dev

mlr3spatial is the package for spatial objects within the mlr3 ecosystem. The package directly loads data from sf objects to train any mlr3 learner. The learner can predict on various raster formats (terra, raster and stars) and writes the prediction raster to disk. mlr3spatial reads large raster objects in chunks to avoid memory issues and predicts the chunks in parallel. Check out mlr3spatiotempcv for spatiotemporal resampling within mlr3.

Resources

There are sections about spatial data in the mlr3book.

The gallery features articles about spatial data in the mlr3 ecosystem.

Installation

Install the last release from CRAN:

install.packages("mlr3spatial")

Install the development version from GitHub:

remotes::install_github("mlr-org/mlr3spatial")

Example

Our goal is to map the land cover of the city of Leipzig. The mlr3spatial package contains a Sentinel-2 scene of the city of Leipzig and a point vector with training sites. The Sentinel-2 scene is a 10m resolution multispectral image with 7 bands and the NDVI. The points represent samples of the four land cover classes: Forest, Pastures, Urban and Water. We load the raster with the terra package and the vector with the sf package in the R Session.

library(mlr3verse)
library(mlr3spatial)
library(terra, exclude = "resample")
library(sf)

leipzig = read_sf(system.file("extdata", "leipzig_points.gpkg", package = "mlr3spatial"), stringsAsFactors = TRUE)

leipzig_raster = rast(system.file("extdata", "leipzig_raster.tif", package = "mlr3spatial"))

The function as_task_classif_st() converts the sf::sf object to a spatial classification task.

task = as_task_classif_st(leipzig, target = "land_cover")
task
## 
## ── <TaskClassifST> (97x9) ──────────────────────────────────────────────────────────────────────────
## • Target: land_cover
## • Properties: multiclass
## • Features (8):
##   • dbl (8): b02, b03, b04, b06, b07, b08, b11, ndvi
## * Coordinates:
##            X       Y
##  1: 732480.1 5693957
##  2: 732217.4 5692769
##  3: 732737.2 5692469
##  4: 733169.3 5692777
##  5: 732202.2 5692644
## ---                 
## 93: 733018.7 5692342
## 94: 732551.4 5692887
## 95: 732520.4 5692589
## 96: 732542.2 5692204
## 97: 732437.8 5692300

The points are located in the district of Lindenau and Zentrum-West.

Now we train a classification tree on the leipzig task.

learner = lrn("classif.rpart")
learner$train(task)

As a last step, we predict the land cover class for the whole area of interest. For this, we pass the Sentinel-2 scene and the trained learner to the predict_spatial() function.

land_cover = predict_spatial(leipzig_raster, learner)
## Warning in warn_deprecated("DataBackend$data_formats"): DataBackend$data_formats is deprecated and
## will be removed in the future.

FAQ

Copy Link

Version

Install

install.packages('mlr3spatial')

Monthly Downloads

2,909

Version

0.6.0

License

LGPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Marc Becker

Last Published

July 18th, 2025

Functions in mlr3spatial (0.6.0)

generate_stack

Generate Raster Stack
DataBackendVector

DataBackend for Vector Objects
TaskRegrST

Spatiotemporal Regression Task
DataBackendRaster

DataBackend for Raster Objects
mlr3spatial-package

mlr3spatial: Support for Spatial Objects Within the 'mlr3' Ecosystem
mask_stack

Sample Points in Raster Stack
numeric_layer

Numeric Layer Generator
block_size

Split Raster Into Chunks
write_raster

Write a Raster in Chunks
leipzig

Leipzig Land Cover Task
predict_spatial

Predict on Spatial Objects with mlr3 Learners
sample_stack

Sample Points in Raster Stack
factor_layer

Factor Layer Generator
as_data_backend.stars

Coerce to spatial DataBackend
as_task_classif_st

Convert to a Spatiotemporal Classification Task
TaskClassifST

Spatiotemporal Classification Task
as_task_regr_st

Convert to a Spatiotemporal Regression Task