oscarperpinan / rastervis

The raster package defines classes and methods for spatial raster data access and manipulation. The rasterVis package complements raster providing a set of methods for enhanced visualization and interaction.
oscarperpinan.github.io/rastervis
GNU General Public License v3.0
85 stars 26 forks source link

`levelplot()`: use `terra::cats()` #79

Closed brownag closed 2 years ago

brownag commented 2 years ago

Hello. Thank you for the excellent package.

I recently have been converting raster code to use terra and noticed some issues / changes in terra::levels() v.s. terra::cats(). levels() is a list of vectors containing the levels (no column names/ID), whereas cats() has (list of) ID, labels, colors, and other categories in data.frames (the RAT).

Fixes Error in x@ptr$replaceValues(from, to, -1, opt): Not compatible with requested type: [type=NULL; target=double]. when plotting a categorical SpatRaster with levelplot().

Before

library(spDataLarge)
library(terra)
#> terra 1.5.19
library(rasterVis)
#> Loading required package: lattice

r1 <- spDataLarge::nlcd # 8 category National Land Cover Dataset
t1 <- terra::rast(r1)

rasterVis::levelplot(r1, att=2)

rasterVis::levelplot(t1, att=2)
#> Error in x@ptr$replaceValues(from, to, -1, opt): Not compatible with requested type: [type=NULL; target=double].

After

library(spDataLarge)
library(terra)
#> terra 1.5.19
library(rasterVis)
#> Loading required package: lattice

r1 <- spDataLarge::nlcd # 8 category National Land Cover Dataset
t1 <- terra::rast(r1)

rasterVis::levelplot(r1, att=2)

rasterVis::levelplot(t1, att=2)

brownag commented 2 years ago

With terra_1.5-19, raster_3.5-15 that reprex works for me:

library(raster)
#> Loading required package: sp
library(terra)
#> terra 1.5.19
library(rasterVis)
#> Loading required package: lattice

r <- raster(nrow=10, ncol=10)
r[] = 1
r[51:100] = 3
r[3:6, 1:5] = 5
r <- ratify(r)

rat <- levels(r)[[1]]
rat$landcover <- c('Pine', 'Oak', 'Meadow')
rat$class <- c('A1', 'B2', 'C3')
levels(r) <- rat

t1 <- rast(r)

levelplot(r)

levelplot(t1)

sessionInfo()
#> R version 4.1.0 (2021-05-18)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Pop!_OS 21.04
#> 
#> Matrix products: default
#> BLAS:   /usr/local/lib/R/lib/libRblas.so
#> LAPACK: /usr/local/lib/R/lib/libRlapack.so
#> 
#> locale:
#>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
#>  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
#>  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] rasterVis_0.51.2 lattice_0.20-44  terra_1.5-19     raster_3.5-15   
#> [5] sp_1.4-6        
#> 
#> loaded via a namespace (and not attached):
#>  [1] Rcpp_1.0.7          knitr_1.33          magrittr_2.0.1     
#>  [4] viridisLite_0.4.0   jpeg_0.1-8.1        rlang_0.4.12       
#>  [7] stringr_1.4.0       highr_0.9           tools_4.1.0        
#> [10] rgdal_1.5-27        parallel_4.1.0      grid_4.1.0         
#> [13] xfun_0.25           png_0.1-7           latticeExtra_0.6-29
#> [16] htmltools_0.5.1.1   yaml_2.2.1          digest_0.6.29      
#> [19] RColorBrewer_1.1-2  codetools_0.2-18    evaluate_0.14      
#> [22] rmarkdown_2.11      stringi_1.7.6       compiler_4.1.0     
#> [25] hexbin_1.28.1       zoo_1.8-8
oscarperpinan commented 2 years ago

You are right. Sorry, I checked it with an outdated version of terra.