-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
--- | ||
title: "Categories in Raster Data" | ||
description: | | ||
Learn how to work with categorial raster data and how to transform the rastervalues into a numerical values. | ||
categories: | ||
- terra | ||
- raster | ||
- spatial | ||
- tutorial | ||
author: | ||
- name: Aimara Planillo & Moritz Wenzler-Meya | ||
affiliation: IZW Berlin | ||
affiliation_url: https://ecodynizw.github.io/EcoDynIZW | ||
date: 2023-11-09 | ||
output: | ||
distill::distill_article: | ||
self_contained: false | ||
toc: true | ||
draft: true | ||
--- | ||
|
||
```{r setup, include=FALSE} | ||
knitr::opts_chunk$set(echo = TRUE) ## keep this, add other settings if needed | ||
``` | ||
|
||
Please write some accompanying text for each chunk explaining the reason and/or code and output. Leave an empty line between the text and the chunk. | ||
|
||
```{r install} | ||
library(terra) | ||
``` | ||
|
||
If you want to show pseudo code (code that should not run but you want to show the code as an example), please also add the respective output (the returned values in the console) as text to the same chunk. Use `eval=FALSE` as a chunk option to include (but not run) the code output. | ||
|
||
```{r example, eval=FALSE} | ||
ras <- rast(matrix(rep(c("a", "b", "c"), each = 3), nrow = 3, ncol = 3)) | ||
ras | ||
as.data.frame(cats(ras)) | ||
plot(ras) | ||
``` | ||
|
||
lets say you want to reclassify the data, you can add another coulumn by using this: | ||
|
||
```{r plot, echo=FALSE} | ||
recl_df <- data.frame(value = c(1,2,3), | ||
label = c("a", "b", "c"), | ||
new_label = c("d", "e", "f")) | ||
ras_new_cat <- categories(ras, | ||
layer = 1, | ||
value = recl_df, | ||
active = 2) # column 2 (do not count the value column! It has to be numeric) | ||
plot(ras_new_cat) | ||
``` | ||
|
||
Let's say we want to reclassify the spatRaster with a numeric value, we have to take one more step | ||
|
||
```{r} | ||
recl_df_num <- data.frame(value = c(1,2,3), | ||
label = c("a", "b", "c"), | ||
new_label = c("d", "e", "f"), | ||
new_value = c(4, 5, 6)) | ||
ras_new_cat_2 <- categories(ras, | ||
layer = 1, | ||
value = recl_df_num, | ||
active = 3) # column 2 (do not count the value column! It has to be numeric) | ||
plot(ras_new_cat_2) | ||
ras_new_num <- catalyze(ras_new_cat_2) | ||
ras_new_num <- ras_new_num$new_value | ||
plot(ras_new_num, type = "continuous") | ||
plot(rast(matrix(rep(c(1,2,3,4,5,6), each = 4), nrow = 9, ncol = 9)), type = "continuous") | ||
``` | ||
|
||
|
||
|
||
Please knit the whole document in the end, to test if it is working! | ||
|
||
# old code | ||
|
||
```{r} | ||
# reclassify clc table | ||
clc_newclasses <- read.csv(paste0(geoproc_wd, "/clc_reclassification.csv")) | ||
head(clc_newclasses) | ||
unique(clc_newclasses$clc_reclass) | ||
clc_newclasses$CODE_18 <- as.character(clc_newclasses$CODE_18) | ||
## get table of values in the raster | ||
clc2 <-clc | ||
plot(clc2) | ||
clc_data <- as.data.frame(cats(clc2)) | ||
## add our new columns | ||
clc_new <- clc_data %>% | ||
left_join(clc_newclasses, by =c("LABEL3", "CODE_18")) | ||
## assign values of the new columns to a new raster | ||
clc_fct <- categories(clc2, layer = 1, value = clc_new, active = 7) # column 7 of data: clc_fct without counting the "Values" column | ||
plot(clc_fct) | ||
## or only the numbered coded factors | ||
clc_new2 <- clc_new %>% | ||
dplyr::select(Value, clc_fct) | ||
clc_fct2 <- categories(clc2, layer = 1, value = clc_new2, active = 1) # column 7 of data: clc_fct without counting the "Values" column | ||
plot(clc_fct2) | ||
``` | ||
|