0

I want to calculate the sum of a moving window circle in R for each center cell. Is there a way to do this in R with focal or focalWeights on my raster (preferrably with map units)? Focal does not take circle or map units and focalWeight gives only weighted sums.

I am looking for an alternative to FocalStatisticas (in Esri) in R.

I found a thread at http://r-sig-geo.2731867.n2.nabble.com/Raster-package-Focal-sum-in-circles-td7587683.html which is tackeling quite a similar idea, which made me come up with a code similar to this.

fwModel <- focalWeight(modell_soil, 25, type='circle')
fwModel[fwModel>0] <- 1
p_focal<-focal(modell_soil, w=fwModel ,fun=sum , na.rm=TRUE)

This code allowes me to use the focal command with a circluar window and map units. The only problem I have until now, is that it does not allow me yet to only calculate the sum for the center cell instead of the whole circular window. I tried quite some things but did not get closer to the solution... Is there a way to tackle the center cell that could fit in my method above?

NAs are actually not a problem for me, as I do not have any in my raster. It's just part of the thread (in the link, so I included it in my trie-outs).

When I had to use rectangular windows I already learned to tackle the center cell, but I am not sure how to use it with a circular window.

  • 1
    The result of focalWeights is a matrix. Just turn the resulting weights into a value of 1, something like: w[w > 0] <- 1 – Jeffrey Evans Jun 26 '18 at 17:44
  • That's what I did in the middle line (which is now commented out). But that does not solve my problem. The sum is still calculated for the whole matrix. But I somehow need to tackle the center cell. I just do not know how to do this in a circular matrix, that might change because it was calculated in map units and not in cells... – Li12 Jun 27 '18 at 7:35
  • 1
    I don't understand what you are after here. The focal function operates on the matrix, regardless of it being circular or rectangular and assigns the resulting value to the center cell. In a binary matrix values with 1 would be operated on and those with 0, ignored. A circular window is a rectangular matrix where, values occuring outside the defined radius are 0. Quit focusing on the "center cell" as it is just confounding your thinking here. – Jeffrey Evans Jun 27 '18 at 12:54
  • I think I misunderstood this part of the focal function then. I thought the sum (or whatever is calculated in the focal command) is assigned to the whole matrix... With my data it was also not easy to interpret the results and to me it looked like the calculated value is assigned to the whole matrix. Thanks for your explanation! – Li12 Jun 27 '18 at 13:03
  • 1
    What you are describing is a block function and not focal. Assigning the value to the matrix, and thus the raster, would result in a raster of non-overlapping rectangles of uniform values and as such, could not be circular in nature. – Jeffrey Evans Jun 27 '18 at 13:50
2

It seems like I have misunderstood how the focal function assigns values. It seems to assign the calculated value of the matrix always to the center cell of the matrix, like @Jefferey Evans, has explained me.

Thus the code was already the solution for my problem. Thanks for the explanation, Jefferey Evans.

fwModel <- focalWeight(modell_soil, 25, type='circle')
fwModel[fwModel>0] <- 1
p_focal<-focal(modell_soil, w=fwModel ,fun=sum , na.rm=TRUE)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.