[Raster-commits] r283 - pkg/raster/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Feb 13 12:33:28 CET 2009


Author: rhijmans
Date: 2009-02-13 12:33:27 +0100 (Fri, 13 Feb 2009)
New Revision: 283

Modified:
   pkg/raster/R/setDatatype.R
Log:


Modified: pkg/raster/R/setDatatype.R
===================================================================
--- pkg/raster/R/setDatatype.R	2009-02-13 05:04:51 UTC (rev 282)
+++ pkg/raster/R/setDatatype.R	2009-02-13 11:33:27 UTC (rev 283)
@@ -5,16 +5,41 @@
 # Licence GPL v3
 
 setDatatype <- function(raster, datatype) {
-	datatype <- trim(datatype)
-	if (!(datatype %in% c('LOGICAL', 'INT1S', 'INT2S', 'INT4S', 'INT8S', 'INT1U', 'INT2U', 'INT4U', 'INT8U', 'FLT4S', 'FLT8S'))) {
+# for backward compatibility issues and non fatal mistakes.
+	datatype <- substr( toupper( trim(datatype) ), 1, 5)
+	if (datatype=='LOGIC') {datatype <- 'LOG1S'}
+	if (datatype == 'INTEG') {datatype <- 'INT4S'}
+	if (datatype == 'NUMER') {datatype <- 'FLT4S'}
+	if (datatype == 'FLOAT') {datatype <- 'FLT4S'}
+	if (datatype == 'DOUBL') {datatype <- 'FLT8S'}
+	if (datatype == 'SINGL') {datatype <- 'FLT4S'}		
+	if (datatype == 'REAL') {datatype <- 'FLT4S'}	
+	
+	if (nchar(datatype) < 3) {
+		stop(paste('invalid datatype:', datatype))
+	} else if (nchar(datatype) == 3) {
+		if (datatype == 'LOG') { 
+			datatype <- paste(datatype, '1S', sep='') 		
+		} else {
+			datatype <- paste(datatype, '4S', sep='') 
+		}
+	} else if (nchar(datatype) == 4) {
+		if (datatype == 'INT1') { 
+			datatype <- paste(datatype, 'U', sep='') 
+		} else { 
+			datatype <- paste(datatype, 'S', sep='')
+		}
+	}
+
+# now for real
+	
+	if (!(substr(datatype, 1, 4) %in% c('LOG1', 'INT1', 'INT2', 'INT4', 'INT8', 'INT1', 'INT2', 'INT4', 'INT8', 'FLT4', 'FLT8'))) {
 		stop('not a valid data type')
 	}
 	type <- substr(datatype,1,3)
 	size <- substr(datatype,4,4)
 	signed <- substr(datatype,1,3) != 'U'
 	
-	raster at file@datanotation <- datatype
-	
 	if (type == "FLT") {
 		raster at file@datatype <- 'numeric'
 		raster at file@datasigned <- TRUE
@@ -22,9 +47,11 @@
 			raster at data@values <- as.numeric(values(raster))
 		}
 		if (size == '4') {
+			raster at file@datanotation <- 'FLT4S'
 			raster at file@datasize <- as.integer(4)
 			raster at file@nodatavalue <- -3.4E38
 		} else if (size == '8') {
+			raster at file@datanotation <- 'FLT8S'
 			raster at file@datasize <- as.integer(8)
 			raster at file@nodatavalue <-  -1.7E308
 		} else { 
@@ -41,28 +68,39 @@
 		if (size == '4') {
 			raster at file@datasize <- as.integer(4)
 			if (signed) {
+				raster at file@datanotation <- 'INT4S'
 				raster at file@nodatavalue <- -2147483647
 			} else {
+				raster at file@datanotation <- 'INT4U'
 				raster at file@nodatavalue <- 4294967295
 			}
 		} else if (size == '2') {
 			raster at file@datasize <- as.integer(2)
 			if (signed) {
+				raster at file@datanotation <- 'INT2S'
 				raster at file@nodatavalue <- -32768
 			} else {
+				raster at file@datanotation <- 'INT2U'
 				raster at file@nodatavalue <- 65535
 			}
 		} else if (size == '1') {
 			raster at file@datasize <- as.integer(1)
 			# there is no nodata value for byte
 			raster at file@nodatavalue <- -9999
+			if (signed) {
+				raster at file@datanotation <- 'INT1S'
+			} else {
+				raster at file@datanotation <- 'INT1U'
+			}
 			warning("binary files of a single byte do not have NA values on disk")
 		} else if (size == '8') {
 			raster at file@datasize <- as.integer(8)
 			if (signed) {
 				raster at file@nodatavalue <- -9223372036854775808
+				raster at file@datanotation <- 'INT8S'							
 			} else {
 				raster at file@nodatavalue <- 18446744073709551615
+				raster at file@datanotation <- 'INT8U'			
 			}
 		} else {
 			stop("invalid datasize for this datatype") 
@@ -72,6 +110,7 @@
 		raster at file@datasigned <- TRUE		
 		raster at file@datasize <- as.integer(1)
 		raster at file@nodatavalue <- -127
+		raster at file@datanotation <- 'LOG1S'
 	} else {
 		stop("unknown datatype")
 	} 



More information about the Raster-commits mailing list