[R-sig-Geo] calc, overlay and raster stacks

Robert J. Hijmans r.hijmans at gmail.com
Fri Nov 19 20:09:20 CET 2010


Agus,

There are some variations, but generally the functions in raster
assess if the data can be loaded, processed and kept in memory. And
they do so if possible. If not, and if no filename argument was
provided, the results will be written to a temporary file. If you do
provide a filename argument, the resulting object won't have the
values in memory (only a reference to them). Setting 'format' and
'datatype' is optional. format can be omitted if you provide a known
extension (e.g. filename='abc.tif' )

Robert


On Fri, Nov 19, 2010 at 12:04 PM, Agustin Lobo <alobolistas at gmail.com> wrote:
> Thanks!
>
> It works with raster_1.6-22 and  sp_0.9-72
>
> The last 2 lines must be
> fun1 <- function(x,y) { x[y!=248 & y!=232] <- NA; return(x)}
> rc <- overlay(rNDVI, rSM, fun=fun1)
>
> Is it required to include datatype and/or format in overlay() to get
> the result (rc) written to disk
> and not fully loaded in RAM?
>
> Agus
>
> 2010/11/19 Robert J. Hijmans <r.hijmans at gmail.com>:
>> Hi Agus,
>>
>> Your example with overlay works for me. I think you need to update
>> 'raster' . In earlier versions many functions only worked for
>> RasterLayer objects, now most functions also support multi-layer
>> (RasterStack or RasterBrick) objects.
>>
>> Robert
>>
>> On Fri, Nov 19, 2010 at 8:50 AM, Agustin Lobo <alobolistas at gmail.com> wrote:
>>> Hi!
>>>
>>> I have to set as NA data in a 3D array (time series of images)
>>> according to values in another 3D array of Quality flags
>>>
>>> #Example
>>> NDVI = round(array(rnorm(5*5*3,180,10),dim=c(5,5,3)))
>>> SM = array(rep(248,5*5*3),dim=c(5,5,3))
>>> SM[4,3:4,1:2] = 232
>>> SM[2,3:4,1:2] = 243
>>> SM[3,4,1:2] = 243
>>>
>>> #The only valid data are the ones for which SM is either 232 or 248, so:
>>> NDVI[SM!=248] <- NA
>>> NDVI[SM!=232] <- NA
>>> #which is the same as
>>> NDVI[SM!=248 & SM!=232] <- NA
>>>
>>> #The problem is that what I really have are images 3697 columns x
>>> 1905 rows and 360 Bands (and even larger) , so I try package raster
>>>
>>> require(rgdal)
>>> require(raster)
>>>
>>> #First, I make fake data:
>>> rNDVI = stack(raster(NDVI[,,1]),raster(NDVI[,,2]),raster(NDVI[,,2]))
>>> rSM = stack(raster(SM[,,1]),raster(SM[,,2]),raster(SM[,,2]))
>>>
>>> fun <- function(x,y) { x[y!=248 & y!=232] <- NA; return(x)}
>>>> rc <- overlay(rNDVI, rSM, fun)
>>> Error in function (classes, fdef, mtable)  :
>>>  unable to find an inherited method for function "overlay", for
>>> signature "RasterStack", "RasterStack"
>>> Calls: overlay -> <Anonymous>
>>>
>>> #It seems that calc() must be used for stacks, but seems to me that calc()
>>> #only accepts one stack, not two. Maybe I should just stack both NDVI and SM?
>>>
>>> Thanks!
>>> Agus
>>>
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at stat.math.ethz.ch
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>



More information about the R-sig-Geo mailing list