0

I get the Error: cannot allocate vector of size 1003.7 Mb while doing calculations with rasters in a loop. The strange thing is, that my biggest raster only has 7000KB. The Code I am using is:

list files from folder

ultrablue <- list.files(path = "C:/Users/Felix/Desktop/Bachelorarbeit/Daten/Datenverarbeitung_R/L1C_clipped_copied",
                    pattern = 'B01.jp2$', full.names=T)
blue <- list.files(path = "C:/Users/Felix/Desktop/Bachelorarbeit/Daten/Datenverarbeitung_R/L1C_clipped_copied",
               pattern = 'B02.jp2$', full.names=T)
green <- list.files(path = "C:/Users/Felix/Desktop/Bachelorarbeit/Daten/Datenverarbeitung_R/L1C_clipped_copied",
                pattern = 'B03.jp2$', full.names=T)
red <- list.files(path = "C:/Users/Felix/Desktop/Bachelorarbeit/Daten/Datenverarbeitung_R/L1C_clipped_copied",
              pattern = 'B04.jp2$', full.names=T)
redge1 <- list.files(path = "C:/Users/Felix/Desktop/Bachelorarbeit/Daten/Datenverarbeitung_R/L1C_clipped_copied",
                 pattern = 'B05.jp2$', full.names=T)
redge2 <- list.files(path = "C:/Users/Felix/Desktop/Bachelorarbeit/Daten/Datenverarbeitung_R/L1C_clipped_copied",
                 pattern = 'B06.jp2$', full.names=T)
redge3 <- list.files(path = "C:/Users/Felix/Desktop/Bachelorarbeit/Daten/Datenverarbeitung_R/L1C_clipped_copied",
                 pattern = 'B07.jp2$', full.names=T)
NNIR <- list.files(path = "C:/Users/Felix/Desktop/Bachelorarbeit/Daten/Datenverarbeitung_R/L1C_clipped_copied",
               pattern = 'B8A.jp2$', full.names=T)

define function which is used in loop

  rangeFun <- function(x){
  x[x > 10000 | x < 0] <- NA
  return(x)
}

loop

   for(i in seq_along(ultrablue))
   {

  range_B1 <- rangeFun(raster(ultrablue[i]))
  range_B2 <- rangeFun(raster(blue[i]))
  range_B3 <- rangeFun(raster(green[i]))
  range_B4 <- rangeFun(raster(red[i]))
  range_B5 <- rangeFun(raster(redge1[i]))
  range_B6 <- rangeFun(raster(redge2[i]))
  range_B7 <- rangeFun(raster(redge3[i]))
  range_B8A <- rangeFun(raster(NNIR[i]))

create names

  sat = 'S2_'
  path = substr(ultrablue[i], 84, 90)
  date = substr(ultrablue[i], 91, 98)

save files

 setwd("C:/Users/Felix/Desktop/Bachelorarbeit/Daten/Datenverarbeitung_R/L1C_clip_range")
  writeRaster(range_B1,filename = paste0(sat, path, date,'_B1','.tif'))
  writeRaster(range_B2,filename = paste0(sat, path, date,'_B2','.tif'))
  writeRaster(range_B3,filename = paste0(sat, path, date,'_B3','.tif'))
  writeRaster(range_B4,filename = paste0(sat, path, date,'_B4','.tif'))
  writeRaster(range_B5,filename = paste0(sat, path, date,'_B5','.tif'))
  writeRaster(rrange_B6,filename = paste0(sat, path, date,'_B6','.tif'))
  writeRaster(range_B7,filename = paste0(sat, path, date,'_B7','.tif'))
  writeRaster(range_B8A,filename = paste0(sat, path, date,'_B8A','.tif'))

  removeTmpFiles(h=0.1)
  }

I dont really understand at what part of my code a vector with the size of 1003,7mB is created.

1 Answer 1

0

I do not know why this happens. But it seems there are ways to make your code run better, although it is tricky to help given that you provide no data and incomplete code.

In stead of rangeFun I would suggest using memory-safe function reclassify like this:

m <- rbind(c(-Inf, 0, NA), c(1000, Inf, NA))
reclassify(x, m, filename="")

It seems that your entire script could be reduced to something like this:

inf <- list.files(pattern = 'B..\\.jp2$', full.names=TRUE)
path <- substr(inf, 84, 90)
date <- substr(inf, 91, 98)
band <- gsub('0', '', substr(basename(inf), 1, 3))
outf <- paste0('S2_', path, date, '_', band ,'.tif')
m <- rbind(c(-Inf, 0, NA), c(1000, Inf, NA))

for (i in seq_along(inf)) {
    y <- reclassify(raster(inf[i]), m, filename=outf[i], overwrite=TRUE)
}
1
  • Your code is easier, faster and takes less memory so I can actually use my computer while the R.session is running. Thank you very much Robert. I think its not the first time you help me Commented Oct 18, 2018 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.