Raster regress filter

0

I have a series of raster images with failed pixels and I want to replace these with a regressive time filter where the current day takes the value of the previous day, that is, the image i-3 incorporates the processing of i-2, i -2 the one from i-1 and so on up. My processing is a subset where if the current day has value 1 and the previous 3 take the value of 3. What I can not do is that the processing is carried out from i to i-n.

#Filtro temporal
library(raster)
library(MODIS)
library(rgdal)
setwd("D:/Estacional")
mypath9<-"D:/SNOWL"
myras9<-list.files(path=mypath9,pattern = glob2rx("*.tif$"), 
                    full.names = TRUE, recursive = TRUE)
name9<-substr(myras9,16,28)

for (i in 363:93){
r<-raster(myras9[i])
r1<-raster(myras9[i-1])
r[(r1==3) & (r==1)]<-3  
writeRaster(r,paste0("MOYDTF2",name9[i], sep=""),datatype='INT1U',format="GTiff",overwrite=TRUE)
}
    
asked by tmsppc 29.07.2017 в 18:37
source

1 answer

0

Interesting question, especially if you are working with a daily product within the MODIS series. You simply have to perform a logical test on the target raster and take the same logical test to obtain the values of the previous day.

The example does not occupy a code similar to yours, it is created to be reproducible, but strictly speaking it is exactly the same, only that r will be the stack of the rasters you have, although the principle also works between different objects.

We created a test raster:

library(raster)
library(rasterVis) # sólo para generar los levelplot() en este caso

r <- raster(nrows=10,ncol=10)

set.seed(1)

We assign value per pixel and in turn we create some pixels NA to reproduce the problem:

rList <- list()

teselasVacias <- sample(1:100,40,replace=F)

for(i in 1:4){
  rList[[i]] <- setValues(r,sample((0:99)+(i*200), 100, replace=F))
  rList[[i]][teselasVacias[(1:10)+((i-1)*10)]] <- NA
}

r <- stack(rList)

levelplot(r)

The logical test is used to determine what value is NA in the raster that you want to fill with values, same test is used for the origin raster of the pixels from which you will obtain the value (in this case, being a stack the previous index i-1 is occupied:

for(i in 2:4){
  r[[i]][is.na(r[[i]])] <- r[[i-1]][is.na(r[[i]])]
}

levelplot(r)

The result shows that the values were replaced with the pixels of the previous index raster.

    
answered by 30.07.2017 / 05:59
source