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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jan 28 03:33:38 CET 2009


Author: rhijmans
Date: 2009-01-28 03:33:38 +0100 (Wed, 28 Jan 2009)
New Revision: 196

Added:
   pkg/raster/R/readSurfer.R
Modified:
   pkg/raster/R/raster.create.R
   pkg/raster/R/writeRaster.R
Log:


Modified: pkg/raster/R/raster.create.R
===================================================================
--- pkg/raster/R/raster.create.R	2009-01-27 14:45:56 UTC (rev 195)
+++ pkg/raster/R/raster.create.R	2009-01-28 02:33:38 UTC (rev 196)
@@ -35,7 +35,7 @@
 
 rasterFromFile <- function(filename, values=FALSE, band=1) {
 	fileext <- toupper(fileExtension(filename)) 
-	if (fileext == ".RASTER" | fileext == ".GRD") {
+	if (fileext == ".GRD") {
 		raster <- .rasterFromFile(filename, band) 
 	} else {
 		raster <- .rasterFromGDAL(filename, band) 
@@ -102,6 +102,10 @@
 
 
 .rasterFromFile <- function(filename, band=1) {
+	if (!file.exists( .setFileExtensionValues(filename)) ){
+		warning("no '.gri' file. Assuming this is a Surfer file")
+		return(.readSurfer6(filename))
+	}	
 	ini <- readIniFile(filename)
 	ini[,2] = toupper(ini[,2]) 
 

Added: pkg/raster/R/readSurfer.R
===================================================================
--- pkg/raster/R/readSurfer.R	                        (rev 0)
+++ pkg/raster/R/readSurfer.R	2009-01-28 02:33:38 UTC (rev 196)
@@ -0,0 +1,23 @@
+
+.readSurfer6 <- function(filename) {
+	con <- file(filename, "rb")
+	id <- readBin(con, "characater", n=1, size=4)
+	r <- newRaster()
+	r at ncols <- readBin(con, "int", n=4, size=2)
+	r at rows <- readBin(con, "int", n=4, size=2)
+	r at bbox@xmin <- readBin(con, "double", n=1, size=8)
+	r at bbox@xmax <- readBin(con, "double", n=1, size=8)
+	r at bbox@ymin <- readBin(con, "double", n=1, size=8)
+	r at bbox@ymax <- readBin(con, "double", n=1, size=8)
+	r at data@min <-  readBin(con, "double", n=1, size=8)
+	r at data@max <-  readBin(con, "double", n=1, size=8)
+	ncells <- r at ncols * r at nrows
+	v <- readBin(con, "numeric", n=ncells, size=4)
+	close(con)
+	m <- matrix(v, nrow=r at rows, ncol=r at ncol, byrow=T)
+	m <- m[nrow(m):1, ] 
+	r <- setValues(r, as.vector(t(m)))
+	return(r)
+}
+
+

Modified: pkg/raster/R/writeRaster.R
===================================================================
--- pkg/raster/R/writeRaster.R	2009-01-27 14:45:56 UTC (rev 195)
+++ pkg/raster/R/writeRaster.R	2009-01-28 02:33:38 UTC (rev 196)
@@ -111,21 +111,25 @@
 #	if (dataContent(raster) != 'row') { 
 #		stop('raster does not contain a row') 
 #	}
-	
 	raster at data@values[is.nan(raster at data@values)] <- NA
 	raster at data@values[is.infinite(raster at data@values)] <- NA
-
+	
 	if (raster at file@datatype == "integer" |  raster at file@datatype =='logical' ) { 
 		values <- as.integer(round(raster at data@values))  
 		values[is.na(values)] <- as.integer(raster at file@nodatavalue)		
 	} else { 
 		values  <- as.numeric(raster at data@values) 
 	}
-
 	if (dataIndices(raster)[1] == 1) { 
 		raster <- ..startWriting(raster, overwrite=overwrite)
  	} 
 	
+	rsd <- na.omit(raster at data@values) # min and max values
+	if (length(rsd) > 0) {
+		raster at data@min <- min(raster at data@min, min(rsd))
+		raster at data@max <- max(raster at data@max, max(rsd))
+	}	
+	
 	writeBin(values, raster at filecon, size = raster at file@datasize)
 	
 	if (dataIndices(raster)[2] >= ncell(raster)) {



More information about the Raster-commits mailing list