https://github.com/cran/spatstat
Raw File
Tip revision: ed8602848e8d53042162af1cec22320e17b0d885 authored by Adrian Baddeley on 27 October 2005, 22:47:24 UTC
version 1.7-13
Tip revision: ed86028
as.im.R
#
#    as.im.R
#
#    conversion to class "im"
#
#    $Revision: 1.3 $   $Date: 2005/03/02 21:24:34 $
#
#    as.im()
#
as.im <- function(X, W, ...) {

  x <- X
  
  if(verifyclass(x, "im", fatal=FALSE))
    return(x)

  if(verifyclass(x, "owin", fatal=FALSE)) {
    w <- as.mask(x)
    m <- w$m
    v <- m * 1
    v[!m] <- NA
    out <- list(v = v, 
                dim    = w$dim,
                xrange = w$xrange,
                yrange = w$yrange,
                xstep  = w$xstep,
                ystep  = w$ystep,
                xcol   = w$xcol,
                yrow   = w$yrow)
    class(out) <- "im"
    return(out)
  }

  if(is.numeric(x) && length(x) == 1) {
    xvalue <- x
    x <- function(xx, yy, ...) { rep(xvalue, length(xx)) }
  }
  
  if(is.function(x)) {
    f <- x 
    w <- as.owin(W)
    w <- as.mask(w)
    m <- w$m
    funnywindow <- !all(m)
    xx <- raster.x(w)
    yy <- raster.y(w)
    if(!funnywindow) {
      values <- f(xx, yy, ...)
      v <- matrix(values, nrow=nrow(m), ncol=ncol(m))
    } else {
      xx <- xx[m]
      yy <- yy[m]
      values <- f(xx, yy, ...)
      v <- matrix(, nrow=nrow(m), ncol=ncol(m))
      v[m] <- values
      v[!m] <- NA
    }
    return(im(v, w$xcol, w$yrow))
  }

  stop("Can't convert x to a pixel image")
}
back to top