Alexy ROSA
Alexy ROSA

Reputation: 1

Terra does not handler correctly multicategorical rasters

I'm currently using terra to manipulate raster. One of them is categorical with bedrock type.

class       : SpatRaster 
dimensions  : 4346, 5235, 1  (nrow, ncol, nlyr)
resolution  : 0.008333333, 0.008333333  (x, y)
extent      : -10.62514, 32.99986, 34.99986, 71.21653  (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (EPSG:4326) 
source      : lithology.tif 
categories  : xx 
name        : xx 
min value   : sc 
max value   : ig 

enter image description here

but when I try to plot it, I get:

plot(x)
#[plot] unknown categories in raster values

I think it might be the symptome of something that isn't working correctly. Because after, i have troubles with multiple models that use this raster.

Here are the details of my R session and of my terra version :

R version 4.3.2 (2023-10-31)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.6 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/atlas/libblas.so.3.10.3 
LAPACK: /usr/lib/x86_64-linux-gnu/atlas/liblapack.so.3.10.3;  LAPACK version 3.9.0

locale:
 [1] LC_CTYPE=fr_FR.UTF-8       LC_NUMERIC=C               LC_TIME=fr_FR.UTF-8       
 [4] LC_COLLATE=fr_FR.UTF-8     LC_MONETARY=fr_FR.UTF-8    LC_MESSAGES=fr_FR.UTF-8   
 [7] LC_PAPER=fr_FR.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=fr_FR.UTF-8 LC_IDENTIFICATION=C       

time zone: Europe/Paris
tzcode source: system (glibc)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] terra_1.7-71 sf_1.0-15    abind_1.4-5 

loaded via a namespace (and not attached):
 [1] vctrs_0.6.5        cli_3.6.2          rlang_1.1.3        DBI_1.2.1          processx_3.8.3    
 [6] KernSmooth_2.23-22 generics_0.1.3     glue_1.7.0         e1071_1.7-14       pkgbuild_1.4.3    
[11] ps_1.7.6           fansi_1.0.6        grid_4.3.2         tibble_3.2.1       classInt_0.4-10   
[16] lifecycle_1.0.4    compiler_4.3.2     dplyr_1.1.4        codetools_0.2-19   pkgconfig_2.0.3   
[21] Rcpp_1.0.12        rstudioapi_0.15.0  R6_2.5.1           tidyselect_1.2.0   utf8_1.2.4        
[26] class_7.3-22       parallel_4.3.2     pillar_1.9.0       curl_5.2.0         callr_3.7.3       
[31] magrittr_2.0.3     tools_4.3.2        proxy_0.4-27       units_0.8-5        remotes_2.4.2.1   
[36] desc_1.4.3     

terra::gdal(lib="")
    gdal     proj     geos 
 "3.4.3"  "8.2.0" "3.10.2" 

Does anyone have an idea how to solve this problem ?

Upvotes: 0

Views: 108

Answers (1)

Robert Hijmans
Robert Hijmans

Reputation: 47481

A categorical raster stores integer values (keys) such as 1, 2, 3 and a table that matches these keys to labels such as "sc", "ss". The message (presumably it is a warning) tells you that there are values in the raster that do not appear in the table. So these values cannot be interpreted (cannot be added to the legend). You could fix this by adding this/these keys to the categories (see ?terra::levels).

Here is a minimal, self-contained reproducible example of what I think you are seeing.

library(terra)
set.seed(0)
r <- rast(nrows=10, ncols=10)
values(r) <- sample(3, ncell(r), replace=TRUE)
levels(r) <- data.frame(id=c(1,3), cover=c("forest", "water"))
plot(r)
#Warning message:
#[plot] unknown categories in raster values 

This is because there is no label for cell value "2".

levels(r)[[1]]
#  id  cover
#1  1 forest
#2  3  water

But as you can see above, "terra" handles that. I could show how to add that missing key, and that would probably fix things for you, but I cannot be sure. You say that you have "troubles", but you are not saying what they are; so I would like to know what these troubles are to understand if I can fix the problem.

Upvotes: 1

Related Questions