Dear All,

I'm trying to remove land transitions (represented by the numbers '21' and
'31') that occur more than once in given pixel over a ten-yr period. In
particular, I want to keep only the first transition and replace the other
(s) by a given LU. The functions I'm using to do this work fine ('rep.f' and
'rep.p', below), but because the file is too big I cannot use the 'calc'
function; I run into memory problems.

To deal with this problem, I
1) got the values from the 'raster' files (getValues),
2) looped through each row (pixel) over time, removing duplicated land
transitions,
3) re-constructed the raster files (I know...not very wise).

Question: is there a way to deal with the memory issues? Note that saving
the file to disc didn't solve this problem.

Many thanks,

Paulo

#example (VERY SLOW)
library(raster)

#function to replace repeated transitions (31) in land use
rep.f = function(x) {
    if(length(which(x==31))>1)
    {
        a <- which (x==31)
        b <- subset (a, a > min(a))
        x[b] <- 1
        return(x)
    }
    else{return(x)}
}
#testing function 'rep.f'
xx = c(3,3,3,21,2,31,1,31,1,21)
(x2 = rep.f(xx))

#function to replace repeated transitions (21) in land use
rep.p = function(x) {
    if(length(which(x==21))>1)
    {
        a2 <- which(x==21)
        b2 <- subset (a2, a2 > min(a2))
        x[b2] <- 1
        return(x)
    }
    else{return(x)}
}

#testing function 'rep.p'
rep.p(x2)

#creating example data
a <- raster(nrow=2974, ncol=2546)

#list
b <- list()

#creating ten large raster files with 1,2 (land uses) and 21, 31
(transitions from one LU to another)
for (i in 1:10) {
    b[i] <- setValues(a, sample(c(rep(1,10),
                                  rep(c(21,31),3),
                                  rep(2,4)),ncell(a), replace=T))
}

#stacking all 10 raster files
stacked <- stack(b)

#calling the functions...VERY SLOW AND CRASHES (see error below)
s.31.unique    = calc(stacked, fun=rep.f)
s.31.21.unique = calc(s.31.unique, fun=rep.p)

Warning messages:
1: In as.vector(data) :
  Reached total allocation of 8179Mb: see help(memory.size)
#########################################################
#
#alternative way...not very elegant but seems to work
#
#########################################################
dd = getValues(stacked)

#matrix for indexing
r.31 = matrix(NA,nrow=nrow(dd), ncol=ncol(dd))
for(i in 1:nrow(dd)){
    #apply the rep.f function
    #(replace repeated 31s)

    r.31[i,1:10]=rep.f(dd[i,1:10])
}

#########################################################

#matrix for indexing
r.31.21 = matrix(NA,nrow=nrow(dd), ncol=ncol(dd))
for(i in 1:nrow(dd)){
    #apply the rep.p function
    #(replace repeated 21s)

    r.31.21[i,1:10]=rep.p(r.31[i,1:10])
}
#########################################################
#########################################################
# create raster
rr <- subset(stacked,1)
#replace all values with NAs
rr[rr>0] = NA

##reconstructing the raster files
ss <- list()

for (i in 1:10) {
    ss[i] <- setValues(rr, r.31.21[,i])
}

#stacking...
ss2 <- stack(ss)

#plot...
plot(ss2)


#Computer/R info
< sessionInfo()

R version 2.12.1 (2010-12-16)
Platform: x86_64-pc-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
        [1] stats     graphics  grDevices utils     datasets  methods
base

other attached packages:
        [1] rgdal_0.6-33  raster_1.7-18 sp_0.9-74

loaded via a namespace (and not attached):
        [1] grid_2.12.1     lattice_0.19-13 tools_2.12.1

	[[alternative HTML version deleted]]

