Skip to content

Using Variable Window Sizes within Focal Function #176

@azh3

Description

@azh3

So I am attempting to implement a maximum filter on a CHM (Canopy Height Model) with a variable window size here, 'Tree_Heights' is a data frame containing the fitted values of a linear regression model to predict tree diameter, the window size is supposed to match the lower prediction interval for the radius of a tree at a given height.

RoundOdd <- function(x) {2*floor(x/2)+1}

fitlwr <- function(x){
RoundOdd(Tree_Heights[Tree_Heights$Tree_Height == x, "fit.lwr"]/2)
}

m <- raster::focalWeight(x = CMM, d = fitlwr(), type = "circle")

CMM <- raster::focal(x = CMM, w = m, fun = max)

This returns the following error:

Error in [.data.frame(Tree_Heights, Tree_Heights$Tree_Height == x, "fit.lwr") : argument "x" is missing, with no default
6.[.data.frame(Tree_Heights, Tree_Heights$Tree_Height == x, "fit.lwr")
5.Tree_Heights[Tree_Heights$Tree_Height == x, "fit.lwr"]
4.RoundOdd(Tree_Heights[Tree_Heights$Tree_Height == x, "fit.lwr"]/2)
3.fitlwr()
2..circular.weight(x, d[1])
1.raster::focalWeight(x = CMM, d = fitlwr(), type = "circle")

If I try instead to use the function in the argument for window size, I get this error:

Error in .local(x, ...) : is.matrix(w) is not TRUE
5. stop(simpleError(msg, call = if (p <- sys.parent(1L)) sys.call(p)))
4. stopifnot(is.matrix(w))
3. .local(x, ...)
2. raster::focal(x = CMM, w = fitlwr, fun = max)

  1. raster::focal(x = CMM, w = fitlwr, fun = max)

Is there no way to use a variable window size inherently in this function? Are there any other options for doing this in the Raster package?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions