0

everyone, I'm trying to get a 3D map with the rayshader package, but apparently the file is too big to be processed by RStudio. Here is part of the code:

(...)

elevation = raster::merge(srtm,srtm2) #works fine
height_shade(raster_to_matrix(elevation)) %>% plot_map() #works fine
 
piecergb <- raster::stack(piece) #works fine

setValues(piecergb, scales::rescale(values(piecergb), to = c(0, 255)))

   Error: cannot allocate vector of size 11.9 Gb

So, I first checked the capacities of my RStudio:

memory.size()
[1] 27720.21

memory.limit()
[1] 2e+05 ###this I have previously changed with memory.limit(size=200000)

and called gc() to clean my session

gc()
            used   (Mb) gc trigger    (Mb)   max used    (Mb)
Ncells   2766617  147.8    4954078   264.6    4954078   264.6
Vcells 404719527 3087.8 3551300168 27094.3 3774733748 28799.0

I then restarted my RStudio, redefined the memory limit and tried again. But I still get the same error message. Does someone know what to do to fix this problem? I haven't found another solution so far and cannot proceed with the map.

My system info is:

Sys.info()
       sysname        release        version       nodename        machine          login           user effective_user 
     "Windows"       "10 x64"  "build 18363"        "NSF85"       "x86-64"      "install"      "install"      "install" 

Thank you so much in advance

Adriana

Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63

2 Answers2

0

On this line, you use the values to read all cell values from Raster* object piecergb into memory, and then you use setValues to assign them again to a Raster*.

x <- setValues(piecergb, scales::rescale(values(piecergb), to = c(0, 255)))

That leads to problems if your dataset is large. Instead, use methods from the raster package (these avoid that problem). I think that what you are doing is equivalent to

x <- clamp(piecergb, 0, 255)

Also see ?reclassify

Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
  • I tried your approach and it worked, but not for the following step of my script. I tried `raster::plotRGB(xt, scale=255^2)` (where xt is the output of `clamp(piecergb, 0, 255)`) but I get the message `Error in (function (classes, fdef, mtable): unable to find an inherited method for function ‘plotRGB’ for signature ‘"RasterLayer"’` so I think it doesn't do the job for the `rayshader` pipeline, even though I could get around with the memory problem! If you have any additional idea in this regard, I'd be happy to know, but your help was already enough to answer the question I posted! – adriana.cravo Dec 11 '20 at 10:57
  • plotRGB needs three layers (R, G, B, in a RasterStack or RasterBrick). Apparently `piecergb` only has one (it is a RasterLayer). `clamp` does not affect that. – Robert Hijmans Dec 11 '20 at 17:19
-1

Use following code to maximise your memory

library(raster)
rasterOptions(maxmemory = 1e+09)
rasterOptions(tmpdir="temp_files")

Good Luck!

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 17 '22 at 00:39