R/seqSPDF.R

Defines functions seqSPDF

Documented in seqSPDF

#' @title Split Spatial* object into a list.
#'
#' @description
#' \code{seqSPDF} takes a Spatial* object from the (sp) package, splits it
#' into a list containing Spatial*DataFrame objects of a size <= \code{sep}.
#'
#' @details
#' Insert details here.
#'
#' @param SP Spatial* object.
#' @param sep Max length of Spatial*DataFrame in output. Defaults to 1000.
#'
#' @return A list with Spatial*DataFrame objects of a size <= \code{sep}.
#' @export
# @examples need to find test data and add examples
seqSPDF <- function(SP, sep = 1000) {
  if (!grepl("Spatial", class(SP))) {
    stop("Input into SPtoSPDF is not a \"Spatial*\" object")
  } else if (!grepl("DataFrame", class(SP))) {
    SPDF <- SPtoSPDF(SP)
  } else {
    SPDF <- SP
  }

  if (nrow(SPDF) <= sep) {
    return(list(SPDF))
  }

  start <- seq(1, nrow(SPDF), sep)

  end <- seq(sep, nrow(SPDF), sep)
  end <- c(end, nrow(SPDF))

  output <- lapply(1:length(start), function(x) SPDF[start[x]:end[x],])
  return(output)
}
jacpete/jpfxns documentation built on May 16, 2020, 5:02 a.m.