Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mlr-org/mlr3spatial
Spatial objects within the mlr3 ecosystem
https://github.com/mlr-org/mlr3spatial
mlr3 r r-package raster-prediction spatial spatial-modelling
Last synced: 3 days ago
JSON representation
Spatial objects within the mlr3 ecosystem
- Host: GitHub
- URL: https://github.com/mlr-org/mlr3spatial
- Owner: mlr-org
- Created: 2020-01-10T18:28:34.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2024-08-14T09:40:17.000Z (5 months ago)
- Last Synced: 2025-01-14T04:08:09.402Z (11 days ago)
- Topics: mlr3, r, r-package, raster-prediction, spatial, spatial-modelling
- Language: HTML
- Homepage: https://mlr3spatial.mlr-org.com
- Size: 11 MB
- Stars: 43
- Watchers: 3
- Forks: 6
- Open Issues: 4
-
Metadata Files:
- Readme: README.Rmd
- Changelog: NEWS.md
Awesome Lists containing this project
README
---
output: github_document
---```{r, include=FALSE}
library(mlr3)
library(mlr3spatial)
library(terra, exclude = "resample")
library(sf)lgr::get_logger("mlr3")$set_threshold("warn")
lgr::get_logger("mlr3spatial")$set_threshold("warn")
set.seed(1)
options(
datatable.print.nrows = 10,
datatable.print.class = FALSE,
datatable.print.keys = FALSE,
width = 100)
```# mlr3spatial
Package website: [release](https://mlr3spatial.mlr-org.com/) | [dev](https://mlr3spatial.mlr-org.com/dev/)
[![r-cmd-check](https://github.com/mlr-org/mlr3spatial/actions/workflows/r-cmd-check.yml/badge.svg)](https://github.com/mlr-org/mlr3spatial/actions/workflows/r-cmd-check.yml)
[![CRAN status](https://www.r-pkg.org/badges/version/mlr3spatial)](https://CRAN.R-project.org/package=mlr3spatial)
[![StackOverflow](https://img.shields.io/badge/stackoverflow-mlr3-orange.svg)](https://stackoverflow.com/questions/tagged/mlr3)
[![Mattermost](https://img.shields.io/badge/chat-mattermost-orange.svg)](https://lmmisld-lmu-stats-slds.srv.mwn.de/mlr_invite/)*mlr3spatial* is the package for spatial objects within the [`mlr3`](https://mlr-org.com) ecosystem.
The package directly loads data from [`sf`](https://CRAN.R-project.org/package=sf) objects to train any mlr3 learner.
The learner can predict on various raster formats ([`terra`](https://CRAN.R-project.org/package=terra), [`raster`](https://CRAN.R-project.org/package=raster) and [`stars`](https://CRAN.R-project.org/package=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`](https://github.com/mlr-org/mlr3spatiotempcv) for spatiotemporal resampling within mlr3.## Resources
There are sections about spatial data in the [mlr3book](https://mlr3book.mlr-org.com).
* Learn how to [predict](https://mlr3book.mlr-org.com/chapters/chapter13/beyond_regression_and_classification.html#sec-spatial-prediction) a spatial raster image.
* Estimate the performance of a model with [spatial cross-validation](https://mlr3book.mlr-org.com/chapters/chapter13/beyond_regression_and_classification.html#sec-spatiotemporal).The gallery features articles about spatial data in the mlr3 ecosystem.
* Learn the basics with a [land cover classification](https://mlr-org.com/gallery/technical/2023-02-27-land-cover-classification/) of the city of Leipzig.
## Installation
Install the last release from CRAN:
```{r, eval=FALSE}
install.packages("mlr3spatial")
```Install the development version from GitHub:
```{r, eval=FALSE}
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`](https://CRAN.R-project.org/package=terra) package and the vector with the [`sf`](https://CRAN.R-project.org/package=sf) package in the R Session.```{r}
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.
```{r}
task = as_task_classif_st(leipzig, target = "land_cover")
task
```The points are located in the district of Lindenau and Zentrum-West.
Now we train a classification tree on the leipzig task.
```{r}
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.```{r}
land_cover = predict_spatial(leipzig_raster, learner)
```## FAQ
Will mlr3spatial support spatial learners?
Eventually. It is not yet clear whether these would live in mlr3extralearners or in mlr3spatial.
So far there are none yet.Why are there two packages, mlr3spatial and mlr3spatiotempcv?
mlr3spatiotempcv is solely devoted to resampling techniques.
There are quite a few and keeping packages small is one of the development philosophies of the mlr3 framework.
Also back in the days when mlr3spatiotempcv was developed, it was not yet clear how we want to structure additional spatial components such as prediction support for spatial classes and so on.