1# Author: Robert J. Hijmans 2# Date: October 2018 3# Version 1.0 4# License GPL v3 5 6 7setMethod("names", signature(x="SpatRaster"), 8 function(x) { 9 nms <- x@ptr$names 10 Encoding(nms) <- "UTF-8" 11 nms 12 } 13) 14 15 16setMethod("names<-", signature(x="SpatRaster"), 17 function(x, value) { 18 value <- enc2utf8(as.character(value)) 19 if (length(value) != nlyr(x)) { 20 error("names<-", "incorrect number of names") 21 } 22 if (! x@ptr$setNames(value, FALSE)) { 23 error("names<-", "cannot set these names") 24 } 25 26 if (any(names(x) != value)) { 27 # should only be possible with $setNames(value, TRUE) 28 warn("names<-", "some names were changed to make them valid and/or unique") 29 } 30 return(x) 31 } 32) 33 34setMethod("names", signature(x="SpatRasterDataset"), 35 function(x) { 36 nms <- x@ptr$names 37 Encoding(nms) <- "UTF-8" 38 nms 39 } 40) 41 42 43setMethod("names<-", signature(x="SpatRasterDataset"), 44 function(x, value) { 45 x@ptr$names <- enc2utf8(as.character(value)) 46 x 47 } 48) 49 50setMethod("varnames", signature(x="SpatRasterDataset"), 51 function(x) { 52 nms <- x@ptr$names 53 Encoding(nms) <- "UTF-8" 54 nms 55 } 56) 57 58 59setMethod("varnames<-", signature(x="SpatRasterDataset"), 60 function(x, value) { 61 value <- enc2utf8(as.character(value)) 62 x@ptr$names <- value 63 x 64 } 65) 66 67 68setMethod("names", signature(x="SpatVector"), 69 function(x) { 70 nms <- x@ptr$names 71 Encoding(nms) <- "UTF-8" 72 nms 73 } 74) 75 76setMethod("names<-", signature(x="SpatVector"), 77 function(x, value) { 78 if (length(value) != ncol(x)) { 79 error("names<-,SpatVector", "incorrect number of names") 80 } 81 value <- enc2utf8(as.character(value)) 82 x@ptr$names <- value 83 if (any(names(x) != value)) { 84 warn("names<-", "some names were changed to make them valid and/or unique") 85 } 86 return(x) 87 } 88) 89 90 91setMethod("varnames", signature(x="SpatRaster"), 92 function(x) { 93 nms <- x@ptr$get_sourcenames() 94 Encoding(nms) <- "UTF-8" 95 nms 96 } 97) 98 99setMethod("varnames<-", signature(x="SpatRaster"), 100 function(x, value) { 101 value <- enc2utf8(as.character(value)) 102 if (!x@ptr$set_sourcenames(value)) { 103 error("varnames<-,SpatRaster", "cannot set these names") 104 } 105 return(x) 106 } 107) 108 109setMethod("longnames", signature(x="SpatRasterDataset"), 110 function(x) { 111 nms <- x@ptr$long_names 112 Encoding(nms) <- "UTF-8" 113 nms 114 } 115) 116 117 118setMethod("longnames", signature(x="SpatRaster"), 119 function(x) { 120 nms <- x@ptr$get_sourcenames_long() 121 Encoding(nms) <- "UTF-8" 122 nms 123 } 124) 125 126setMethod("longnames<-", signature(x="SpatRasterDataset"), 127 function(x, value) { 128 x@ptr$long_names <- enc2utf8(as.character(value)) 129 return(x) 130 } 131) 132 133 134setMethod("longnames<-", signature(x="SpatRaster"), 135 function(x, value) { 136 value <- enc2utf8(as.character(value)) 137 if (!x@ptr$set_sourcenames_long(value)) { 138 error("longnames<-,SpatRaster", "cannot set these names") 139 } 140 return(x) 141 } 142) 143 144