[R-sig-Geo] Replacing values in a RasterBrick

Michael Sumner mdsumner at gmail.com
Thu Sep 7 10:38:06 CEST 2017


On Thu, 7 Sep 2017 at 08:22 BEUTEL Terry S <Terry.Beutel at daf.qld.gov.au>
wrote:

> Hi.
>
> I'm working with the Raster package. I have a RasterBrick of 374 monthly
> rainfall rasters called Rain2.
>
>
> > Rain2
> class       : RasterBrick
> dimensions  : 374, 315, 117810, 374  (nrow, ncol, ncell, nlayers)
> resolution  : 0.05, 0.05  (x, y)
> extent      : 137.925, 153.675, -29.275, -10.575  (xmin, xmax, ymin, ymax)
> coord. ref. : +proj=longlat +ellps=GRS80 +no_defs
> data source : K:\RainStuff\RainRaster.grd
> names       :    X198601,    X198602,    X198603,    X198604 ...
> min values  :       0.00,       0.00,       0.00,       0.00 ...
> max values  :  962.01929,  534.42188,  929.24780,  790.83087 ...
>
>
>
> I want to replace the 374 monthly rainfall pixel values at a particular
> location (defined by the intersection of a single row and a single column)
> with a different series of 374 values. But when I try the reassignment, I
> run into memory issues....
>
>
As far as I understand it there is no actual partial-write for the this
format, it's actually a complicated topic given the range of ways raster
can store data either on file or in memory.

A Raster in memory is a long-form matrix with the matrix flattened out
(left right top to bottom, different to R's matrix), and the columns are
"layers". On disk, the rasterfile (.grd) is a flat binary with an
orientation ordering (either band sequential, band interleaved by line or
band interleaved by pixel) and so that can't be easily overridden without
knowing the details.

Given the size of the file I would first try pulling it all into memory and
rewriting in whole:

Rain2 <- readAll(Rain2)

## then run your updates
## ...
##  now write back to file

writeRaster(Rain2, "RainRaster_test.grd")

and check.

If that fails, because your system can't perform this (wasteful, brute
force) read and write then you could use ff - it's possible to
transparently map an ff array to an existing RasterBrick (in rasterfile.grd
format) and update it using ff's efficient memory mapping.

Let  me know if you that sounds promising and you want to explore more,
it's a simple thing I should finish up and put on CRAN.

Cheers, Mike.


>
> > Rain2[100,100]<-c(1:374)
>
> Error: cannot allocate vector of size 336.2 Mb
>
>
>
> I've also tried using setValues to reassign the values, as per the
> "Writing functions with the 'raster' package" vignette, but without success
> e.g. ...
>
> > f2<-
>
> + function(x,row,col,a){
>
> + x[row,col]<-setValues(x[row,col],a)
>
> + return(x)}
>
> >
>
> > a<-c(1:374)
>
> > f2(Rain2,100,101,a)
>
>
>
> Error in (function (classes, fdef, mtable)  :
>
>   unable to find an inherited method for function 'setValues' for
> signature '"matrix"'
>
>
>
>
>
> I don't work much with the Raster package and I'm sure I'm making an
> elementary error here, but I'd appreciate any advice on the best way
> replace these values efficiently.
>
> Many thanks
>
> Terry
>
> ------------------------------
> The information in this email together with any attachments is intended
> only for the person or entity to which it is addressed and may contain
> confidential and/or privileged material. There is no waiver of any
> confidentiality/privilege by your inadvertent receipt of this material.
> Any form of review, disclosure, modification, distribution and/or
> publication of this email message is prohibited, unless as a necessary part
> of Departmental business.
> If you have received this message in error, you are asked to inform the
> sender as quickly as possible and delete this message and any copies of
> this message from your computer and/or your computer system network.
> ------------------------------
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
-- 
Dr. Michael Sumner
Software and Database Engineer
Australian Antarctic Division
203 Channel Highway
Kingston Tasmania 7050 Australia

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list