R/clean.R

#' @title Clean the occurrence records
#' @name clean
#'
#' @description A function to remove the points that are outside the extension of the raster and to maintain, at most one register per pixel.
#'
#' @param coord data.frame with the coordinates
#' @param abio an object with the rasters to be cut. Accepts an object of type RasterStack, generated by the function \code{\link[raster]{stack}}
#'
#' @details Used internally in function \code{\link[modelos]{modelos}}.
#'
#' @return data.frame containing longitude and latitude.
#'
#' @author Diogo S. B. Rocha
#'
#' @seealso \code{\link[raster]{cellFromXY}}, \code{\link[raster]{mask}}, \code{\link[raster]{extract}}
#'
#' @examples
#' fnames <- list.files(path=paste(system.file(package="dismo"), '/ex', sep=''), pattern='grd', full.names=TRUE )
#' predictors <- raster::stack(fnames)
#' clean(coord = manimax, abio = predictors)
#'
#' @import raster
#'
#' @export
clean = function(coord, abio) {
    if (dim(coord)[2] == 2) {
        if (exists("abio")) {
            # selecionar os pontos únicos e sem NA
            mask = abio[[1]]
            # Selecionar pontos espacialmente únicos #
            cell <- cellFromXY(mask, coord)  # get the cell number for each point
            dup <- duplicated(cell)
            pts1 <- coord[!dup, ]  # select the records that are not duplicated
            pts1 <- pts1[!is.na(extract(mask, pts1)), ]  #selecionando apenas pontos que tem valor de raster

            cat(dim(coord)[1] - dim(pts1)[1], "points removed\n")
            cat(dim(pts1)[1], "spatially unique points\n")
            names(pts1) = c("lon", "lat")#
            return(pts1)
        } else (cat("Indicate the object with the predictive variables"))
    } else (stop("Coordinate table has more than two columns.\nThis table should only have longitude and latitude in this order."))
}
diogosbr/modelos documentation built on May 9, 2019, 5:23 p.m.