R/sampleMODISPoints.R

#' sampleModisPoints
#'
#' This function gets a sample of MODIS points (to allow for least-cost to MODIS)
#'
#' @import sp
#' @import raster
#' @param sampleFromCategory category to sample from. Use 0 for absent and 1 for present
#' @param pixelsPerPoint number of pixels represented by one sample point
#' @return the spatialPoints object with sampled MODIS points


sampleModisPoints <- function(modis, sampleFromCategory, pixelsPerPoint=400) {

  modis[modis != sampleFromCategory] <- NA
  validCells <- length(which(!is.na(values(modis))))
  nPoints <- floor(validCells / pixelsPerPoint)
  if (nPoints == 0 & validCells > 0) {
    nPoints <- 1
  }
  if (nPoints == 0) {
    return(data.frame())
  } else {
    samplePoints <- sampleRandom(modis, nPoints, sp=TRUE)
    return(samplePoints)
  }
}
jirikadlec2/snow-data-fusion documentation built on May 19, 2019, 10:36 a.m.