[Raster-commits] r435 - in pkg/raster: . R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Apr 28 16:43:50 CEST 2009


Author: rhijmans
Date: 2009-04-28 16:43:49 +0200 (Tue, 28 Apr 2009)
New Revision: 435

Modified:
   pkg/raster/DESCRIPTION
   pkg/raster/R/properties.R
   pkg/raster/R/rasterFromFile.R
   pkg/raster/R/rasterFromFileGDAL.R
   pkg/raster/R/readCells.R
   pkg/raster/R/readRaster.R
   pkg/raster/R/reclass.R
   pkg/raster/R/resetConnection.R
   pkg/raster/R/writeGDAL.R
   pkg/raster/R/writeRaster.R
   pkg/raster/R/writeRasterRow.R
   pkg/raster/R/writeRasterSparse.R
   pkg/raster/man/writeadvanced.Rd
Log:


Modified: pkg/raster/DESCRIPTION
===================================================================
--- pkg/raster/DESCRIPTION	2009-04-28 01:30:08 UTC (rev 434)
+++ pkg/raster/DESCRIPTION	2009-04-28 14:43:49 UTC (rev 435)
@@ -1,8 +1,8 @@
 Package: raster
 Type: Package
 Title: Raster data handling for geographic data analysis and modeling
-Version: 0.8.9-14
-Date: 23-April-2009
+Version: 0.8.9-15
+Date: 28-April-2009
 Depends: methods, sp, rgdal (>= 0.5-33), R (>= 2.8.0)
 Author: Robert J. Hijmans & Jacob van Etten
 Maintainer: Robert J. Hijmans <r.hijmans at gmail.com> 

Modified: pkg/raster/R/properties.R
===================================================================
--- pkg/raster/R/properties.R	2009-04-28 01:30:08 UTC (rev 434)
+++ pkg/raster/R/properties.R	2009-04-28 14:43:49 UTC (rev 435)
@@ -40,16 +40,27 @@
 
 
 .driver <- function(object) {
-	fcon <- class(try( object at file@con, silent = T ))[1]
-	if (fcon == 'file') {
+	fn <- filename(object)
+	if (fn == '') {
+		stop('no file asociated with this object')
+	}
+	fileext <- toupper(ext(fn)) 
+	if ( fileext == ".GRD" | fileext == ".GRI" ) {
 		return('raster')
-	} else if (fcon == "GDALReadOnlyDataset") {
+	} else {
 		return('gdal')
-	} else if (fcon == "try-error") {
-		return('NA')
-	} else {
-		stop('unknown driver')
 	}
+	
+#	fcon <- class(try( object at file@con, silent = T ))[1]
+#	if (fcon == 'file') {
+#		return('raster')
+#	} else if (fcon == "GDALReadOnlyDataset") {
+#		return('gdal')
+#	} else if (fcon == "try-error") {
+#		return('NA')
+#	} else {
+#		stop('unknown driver')
+#	}
 
 }	
 

Modified: pkg/raster/R/rasterFromFile.R
===================================================================
--- pkg/raster/R/rasterFromFile.R	2009-04-28 01:30:08 UTC (rev 434)
+++ pkg/raster/R/rasterFromFile.R	2009-04-28 14:43:49 UTC (rev 435)
@@ -23,6 +23,7 @@
 	projstring <- ""
 	minval <- NA
 	maxval <- NA
+	nodataval <- -Inf
 	
 	for (i in 1:length(ini[,1])) {
 		if (ini[i,2] == "MINX") {xn <- as.numeric(ini[i,3])} 
@@ -47,7 +48,7 @@
 #		else if (ini[i,2] == "NCELLVALS") {ncellvals <- ini[i,3]} 
 		else if (ini[i,2] == "PROJECTION") {projstring <- ini[i,3]} 
     }  
-
+	if (projstring == 'GEOGRAPHIC') { projstring <- "+proj=longlat" }
     raster <- raster(ncols=nc, nrows=nr, xmn=xn, ymn=yn, xmx=xx, ymx=yx, projs=projstring)
 	filename(raster) <- filename
 #	raster at file@driver <- "raster"
@@ -69,7 +70,7 @@
 
 	raster at data@source <- 'disk'
 
-	attr(raster at file, "con") <- file(grifile, "rb")
+#	attr(raster at file, "con") <- file(grifile, "rb")
 
     return(raster)
 }

