Closed chris-english closed 9 months ago
I have edited your question for legibility, but I do not know what the issue is. Can you please explain? What do you expect to find, and what do you find instead? I am guessing that you do not think that the min value returned by focal is always correct; and that you show that by computing them "manually" using the adjacent cells. Can you point to a particular cell in fdem
that is wrong?
Here is how I would compare focal with an adjacency approach, suggesting that the results are correct.
w <- matrix(c(1,1,NA,1,1,NA,NA,NA,NA), 3, 3)
fdem <- focal(dem, w =w, fun = 'min', na.rm=T)
w[is.na(w)] <- 0
a <- adjacent(dem, 1:ncell(dem), w)
da <- setValues(dem, apply(a, 1, \(i) min(dem[i], na.rm=T)))
all(values(da) == values(fdem), na.rm=T)
# [1] TRUE
I see I wasn't properly attending to NA via na.rm=T (and neglected to explore the difference) . Sorry for the distraction and thanks for the above code.
Well, there's something going on here, as using your code above: all(values(da) == values(fdem), na.rm = T) [1] TRUE but all(values(dem) == values(fdem), na.rm = T) # [1] FALSE which(matrix(c(values(dem), values(fdem)), nrow = 36, ncol = 2)[, 1] != matrix(c(values(dem), values(fdem)), nrow = 36, ncol = 2)[, 2]) [1] 4 12 17 18 20 21 23 25 26 30 31 36 Isn't dem the ground truth of its values?
Given this
dem <- rast(elev)
w <- matrix(c(1,1,NA,1,1,NA,NA,NA,NA), 3, 3)
fdem <- focal(dem, w =w, fun = 'min')
It would be very surprising indeed if this were TRUE
all(values(dem) == values(fdem), na.rm = T)
#[1] FALSE
But we should expect this to be TRUE
all(values(dem) >= values(fdem), na.rm = T)
#[1] TRUE
Noted, as >= is the case. I misconstrued that focal could be used for indexing.
Context: line mapping a descent, as water, by steepest path, over dem.
This is with ubuntu-22.04 R-4.2.1
Not all of these values will be used as
cells(focal( %%6 == 0))
wrap RHS to LHS, but useful starting point. Indexing the focal window in this example