R/contour2sp.R

Defines functions contour2sp

Documented in contour2sp

#' Contour Lines to SpatialLinesDataFrame
#' 
#' This function takes a SpatialGrid object and returns
#' contour lines as a SpatialLinesDataFrame object.
#' 
#' @param x a \code{SpatialGrid} object.
#' @param layer which layer of x to use. This may be the column name or a column number.
#' @param nlevels number of contour levels.
#' 
#' @return a \code{SpatialLinesDataFrame}
#' @export
contour2sp <- function(x, layer = 1, nlevels = 10){
  
  im <- as.image.SpatialGridDataFrame(x, attr = layer)
  cl <- contourLines(im, nlevels = nlevels)
  id <- seq_len(length(cl))
  mat <- lapply(cl, function(x) cbind(x$x, x$y))
  lin <- lapply(mat, function(x) Line(x))
  lins <- mapply(function(x, y) Lines(x, ID = y), lin, id)
  SL <- SpatialLines(lins)
  dat <- as.data.frame(sapply(cl, function(x) x$level))
  names(dat) <- "level"
  SD <- SpatialLinesDataFrame(SL, dat, match.ID = FALSE)
  
  return (SD)
}
Hackout2/epimap documentation built on May 6, 2019, 9:47 p.m.