R/write_thumbnail.r

Defines functions write_thumbnail

Documented in write_thumbnail

#' Writes thumbnails of cropmonitor images, if an roi element
#' as generated by estimate_roi() is provided this will be used
#' in overplotting ROI details. If not the ROI and horizon is
#' estimated.
#'
#' @param img: image file (3-layer RGB), or image path
#' @param path: path where to write the image file to (includes image name)
#' @param roi: roi element as generated by estimate_roi()
#' includes estimates of the horizon and ROI
#' @keywords gcc calculation, QA/GC, thumbnails
#' @export

write_thumbnail = function(img = NULL,
                           path = NULL,
                           roi = NULL){
  
  # verify data formats if not transform
  # to the correct data format
  if (class(img) == "character"){
    img = raster::brick(img)
  }
  
  # sometimes negative values occur, trap this error
  # which crashes the routine
  img[img < 0] = 0
  
  # if not roi is provided estimate the roi
  # to be overplotted on the thumbnail
  if (is.null(roi)){
    roi = estimate_roi(img = img)
  }
  
  # visualize the regions of interest and horizon
  # in an image thumbnail for review, if a path is
  # provided, write to file otherwise plot the data
  # to the R graphical device
  if(!is.null(path)){
    jpeg(path, 374, 280, bg = "black")
  }
  
  # plot the images
  raster::plotRGB(img)
  lines(1:ncol(img),
        roi$horizon,
        lwd = 2,
        col='red')
  lines(roi$roi,
        lwd = 2,
        lty = 2,
        col = 'yellow')
  
  # close plotting device if writing to file
  if(!is.null(path)){
    dev.off()  
  }
}
khufkens/cropmonitor documentation built on June 21, 2018, 5:51 a.m.