1# Author: Robert J. Hijmans 2# Date : June 2017 3# Version 0.9 4# License GPL v3 5 6 7setMethod("dim", signature(x="SpatRaster"), 8 function(x){ return(c(nrow(x), ncol(x), nlyr(x))) } 9) 10 11setMethod("dim", signature(x="SpatRasterDataset"), 12 function(x) { 13 dim(x[1])[1:2] 14 } 15) 16 17setMethod("nrow", signature(x="SpatRaster"), 18 function(x){ return(x@ptr$nrow())} 19) 20 21setMethod("nrow", signature(x="SpatRasterDataset"), 22 function(x){ return(x[1]@ptr$nrow())} 23) 24 25setMethod("nrow", signature(x="SpatVector"), 26 function(x){ return(x@ptr$nrow())} 27) 28 29setMethod("ncol", signature(x="SpatRaster"), 30 function(x){ return(x@ptr$ncol()) } 31) 32 33setMethod("ncol", signature(x="SpatRasterDataset"), 34 function(x){ return(x[1]@ptr$ncol())} 35) 36 37setMethod("ncol", signature(x="SpatVector"), 38 function(x){ return(x@ptr$ncol())} 39) 40 41 42setMethod("dim<-", signature(x="SpatRaster"), 43 function(x, value) { 44 45 if (length(value) == 1) { 46 value <- c(value, ncol(x), nlyr(x)) 47 } else if (length(value) == 2) { 48 value <- c(value, nlyr(x)) 49 } else if (length(value) > 3) { 50 warn("dim<-", "value should have length 1, 2, or 3. Additional values ignored") 51 value <- value[1:3] 52 } 53 value <- as.integer(pmax(round(value), c(1,1,1))) 54 rast(nrows=value[1], ncols=value[2], nlyrs=value[3], extent=ext(x), crs=crs(x)) 55 } 56) 57 58 59 60setMethod("ncell", signature(x="SpatRaster"), 61 function(x) { 62 return(as.numeric(ncol(x)) * nrow(x)) 63 } 64) 65 66setMethod("ncell", signature(x="SpatRasterDataset"), 67 function(x) { 68 ncell(x[1]) 69 } 70) 71 72 73setMethod("ncell", signature(x="ANY"), 74 function(x) { 75 NROW(x) * NCOL(x) 76 } 77) 78 79 80setMethod("size", signature(x="SpatRaster"), 81 function(x) { 82 x@ptr$size() 83 } 84) 85 86 87setMethod("nlyr", signature(x="SpatRaster"), 88 function(x){ 89 return(x@ptr$nlyr() ) 90 } 91) 92 93setMethod("nlyr", signature(x="SpatRasterDataset"), 94 function(x){ 95 sapply(1:length(x), function(i) nlyr(x[i])) 96 } 97) 98 99 100setMethod("nsrc", signature(x="SpatRaster"), 101 function(x){ 102 return(x@ptr$nsrc() ) 103 } 104) 105 106.nlyrBySource <- function(x) { 107 x@ptr$nlyrBySource(); 108} 109 110 111setMethod("ncol<-", signature("SpatRaster", "numeric"), 112 function(x, value) { 113 dim(x) <- c(nrow(x), value) 114 return(x) 115 } 116) 117 118setMethod("nrow<-", signature("SpatRaster", "numeric"), 119 function(x, value) { 120 dim(x) <- c(value, ncol(x)) 121 return(x) 122 } 123) 124 125setMethod("nlyr<-", signature("SpatRaster", "numeric"), 126 function(x, value) { 127 dim(x) <- c(nrow(x), ncol(x), value) 128 return(x) 129 } 130) 131 132 133setMethod("res", signature(x="SpatRaster"), 134function(x) { 135 x@ptr$res 136 } 137) 138 139setMethod("res", signature(x="SpatRasterDataset"), 140function(x) { 141 x[1]@ptr$res 142 } 143) 144 145setMethod("res<-", signature(x="SpatRaster"), 146 function(x, value) { 147 if (length(value) == 1) { 148 value <- c(value, value) 149 } else if (length(value) > 2) { 150 warn("res<-", "value should have length 1 or 2. Additional values ignored") 151 } 152 x@ptr <- x@ptr$set_resolution(value[1], value[2]) 153 messages(x, "resolution") 154 } 155) 156 157 158setMethod("xres", signature(x="SpatRaster"), 159function(x) { 160 res(x)[1] 161 } 162) 163 164setMethod("yres", signature(x="SpatRaster"), 165function(x) { 166 res(x)[2] 167 } 168) 169 170 171