R/subset_bfast.R

Defines functions subset_bfast

Documented in subset_bfast

#' Subset a \code{bfastSpatial} data frame by location
#'
#' This function subsets the data frame generated by \code{bfastSpatial} by
#' location.
#'
#' This function is useful because the full data frame may be too large for easy
#' data manipulation. This function may also be used in conjunction with
#' \code{summary_stat} for zonal statistics.
#'
#' @param bfast_in A data frame generated by \code{bfastSpatial()}.
#' @param template A Raster object with the same resolution, extent, and
#'   projection as the raster dataset used to generate \code{bfast_in}.
#'   Alternatively, an XML file generated by \code{create_raster_metadata()}.
#' @param boundary A SpatialPolygons object used to define the subset.
#' @param inverse A logical indicating if the spatial subset defined by
#'   \code{boundary} should be inverted to keep records for cells
#'   \strong{outside} of it.
#' @return A data frame subsetted according to \code{boundary}.
#' @examples
#' \dontrun{
#' subset_bfast(bf_df, raster(C:/Desktop/MODIS_NDVI.tif), bnd)
#' }
#' @importFrom dplyr filter
#' @export
subset_bfast <- function(bfast_in, template, boundary, inverse=FALSE) {
  template <- create_template_raster(template)
  selected_cells <- extract(template, boundary, cellnumbers=TRUE, df=TRUE)[,"cell"]
  if (inverse) {
    return(filter(bfast_in, !(no_cell %in% selected_cells)))
  } else {
    return(filter(bfast_in, no_cell %in% selected_cells))
  }
}
jnghiem/bfasttools documentation built on Nov. 4, 2019, 3:02 p.m.