Modified: pkg/raster/R/rasterFromFileGDAL.R
===================================================================
--- pkg/raster/R/rasterFromFileGDAL.R	2009-04-28 01:30:08 UTC (rev 434)
+++ pkg/raster/R/rasterFromFileGDAL.R	2009-04-28 14:43:49 UTC (rev 435)
@@ -52,7 +52,7 @@
 
 	projection(raster) <- attr(gdalinfo, "projection")
 	
-	attr(raster at file, "con") <- GDAL.open(filename)
+#	attr(raster at file, "con") <- GDAL.open(filename)
 	
 #oblique.x   0  #oblique.y   0 
 	raster at data@source <- 'disk'

Modified: pkg/raster/R/readCells.R
===================================================================
--- pkg/raster/R/readCells.R	2009-04-28 01:30:08 UTC (rev 434)
+++ pkg/raster/R/readCells.R	2009-04-28 14:43:49 UTC (rev 435)
@@ -65,11 +65,12 @@
 	} else { 
 		dtype <- "integer"
 	}
+	raster <- openConnection(raster)
 	for (i in 1:length(cells)) {
 		seek(raster at file@con, (cells[i]-1) * dsize)
 		res[i] <- readBin(raster at file@con, what=dtype, n=1, size=dsize, endian=raster at file@byteorder) 
 	}
-	seek(raster at file@con, 0)
+	raster <- closeConnection(raster)
 	res[res <=  max(-3e+38, .nodatavalue(raster))] <- NA
 	return(cbind(cells,res))
 }

Modified: pkg/raster/R/readRaster.R
===================================================================
--- pkg/raster/R/readRaster.R	2009-04-28 01:30:08 UTC (rev 434)
+++ pkg/raster/R/readRaster.R	2009-04-28 14:43:49 UTC (rev 435)
@@ -50,6 +50,7 @@
 		dsize <- dataSize(raster at file@datanotation)
 		dsign <- dataSigned(raster at file@datanotation)
 		
+		raster <- openConnection(raster)
 		if (rownr > 0) {
 			seek(raster at file@con, ((rownr-1) * ncol(raster) + (startcol-1)) * dsize)
 			result <- readBin(raster at file@con, what=dtype, n=ncolumns, dsize, dsign, endian=raster at file@byteorder) }	
@@ -57,10 +58,12 @@
 			seek(raster at file@con, 0)
 			result <- readBin(raster at file@con, what=dtype, n=ncell(raster), dsize, dsign, endian=raster at file@byteorder) 
 		}
-#		close(con)
+		raster <- closeConnection(raster)
+
 #		result[is.nan(result)] <- NA
 		if (dtype == 'numeric') {
-			result[result <=  (0.999 * .nodatavalue(raster)) ] <- NA 	
+			result[result <=  (0.999999 * .nodatavalue(raster)) ] <- NA 	
+			result[is.nan(result)] <- NA
 		} else {
 			result[result == raster at file@nodatavalue ] <- NA 			
 		}
@@ -80,8 +83,10 @@
 				reg <- c(1, ncolumns)
 			}
 		}
+		
+		raster <- openConnection(raster)
 		result <- getRasterData(raster at file@con, offset=offs, region.dim=reg, band = raster at file@band)
-		
+		raster <- closeConnection(raster)
 	
 		# if  NAvalue() has been used.....
 		if (raster at file@nodatavalue < 0) {
@@ -101,8 +106,7 @@
 		firstcell <- cellFromRowCol(raster, rownr, 1)
 		lastcell <- cellFromRowCol(raster, rownr, raster at ncols)
 		raster at data@indices <- c(firstcell, lastcell)
-	}
-	
+	}	
 	return(raster)
 }
 

Modified: pkg/raster/R/reclass.R
===================================================================
--- pkg/raster/R/reclass.R	2009-04-28 01:30:08 UTC (rev 434)
+++ pkg/raster/R/reclass.R	2009-04-28 14:43:49 UTC (rev 435)
@@ -22,8 +22,15 @@
 		print(rclmat)
 	}
 	
-	outraster <- raster(raster, filename)
-	dataType(outraster) <- datatype
+	if (dataContent(raster) == 'all') { nr <- 1 } else { nr <- 2 }
+	if (!canProcessInMemory(raster, nr) && filename == '') {
+		filename <- tempfile()
+		if (options('verbose')[[1]]) { cat('writing raster to:', filename(outRaster))	}						
+	}
+	
+	outRaster <- raster(raster)
+	filename(outRaster) <- filename
+	dataType(outRaster) <- datatype
 
 	res <- vector(length = ncol(raster))
 	
@@ -37,13 +44,13 @@
 			}
 		}
 		if ( dataContent(raster) == 'all') { 
-			outraster <- setValues(outraster, res) 
+			outRaster <- setValues(outRaster, res) 
 		}
 		if ( dataContent(raster) == 'sparse') { 
-			outraster <- setValues(outraster, res,  dataIndices(raster)) 
+			outRaster <- setValues(outRaster, res,  dataIndices(raster)) 
 		}
-		if (outraster at file@name != "" ) {
-			outraster <- writeRaster(outraster, overwrite=overwrite, filetype=filetype) 
+		if (outRaster at file@name != "" ) {
+			outRaster <- writeRaster(outRaster, overwrite=overwrite, filetype=filetype) 
 		}
 		
 	} else {
@@ -65,11 +72,11 @@
 			if (hasNA) {
 				res[ is.na(values(raster)) ] <- namat[1, 3] 				
 			}	
-			outraster <- setValues(outraster, res, r)
-			outraster <- writeRaster(outraster, overwrite=overwrite, filetype=filetype)
+			outRaster <- setValues(outRaster, res, r)
+			outRaster <- writeRaster(outRaster, overwrite=overwrite, filetype=filetype)
 		}
-		if (r %in% track) { .showTrack(r, outraster at nrows, track, starttime) }
+		if (r %in% track) { .showTrack(r, outRaster at nrows, track, starttime) }
 	}	
-	return(outraster)
+	return(outRaster)
 }
 

Modified: pkg/raster/R/resetConnection.R
===================================================================
--- pkg/raster/R/resetConnection.R	2009-04-28 01:30:08 UTC (rev 434)
+++ pkg/raster/R/resetConnection.R	2009-04-28 14:43:49 UTC (rev 435)
@@ -6,21 +6,20 @@
 
 
 
-resetConnection <- function(raster) {
+openConnection <- function(raster) {
 	if (class(raster) != 'RasterLayer') {
 		stop('resetConnection is only for RasterLayer objects')
 	}
-
-	fn <- trim(filename(object))
+	fn <- trim(filename(raster))
 	if (fn == "") { stop('no file') }
 	fileext <- toupper(ext(fn)) 
 	if ( fileext == ".GRD" ) {
 		ext(fn) <- '.gri'
-		attr(object at file, "con") <- file(fn, "rb")
+		attr(raster at file, "con") <- file(fn, "rb")
 	} else {
-		attr(object at file, "con") <- GDAL.open(fn)
+		attr(raster at file, "con") <- GDAL.open(fn)
 	}
-	return(object)
+	return(raster)
 }
 
 

Modified: pkg/raster/R/writeGDAL.R
===================================================================
--- pkg/raster/R/writeGDAL.R	2009-04-28 01:30:08 UTC (rev 434)
+++ pkg/raster/R/writeGDAL.R	2009-04-28 14:43:49 UTC (rev 435)
@@ -128,7 +128,7 @@
 
 	tempras <- raster(filename(raster) )
 #	raster at file@driver <- 'gdal'
-	attr(raster at file, "con") <- tempras at file@con
+#	attr(raster at file, "con") <- tempras at file@con
 	raster at data@source <- 'disk'
 	return(raster)
 }

Modified: pkg/raster/R/writeRaster.R
===================================================================
--- pkg/raster/R/writeRaster.R	2009-04-28 01:30:08 UTC (rev 434)
+++ pkg/raster/R/writeRaster.R	2009-04-28 14:43:49 UTC (rev 435)
@@ -36,7 +36,7 @@
 	raster <- setMinMax(raster)
 
 	dtype <- .shortDataType(raster at file@datanotation)
-	
+
 	if ( dtype =='INT') {
 		if (xmin(raster) > -32767 & xmax(raster) < 32768) {
 			dataType(raster) <- 'INT2S'
@@ -74,12 +74,14 @@
 		.writeRasterHdr(raster) 
 	}
 	close(raster at file@con)
-	attr(raster at file, "con") <- file(fnamevals, "rb")
+#	attr(raster at file, "con") <- file(fnamevals, "rb")
 	
 	# put logical values back to T/F
-	if ( dtype =='logical') {
+	if ( dtype =='logical' ) {
 		raster at data@values[raster at data@values <=  raster at file@nodatavalue]  <- NA
 		raster at data@values <- as.logical(values(raster))
+	} else if ( dtype =='integer' ) {
+		raster at data@values[raster at data@values <=  raster at file@nodatavalue]  <- NA
 	}
 
 	return(raster)

Modified: pkg/raster/R/writeRasterRow.R
===================================================================
--- pkg/raster/R/writeRasterRow.R	2009-04-28 01:30:08 UTC (rev 434)
+++ pkg/raster/R/writeRasterRow.R	2009-04-28 14:43:49 UTC (rev 435)
@@ -34,7 +34,7 @@
 	.writeRasterHdr(raster) 
 	close(raster at file@con)
 	fnamevals <- .setFileExtensionValues(raster at file@name)
-	attr(raster at file, "con") <- file(fnamevals, "rb")
+#	attr(raster at file, "con") <- file(fnamevals, "rb")
 	raster at data@haveminmax <- TRUE
 	raster at data@source <- 'disk'
 	raster at data@content <- 'nodata'

Modified: pkg/raster/R/writeRasterSparse.R
===================================================================
--- pkg/raster/R/writeRasterSparse.R	2009-04-28 01:30:08 UTC (rev 434)
+++ pkg/raster/R/writeRasterSparse.R	2009-04-28 14:43:49 UTC (rev 435)
@@ -24,8 +24,11 @@
 	raster <- setMinMax(raster)
 
 	binraster <- .setFileExtensionValues(filename(raster))
+
+	raster <- openConnection(raster)
 	writeBin( as.vector(dataIndices(raster)), raster at file@con, size = as.integer(4)) 
 	writeBin( as.vector(values(raster)), raster at file@con, size = dataSize(raster at file@datanotation) ) 
+	raster <- closeConnection(raster)
 
 	# add the 'sparse' key word to the hdr file!!!
 	.writeRasterHdr(raster) 

Modified: pkg/raster/man/writeadvanced.Rd
===================================================================
--- pkg/raster/man/writeadvanced.Rd	2009-04-28 01:30:08 UTC (rev 434)
+++ pkg/raster/man/writeadvanced.Rd	2009-04-28 14:43:49 UTC (rev 435)
@@ -1,7 +1,7 @@
 \name{advanced writing}
 
 \alias{closeConnection}
-\alias{resetConnection}
+\alias{openConnection}
 \alias{canProcessInMemory}
 
 \title{ advanced writing }
@@ -13,18 +13,15 @@
 In the raster package this function is used to determine wether values should be written to disk (in a temporary file) or not when there was
 no filename supplied to the function.
 
-closeConnection removes the handle to the file on disk that was established by raster(x) (where x is a filename). 
-After the handle is closed, you can no longer read data from the file through the RasterLayer object. 
-You probably do not need to do this; unless you get problems with files that cannot be overwritten (rm() 
-does not immediately remove the handle; it seems). 
+openConnection opens a file connection for reading, closeConnection removes it. 
 
-resetConnection will restore the file connection if it has been lost (e.g. after saving a RasterLayer object to disk).
+
 }
 
 \usage{
 canProcessInMemory(raster, n=4)
 closeConnection(raster)
-resetConnection(raster)
+openConnection(raster)
 }
 
 \arguments{



More information about the Raster-commits mailing list