简体   繁体   中英

intialize function of terra package produces fatal error

I am using the initialize function in the terra package to create a raster in RStudio. According to the documentation , this function should work with for large rasters, but I am getting a fatal error in RStudio when I try to create a 10,000 x 10,000 raster with a single value for all cells.

Using the example code works as expected

r <- rast(ncols=10, nrows=5, xmin=0, xmax=10, ymin=0, ymax=5)
z <- init(r, fun=8) # works

However, when I increase the dimensions to 10k x 10k, the fatal error occurs.

r <- rast(ncols=10000, nrows=10000, xmin=0, xmax=10000, ymin=0, ymax=10000)
z <- init(r, fun=8) # crashes

Watching the Activity Monitor on my Macbook Pro shows that memory pressure spikes when running the code with the RStudio process exceeding 80GB of RAM. I have 16GB of RAM on my computer so I'm thinking the problem may be related to some memory issue?

I'm not sure if the function is not working as intended or if the issue is with RStudio. Or am I doing something wrong?

Any help would be greatly appreciated.

My specs:
OS: Monterey 12.0.1
Chip: Apple M1 Pro.
R version: 4.1.2 (2021-11-01)
RStudio version: 2021.9.1.372 "Ghost Orchid"

Edit:

terra version: 1.4.22

I tried two solutions recommended by rhijmans in the link from @FKneip but both resulted in the same fatal error as before.

r <- rast(ncols=10000, nrows=10000) # works

values(r) <- 1 # crashes
z <- init(r, 1, filename="test.tif", datatype="INT1U") # crashes

This is not a true solution to the problem but it is a temporary workaround that I just found. Use the classify function in terra to reclassify an empty raster (see below). This also worked to reclassify an existing 10k x 10k raster already populated with values. See ?classify for details on how to enter a matrix for the rcl= argument depending on the values of your raster.

r <- rast(ncols=10000, nrows=10000)
r
class       : SpatRaster 
dimensions  : 10000, 10000, 1  (nrow, ncol, nlyr)
resolution  : 0.036, 0.018  (x, y)
extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 

x <- classify(r, rcl = matrix(c(NA,1), 1, 2, byrow = TRUE), right=NA) # does not crash
x
class       : SpatRaster 
dimensions  : 10000, 10000, 1  (nrow, ncol, nlyr)
resolution  : 0.036, 0.018  (x, y)
extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 
source      : memory 
name        : lyr.1 
min value   :     1 
max value   :     1 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM