r-spatialecology/landscapemetrics

options_landscapemetrics(TRUE) causes warnings...

Closed this issue · 6 comments

jszhao commented

options_landscapemetrics(to_disk = TRUE) cause some function of terra package print warning message.

Here is a mwe that demo the problem:

library(landscapemetrics)
library(terra)

# change to_disk to FALSE, the following warning messages disappear
options_landscapemetrics(to_disk = TRUE)

r <- rast(landscape)
z <- spatialize_lsm(r, what = c("lsm_p_area", "lsm_p_circle"))
zz <- rast(lapply(z$layer_1, rast))
plot(zz)
writeRaster(zz, filename = "lsm.tif", overwrite = TRUE)

I can't really reproduce the a warning? But running rast with the lapply function on all output elements removes all values I think and creates an empty raster? I assume you rather want to extract the already present layers?

jszhao commented

The warning message is :PROJ: proj_create: unrecognized format / unknown name (GDAL error 1)

The output of the above code is here:

> library(landscapemetrics)
Starting from v2.0.0, landscapemetrics does not support the 'raster' or 'sp' packages.
    They are replaced by 'terra' and 'sf', respectively. More information
    about the 'terra' package can be found here: https://rspatial.org/index.html.
> library(terra)
terra 1.7.29
> 
> # The following line cause the problem. Maybe I should report it to landscapemetrics package.
> options_landscapemetrics(to_disk = TRUE)
> 
> r <- rast(landscape)
> z <- spatialize_lsm(r, what = c("lsm_p_area", "lsm_p_circle"))
Warning message:
Please use 'check_landscape()' to ensure the input data is valid. 
> zz <- rast(lapply(z$layer_1, rast))
Warning messages:
1: PROJ: proj_create: unrecognized format / unknown name (GDAL error 1) 
2: PROJ: proj_create: unrecognized format / unknown name (GDAL error 1) 
> plot(zz)
Warning messages:
1: PROJ: proj_create: unrecognized format / unknown name (GDAL error 1) 
2: PROJ: proj_create: unrecognized format / unknown name (GDAL error 1) 
> writeRaster(zz, filename = "lsm.tif", overwrite = TRUE)
Warning messages:
1: PROJ: proj_create: unrecognized format / unknown name (GDAL error 1) 
2: PROJ: proj_create: unrecognized format / unknown name (GDAL error 1) 
jszhao commented

I can't really reproduce the a warning? But running rast with the lapply function on all output elements removes all values I think and creates an empty raster? I assume you rather want to extract the already present layers?

In my code, z is the output of spatialize_lsm(r, what = c("lsm_p_area", "lsm_p_circle")), which is a nested list:

$layer_1
$layer_1$lsm_p_area
class      : RasterLayer 
dimensions : 30, 30, 900  (nrow, ncol, ncell)
resolution : 1, 1  (x, y)
extent     : 0, 30, 0, 30  (xmin, xmax, ymin, ymax)
crs        : NA 
source     : memory
names      : value 
values     : 1e-04, 0.0457  (min, max)


$layer_1$lsm_p_circle
class      : RasterLayer 
dimensions : 30, 30, 900  (nrow, ncol, ncell)
resolution : 1, 1  (x, y)
extent     : 0, 30, 0, 30  (xmin, xmax, ymin, ymax)
crs        : NA 
source     : memory
names      : value 
values     : 0.3633802, 0.8112292  (min, max)

running rast with the lapply function just to create a SpatRaster with two layers. The results is something like:

library(raster)
stack(z$layer_1)

The problem is that when to_disk = TRUE, landscapemetrics write the layer $layer_1$lsm_p_area to a temp file with extension .grd/.gri, and rast does not recognize it. It seems a format recognized by raster package.

Can you please tell me which version of landscapemetrics you are using?

jszhao commented

my session info is as follows:

> sessionInfo()
R version 4.3.0 (2023-04-21 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)

Matrix products: default


locale:
[1] LC_COLLATE=Chinese (Simplified)_China.utf8 
[2] LC_CTYPE=Chinese (Simplified)_China.utf8   
[3] LC_MONETARY=Chinese (Simplified)_China.utf8
[4] LC_NUMERIC=C                               
[5] LC_TIME=Chinese (Simplified)_China.utf8    

time zone: Asia/Shanghai
tzcode source: internal

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

other attached packages:
[1] terra_1.7-29           landscapemetrics_1.5.6

loaded via a namespace (and not attached):
[1] compiler_4.3.0   sp_1.6-0         Rcpp_1.0.10      raster_3.6-20   
[5] codetools_0.2-19 grid_4.3.0       lattice_0.21-8  
jszhao commented

The latest version of raster package on github fixes this problem. I think this issue could be closed. Thanks.