R/splitBuffer.R

Defines functions splitBuffer

Documented in splitBuffer

#' @title Fast Buffer for Spatial* objects.
#'
#' @description
#' \code{splitClip} splits a Spatial* object from the (sp) package using
#' \code{seqSPDF}, and runs \code{rgeos::gBuffer()} on the list.
#'
#' @details
#' This is a general wrapper for \code{rgeos::gBuffer()} that allows
#' for quicker buffer and fixed a ram leak issue caused by large datasets
#' when using the native rgeos function.
#'
#' @param input Spatial* object.
#' @param width Distance for buffer.Defaults to 1.
#' @param sep Passed to \code{seqSPDF()}. Default set to 1000.
#' @param dissolve Do you want to dissolve the buffered feature? Defaults to FALSE.
#'
#' @return Returns a buffer of the Spatial* input feature.
#' @section Warning:
#' Will not output with a dataframe due to limitations of the rgeos
#' functions. Will look into using \code{raster::buffer()} in a future
#' release.
#'
#' @export
# @examples need to find test data and add examples
splitBuffer <- function(input, width = 1.0, sep = 1000, dissolve = F) {

  if (!grepl("Spatial", class(input))) {
    stop("\"input\" input not of a \'Spatial\' class")
  } else if (!(class(width) %in% c("numeric","integer"))) {
    stop("\"width\" input not a number")
  }

  inputsplit <- seqSPDF(input, sep)

  buffList <- lapply(inputsplit, function(x) {
    buff <- rgeos::gBuffer(x, width = width, byid = T)
    return(buff)
  })

  if (length(buffList) == 1) {
    buffBind = buffList[[1]]
  } else {
    buffBind <- suppressWarnings(do.call(raster::bind, buffList))
  }

  if (dissolve == T) {

    dissolved <- splitDissolve(buffBind, sep = sep)

    return(dissolved)

  } else {
    return(buffBind)
  }

}
jacpete/jpfxns documentation built on May 16, 2020, 5:02 a.m.