1

I am looking for a way to improve the speed and lower the memory-usage of the following lines:

  export <- raster(paste0(catch_dir,'/export_streams.rst'))
  catchm_polyg <- readOGR(dsn = catch_dir, layer = 'catchment')
  Model_10 <- extract(export, catchm_polyg, fun = sum, na.rm = TRUE ) 

This gives me the sum of all values from export_streams.rst, with catchm_polyg as an extent.

I want to do this a lot of times for different input-data. Therefor the code is part of a function, which is then used in a foreach loop. That all works fine to a certain degree. The code doesn't work with larger input-data though, as I apparently don't have enough memory (32gb, 64bit R version). Also the calculation time is very high. Any suggestions on how to improve the code?

nowi
  • 53
  • 6

1 Answers1

1

A couple things to speed things up might include some of the following:

  1. Ask yourself: Can I first aggregate my raster to a courser resolution using the sum function?
  2. Memory: Don't always write to the memory when using functions from the raster package. Instead try to write externally when possible or you will get memory errors.

  3. If you have multi-part polygon (a SpatialPolygonDataframe object). Just run the extract function once, then unlist and then run functions.

    # quickly summarise across multiple polygons
    allmyvals <- extract(myrast, myploys)
    myploys$sum_in_poly <- unlist(lapply(allmyvals , function(x) if (!is.null(x)) sum(x, na.rm=TRUE) else NA ))
    
  4. Take an alternative approach out of the raster package or try something with getValues. See these threads:

Community
  • 1
  • 1
Matthew Bayly
  • 556
  • 5
  • 7
  • Thanks for the answer. `extract(myrast, myploys, fun = sum, na.rm = TRUE, filename="myfile.rst", overwrite=TRUE)` This gives me the sum as number. But if I now want to work with this number (e.g. convert to different unit), how do I "grab" that number? Because the code apparently doesn't write a file called "myfile.rst". I could also write `extract(myrast, myploys, fun = sum, na.rm = TRUE, overwrite=TRUE)`. I dont understand where that code actually "writes to file". – nowi Oct 05 '16 at 10:30
  • O right sorry. You're correct writing externally wont work with extract(). I have made edits above accordingly. – Matthew Bayly Oct 05 '16 at 18:42