0
votes

I have a group 34 raster (.tif), of approximate 10MB each, with values ​​1 and 0 of presence or absence of Eucalyptus respectively, product of a supervised classification of the same area and identical. Each raster represents a year of evaluation (i) from 1985 to 2018. I want to find the sum of two types of pixel sequences in groups of three images:

  1. stable pixels: the value of the pixel is 1 in the year (i), (i + 1) e (i-1) and corresponds to a sequence of 111. That is to say that for consecutive years it is Eucalyptus.
  2. unstable pixels: it is 1 in year i and 0 in year i + 1 and year i-1 (sequence 010). That is to say that it is alternately Eucalyptus and in other years it is not, which can be a classification error.

In both cases, the aim is to verify if the classification is robust, beyond the high kappa indexes resulting from the classification. My questions are:

  1. How can I implement this process in R?
  2. I read that maybe the loop with "for" is not the best option to work with large rasters, is there another alternative? Because I have to implement other processes with similar logics.

I tried several alternatives based on: Function to sum each grid cells of raster stack using other rasters as an indicator or here Conditionally calculating the difference between max(raster) and each raster layer of raster stack but I do not understand its logic very well. The same problem I could solve with QGIS with the "Field Calculator" and it works well (it is tedious and feasible of errors) in the following way:

Stable pixel = ("img1985 @ 1" = 1 AND "img1986 @ 1" = 1 AND "img1987 @ 1" = 1) + ... + ("img2016 @ 1" = 1 AND "img2017 @ 1" = 1 AND "img2018 @ 1" = 1)

Unstable pixel = ("img1985 @ 1" = 0 AND "img1986 @ 1" = 1 AND "img1987 @ 1" = 0) + ... + ("img2016 @ 1" = 0 AND "img2017 @ 1" = 1 AND "img2018 @ 1" = 0)

Where to imgYear: is each raster of each year; 1985 ... 2018: year

```
library(raster)
# Initial sample data + result
r1 <- r2 <- r3 <- r4 <- r5 <- rUns <- rSta <- raster(matrix(0, 10, 10))

# Create some "stable pixels" of example.
for (i in seq(from=10, to=50, by=10)) {
r1[(i+6):(i+10)] <- 1
r2[(i+6):(i+10)] <- 1
r3[(i+6):(i+10)] <- 1
r4[(i+6):(i+10)] <- 1
r5[(i+6):(i+10)] <- 1
}
# Create some "unstable pixels" of example.
r2 [c(60,70,80)] <- 1
r3 [1] <- 1
r4 [c(60,70,80)] <- 1
# Stack raster
r <- stack(r1, r2, r3, r4, r5)

# *** Expected results ***
# Sum of stable pixels (Sta)
for (i in 2:nlayers(r)) {
pixelSta <- ((r[[i-1]] == 1) * (r[[i]] == 1) * (r[[i+1]] == 1))* 1
}
# Don't work: Error in .local(x, ...) : not a valid subset
# *** Result Expected (rSta): sequence 111. Manually.
for (i in seq(from=10, to=50, by=10)) {
rSta[(i+6):(i+10)] <- 3
}
as.matrix(rSta)

# Sum of unstable pixels (Uns)
for (i in 2:nlayers(r)) {
pixelUns <- ((r[[i-1]] == 0) * (r[[i]] == 1) * (r[[i+1]] == 0))* 1
}
# Don't work: Error in .local(x, ...) : not a valid subset
# *** Result Expected (rUns): sequence 010. Manually.
rUns [c(60,70,80)] <- 2
rUns [1] <- 1
as.matrix(rUns)
```

The expected results are in the code (*** Result Expected, manually).`

Thank you very much in advance and I hope I have been clear.

1

1 Answers

0
votes

You need a simple function that computes (in)stability. For example, function f

f <- function(x) sum(diff(x)==0)

computes the number of steps that are not transitions from one state to another.

Try it

f(c(0,0,0,0,0,0))
#[1] 5
f(c(0,0,0,0,1,1))
#[1] 4
f(c(0,1,0,1,0,1))
#[1] 0

Now with Raster data

library(raster)
s <- stack(system.file("external/rlogo.grd", package="raster"))
s <- stack(s, s)
x <- calc(s, f)
plot(x)