1# Author: Robert J. Hijmans 2# Date : May 2019 3# Version 1.0 4# License GPL v3 5 6setMethod("ifel", signature(test="SpatRaster"), 7 function(test, yes, no, filename="", ...) { 8 no_num <- FALSE 9 yes_num <- FALSE 10 if (!inherits(no, "SpatRaster")) { 11 # logical includes default NA 12 if (!(is.numeric(no) || is.logical(no))) { 13 error("ifel", "argument 'no' must be a SpatRaster, numeric or logical") 14 } 15 if (length(no) > 1) warn("ifel", 'only the first element of "no" is used') 16 no <- no[1] 17 no_num <- TRUE 18 } 19 if (!inherits(yes, "SpatRaster")) { 20 if (!(is.numeric(yes) || is.logical(yes))) { 21 error("ifel", "argument 'yes' must be a SpatRaster, numeric or logical") 22 } 23 if (length(yes) > 1) warn("ifel", 'only the first element of "yes" is used') 24 yes <- yes[1] 25 yes_num <- TRUE 26 } 27 28 test <- as.logical(test) 29 30 if (no_num & yes_num) { 31 return (classify(test, rbind(c(1, yes), c(0, no)), filename=filename, ...)) 32 } 33 34 if (no_num) { 35 no <- classify(test, rbind(c(0, no), c(1, NA))) 36 } else { 37 no <- mask(no, test, maskvalues=TRUE) 38 } 39 40 if (yes_num) { 41 yes <- classify(test, rbind(c(1, yes), c(0, NA))) 42 } else { 43 yes <- mask(yes, test, maskvalues=FALSE) 44 } 45 46 cover(no, yes, values=NA, filename=filename, ...) 47 } 48) 49 50 51 52