Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to terra / add directions argument #18

Open
mcgregorian1 opened this issue Aug 24, 2023 · 1 comment
Open

Update to terra / add directions argument #18

mcgregorian1 opened this issue Aug 24, 2023 · 1 comment

Comments

@mcgregorian1
Copy link

Per #15 and the conversion by @niekkoelewijn, could the ForestGapsR function be updated in the package to be terra-compatible?

Also, can the directions argument in terra::patches() be included as an argument to the main function? Overall this would look like the following:

getForestGaps <- function(chm_layer, threshold = 10, size = c(1, 10^4), directions = 8){
chm_layer[chm_layer > threshold] <- NA
chm_layer[chm_layer <= threshold] <- 1
gaps <- terra::patches(chm_layer, directions, allowGaps = F)
rcl <- terra::freq(gaps)
rcl[, 3] <- rcl[, 3] * terra::res(chm_layer)[1]^2
z <- terra::classify(gaps, rcl = rcl, right = NA)
z[is.na(gaps)] <- NA
gaps[z > size[2]] <- NA
gaps[z < size[1]] <- NA
gaps <- terra::patches(gaps, directions, allowGaps = F)
names(gaps) <- "gaps"
return(gaps)
}
@mcgregorian1
Copy link
Author

mcgregorian1 commented Aug 24, 2023

Similarly, GapSPDF can be changed to the following. Note that this returns only a data frame, but to return the polygons themselves then the function should return gaps_poly directly.

gapsToTable <- function(gap_layer){
    gaps_poly <- terra::as.polygons(x=gap_layer, dissolve=TRUE, na.rm=TRUE,
                                    values=TRUE)
    names(gaps_poly) <- "gap_id"
    gaps_df <- terra::as.data.frame(terra::centroids(gaps_poly), geom="XY")
    return(gaps_df)
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant