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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Mar 18 05:22:33 CET 2009


Author: rhijmans
Date: 2009-03-18 05:22:33 +0100 (Wed, 18 Mar 2009)
New Revision: 368

Modified:
   pkg/raster/R/write.R
   pkg/raster/R/writeRaster.R
   pkg/raster/R/writeRasterRow.R
   pkg/raster/man/writeRaster.Rd
Log:


Modified: pkg/raster/R/write.R
===================================================================
--- pkg/raster/R/write.R	2009-03-17 11:54:55 UTC (rev 367)
+++ pkg/raster/R/write.R	2009-03-18 04:22:33 UTC (rev 368)
@@ -19,11 +19,10 @@
  
  
 writeRaster <- function(raster, filetype='raster', overwrite=FALSE) {
-	
 	raster_name <- deparse(substitute(raster))
-	
+
 	if (dataContent(raster) != 'row' & dataContent(raster) != 'all' & dataContent(raster) != 'sparse' ) {
-		stop('No data available for writing. First use setValues()')
+		stop('No usable data available for writing. First use setValues()')
 	}
 	
 	if (filetype=='raster') {
@@ -42,14 +41,12 @@
 			raster <- .writeGDALall(raster, gdalfiletype=filetype, overwrite=overwrite, mvFlag=NA, options=NULL)
 		}  
 	}
-	
 	assign(raster_name, raster, envir=parent.frame())
 #	return(invisible())	
-	
 	return(raster)
-	
 }	
 
