Skip to content
Prev 10445 / 29559 Next

Changing pixel values in a stack file based on regular patterns

Dear Paulo,

I think your example works for me:

#function (very basic) -- sure can be improved...
f = function(x)
{
   if(any(x%in%1) & any(x%in%3))#if  land uses 1 and 3 are present in
that pixel over time...
   {
       r.0 =  grepl("3",x)#find the postion of LU3
       r.M1 = grepl("1",x[which(r.0)-1])#pixel yr-1 of LU3 with LU1
       r.P1 = grepl("1",x[which(r.0)+1])#pixel yr+1 of LU3 with LU1
       if(any(any(r.0)&any(r.M1&r.P1)))#if there is a LU3 somewhere
and LU1 before and after that
       {
           x[r.0]=1 #replace the LU3 by LU1
       }
   }
   return(x)
}

library(raster)
r <- r2 <- r3 <- r4 <- raster(ncol=5, nrow=5)
set.seed(23)
r[]  <- as.numeric(sample(rep(1:3,1000),25))
r2[] <- as.numeric(sample(rep(1:3,1000),25))
r3[] <- as.numeric(sample(rep(1:3,1000),25))
r4[] <- as.numeric(sample(rep(1:3,1000),25))

rr = stack(r,r2,r3,r4)
ee = calc(rr, fun=f)

rv <- getValues(rr)
ev <- getValues(ee)

# which rows have different values?
dif <- rv != ev
rows <- which(apply(dif, 1, sum) > 0)
layer1 layer2 layer3 layer4
[1,]      1      3      1      2
[2,]      1      3      1      3
[3,]      3      1      1      3
[4,]      1      3      1      1
[1,] 1 1 1 2
[2,] 1 1 1 1
[3,] 1 1 1 1
[4,] 1 1 1 1

If this does not work for you, perhaps you need to update the raster package.

Here is an alternative formulation of your function that might be useful

f = function(x) {
	# changes 131  to 111, but does not touch 311 or 113
	a <- which(x == 3)
	a <- subset(a, a > 1 & a < length(x))
	b <- which(x[a-1] ==1 & x[a+1] == 1)
	x[a[b]] <- 1
	return(x)
}


Robert
On Sat, Dec 18, 2010 at 9:39 AM, Paulo Brando <paulobrando at gmail.com> wrote: