2

I'm working with the package lidR (v 4.0.3) to extract the tree positions using a custom function but it behaves weirdly. If I use the following, I get an error:

f = function(x) { 0.413 * x^0.5532 }
ttops = locate_trees(r, lmf(f, hmin=2, shape = "circular")) # raster based search
    
#> Error in if (any(ws <= 0)) stop("The function 'ws' returned negative or null values.",  : 
#>      missing value where TRUE/FALSE needed

while if I use this, it works:

f = function(x) { x * 0.08 + 1.5 }

If the parameter hmin filters pixels above 2 metres, how is it possible that I get a negative or null value?

Going a bit deeper, I saw that data contains both NAs and NaNs; nevertheless, if I apply the functions to a vector with the raster values, the results are correct.

ss = terra::values(r)
ss = ss[ss >= 2]
ws = 0.413 * ss^0.5532
any(!is.na(ws) & ws<0)
#> [1] FALSE

Am I missing something?

1 Answer 1

2

There is an internal test in lmf() that checks if the user function does not return windows sizes <= 0. This test is performed for every single point and is not aware of the parameter hmin. So in theory you are correct, your function f looks ok. In practice you must ensure that your function never return values <= 0

f = function(x) { 
  x[x < 5] = 5
  0.413 * x^0.5532 
}

plot(f(-2:40))

I also recommend to protect your function against outliers

f = function(x) { 
  x[x < 5] = 5
  x[x > 30] = 30
  0.413 * x^0.5532 
}

plot(f(-2:40))
1
  • Thanks! such suggestion could be integrated into the example in the lmf function help page or in the lidRbook
    – Nico
    Mar 29, 2023 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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