+
 writeStack <- function(rstack, overwrite=FALSE) {
 	stop("not available yet")
 	for (i in 1:nlayers(rstack)) {

Modified: pkg/raster/R/writeRaster.R
===================================================================
--- pkg/raster/R/writeRaster.R	2009-03-17 11:54:55 UTC (rev 367)
+++ pkg/raster/R/writeRaster.R	2009-03-18 04:22:33 UTC (rev 368)
@@ -18,16 +18,18 @@
 
 .writeRasterAll <- function(raster, overwrite=FALSE) {
 
-	filename <- trim(raster at file@name)
-	if (filename == "") {
+	fname <- trim(raster at file@name)
+	if (fname == "") {
 		stop('first provide a filename. E.g.: raster <- setFilename(raster, "c:/myfile")')
 	}
-	filename <- .setFileExtensionHeader(filename)
-	raster <- setFilename(raster, filename)
+	fname <- .setFileExtensionHeader(fname)
+	filename(raster) <- fname
+	fnamevals <- .setFileExtensionValues(fname)
 
-	if (!overwrite & file.exists(filename)) {
-		stop(paste(filename, "exists. Use 'overwrite=TRUE' if you want to overwrite it")) 
+	if (!overwrite & (file.exists(fname) | file.exists(fnamevals))) {
+		stop(paste(fname,"exists.","use 'overwrite=TRUE' if you want to overwrite it")) 
 	}
+	
 #	raster at file@driver <- 'raster'
 	raster at data@values[is.nan(raster at data@values)] <- NA
 	raster at data@values[is.infinite(raster at data@values)] <- NA
@@ -63,16 +65,16 @@
 		}	
 	}
 
-	attr(raster at file, "con") <- file(filename, "wb")
-	
+	attr(raster at file, "con") <- file(fnamevals, "wb")
 	if (raster at data@content == 'sparse') { 
 		raster <- .writeSparse(raster, overwrite=overwrite) 
 	} else {
-		binraster <- .setFileExtensionValues(filename(raster))
 		dsize <- dataSize(raster at file@datanotation)
 		writeBin( values(raster), raster at file@con, size = dsize ) 
 		.writeRasterHdr(raster) 
-	}	
+	}
+	close(raster at file@con)
+	attr(raster at file, "con") <- file(fnamevals, "rb")
 	
 	# put logical values back to T/F
 	if ( dtype =='logical') {
@@ -80,9 +82,6 @@
 		raster at data@values <- as.logical(values(raster))
 	}
 
-	close(raster at file@con)
-	attr(raster at file, "con") <- file(filename, "rb")
-	
 	return(raster)
 }
  

Modified: pkg/raster/R/writeRasterRow.R
===================================================================
--- pkg/raster/R/writeRasterRow.R	2009-03-17 11:54:55 UTC (rev 367)
+++ pkg/raster/R/writeRasterRow.R	2009-03-18 04:22:33 UTC (rev 368)
@@ -6,20 +6,19 @@
 
  
  .startRowWriting <- function(raster, overwrite) {
-	filename(raster) <- trim(filename(raster))
-	if (filename(raster) == "") {
+ 	fname <- trim(raster at file@name)
+	if (fname == "") {
 		stop('first provide a filename. E.g.: raster <- setFilename(raster, "c:/myfile")')
 	}
+	fname <- .setFileExtensionHeader(fname)
+	filename(raster) <- fname
+	fnamevals <- .setFileExtensionValues(fname)
 	
-	raster <- setFilename(raster, .setFileExtensionHeader(filename(raster)))
-	
-	if (!overwrite & file.exists(filename(raster))) {
-		stop(paste(filename(raster),"exists.","use 'overwrite=TRUE' if you want to overwrite it")) 
+	if (!overwrite & (file.exists(fname) | file.exists(fnamevals))) {
+		stop(paste(fname,"exists.","use 'overwrite=TRUE' if you want to overwrite it")) 
 	}
-	raster at file@name <- .setFileExtensionHeader(filename(raster))
-	binraster <- .setFileExtensionValues(filename(raster))
 	
-	attr(raster at file, "con") <- file(binraster, "wb")
+	attr(raster at file, "con") <- file(fnamevals, "wb")
 	attr(raster at file, "dsize") <- dataSize(raster at file@datanotation)
 	attr(raster at file, "dtype") <- .shortDataType(raster at file@datanotation)
 	
@@ -30,10 +29,12 @@
 	return(raster)
 }
 
+
 .stopRowWriting <- function(raster) {
 	.writeRasterHdr(raster) 
 	close(raster at file@con)
-	attr(raster at file, "con") <- file(filename(raster), "rb")
+	fnamevals <- .setFileExtensionValues(raster at file@name)
+	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/man/writeRaster.Rd
===================================================================
--- pkg/raster/man/writeRaster.Rd	2009-03-17 11:54:55 UTC (rev 367)
+++ pkg/raster/man/writeRaster.Rd	2009-03-18 04:22:33 UTC (rev 368)
@@ -52,52 +52,52 @@
 \seealso{ writeFormats \code{\link[raster]{writeFormats}} }  
 
 \examples{ 
-rs <- raster(system.file("external/test.ag", package="sp"))
+rst <- raster(system.file("external/test.ag", package="sp"))
  
 # read all data
-rs <- readAll(rs)
+rst <- readAll(rst)
 
 # write all to a new binary file
-filename(rs) <- "binall.grd"
-rs <- writeRaster(rs, overwrite=TRUE)
+filename(rst) <- "binall.grd"
+rst <- writeRaster(rst, overwrite=TRUE)
 
 # write all to a new ERDAS .img file
-filename(rs) <- "binall.img"
-rs <- writeRaster(rs, filetype="HFA", overwrite=TRUE)
+filename(rst) <- "binall.img"
+rst <- writeRaster(rst, filetype="HFA", overwrite=TRUE)
 
  
 # write all to an integer binary file
-filename(rs) <- "binallint.grd"
-dataType(rs) <- "INT4S"
-rs <- writeRaster(rs, overwrite=TRUE)
+filename(rst) <- "binallint.grd"
+dataType(rst) <- "INT4S"
+rst <- writeRaster(rst, overwrite=TRUE)
  
 # write all to ascii file
-filename(rs) <- "ascall.asc"
-rs <- writeRaster(rs, filetype='ascii', overwrite=TRUE)
+filename(rst) <- "ascall.asc"
+rst <- writeRaster(rst, filetype='ascii', overwrite=TRUE)
  
 # read and write row by row; write to ascii file
-rs <- raster(system.file("external/test.ag", package="sp"))
-ascras <- raster(rs, filename="ascrow.asc")
-for (r in 1:nrow(rs)) {
-	rs <- readRow(rs, r)
-	v <- values(rs) * 10
+rst <- raster(system.file("external/test.ag", package="sp"))
+ascras <- raster(rst, filename="ascrow.asc")
+for (r in 1:nrow(rst)) {
+	rst <- readRow(rst, r)
+	v <- values(rst) * 10
 	ascras <- setValues(ascras, v, r)
 	ascras <- writeRaster(ascras, filetype='ascii', overwrite=TRUE) 
 }
 	
 # read and write row by row; write to binary file
-binras <- raster(rs, "binbyrow")
-for (r in 1:nrow(rs)) {
-	rs <- readRow(rs, r)
-	binras <- setValues(binras, values(rs), r)
+binras <- raster(rst, filename="binbyrow")
+for (r in 1:nrow(rst)) {
+	rst <- readRow(rst, r)
+	binras <- setValues(binras, values(rst), r)
 	binras <- writeRaster(binras, overwrite=TRUE) 
 }
 
 # read and write row by row; write to GeoTiff binary file
-binras <- raster(rs, "binbyrow2")
-for (r in 1:nrow(rs)) {
-	rs <- readRow(rs, r)
-	binras <- setValues(binras, values(rs), r)
+binras <- raster(rst, filename="binbyrow2.tif")
+for (r in 1:nrow(rst)) {
+	rst <- readRow(rst, r)
+	binras <- setValues(binras, values(rst), r)
 	binras <- writeRaster(binras, filetype="GTiff", overwrite=TRUE) 
 }
 }



More information about the Raster-commits mailing list