R/removeholes.R

#' @title Remove Holes from SpatialPolygons* objects.
#'
#' @description
#' \code{splitDissolve} removes all holes in polygons.
#'
#' @details
#'
#' @param input SpatialPolygons* object.
#'
#' @return Returns a SpatialPolygons* object without holes.
#'
#' @export
# @examples need to find test data and add examples
remove.holes <- function (input) {
  if (!grepl("SpatialPolygon", class(input))) {
    stop("\"input\" input not of a \'SpatialPolygon*\' class")
  }

  is.hole <- unlist(lapply(seq(1, length(input@polygons[[1]]@Polygons)), function(P) input@polygons[[1]]@Polygons[[P]]@hole))
  polys <- input@polygons[[1]]@Polygons[!is.hole]
  Poly1 <- Polygons(polys,ID=input@polygons[[1]]@ID)
  Poly2 <- SpatialPolygons(list(Poly1), proj4string = input@proj4string)

  if (grepl("DataFrame", class(input))) {
    Poly3 <- SpatialPolygonsDataFrame(Poly2, data = input@data)
    return(Poly3)
  } else {
    return(Poly2)
  }
}
jacpete/jpfxns documentation built on May 16, 2020, 5:02 a.m.