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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Feb 24 06:42:19 CET 2009


Author: rhijmans
Date: 2009-02-24 06:42:19 +0100 (Tue, 24 Feb 2009)
New Revision: 295

Added:
   pkg/raster/R/copyRaster.R
Modified:
   pkg/raster/R/raster.create.R
   pkg/raster/R/read.inifile.R
   pkg/raster/R/writeRaster.R
Log:


Added: pkg/raster/R/copyRaster.R
===================================================================
--- pkg/raster/R/copyRaster.R	                        (rev 0)
+++ pkg/raster/R/copyRaster.R	2009-02-24 05:42:19 UTC (rev 295)
@@ -0,0 +1,45 @@
+# Author: Robert J. Hijmans, r.hijmans at gmail.com
+# International Rice Research Institute
+# Date :  February 2009
+# Version 0.8
+# Licence GPL v3
+
+
+moveRasterFile <- function(raster, filename, overwrite=FALSE) {
+	r <- copyRaster(raster, filename, overwrite)
+	f <- trim(filename(raster))
+	fileext <- toupper(fileExtension(f)) 
+	if (fileext == ".GRD") {
+		file.remove(f)
+		file.remove(setFileExtension(f, '.gri'))
+	}
+	return(r)
+}
+
+
+copyRasterFile <- function(raster, filename, overwrite=FALSE) {
+	f <- trim(filename(raster))
+	fileext <- toupper(fileExtension(f)) 
+	if (fileext == ".GRD") {
+		fgrd <- setFileExtension(filename, '.grd')
+		fgri <- setFileExtension(filename, '.gri')
+		if ( file.exists(fgrd) & !overwrite ) {
+			stop('file exists & overwrite = FALSE')
+		}
+		if ( file.exists(fgri) & !overwrite ) {
+			stop('file exists & overwrite = FALSE')
+		}
+		res <- file.copy(f, fgrd, overwrite)
+		if (!res) { stop('could not copy grd file') } 
+		f <- setFileExtension(f, '.gri')
+		res <- file.copy(f, fgri, overwrite)
+		if (!res) { stop('could not copy gri file') } 
+		filename(raster) <- fgrd
+		return(raster)
+	} else {
+		stop('only implemented for raster format files')
+	}
+}
+
+
+

Modified: pkg/raster/R/raster.create.R
===================================================================
--- pkg/raster/R/raster.create.R	2009-02-23 01:38:12 UTC (rev 294)
+++ pkg/raster/R/raster.create.R	2009-02-24 05:42:19 UTC (rev 295)
@@ -1,7 +1,7 @@
 # Author: Robert J. Hijmans, r.hijmans at gmail.com
 # International Rice Research Institute
 # Date : June 2008
-# Version 0,1
+# Version 0.8
 # Licence GPL v3
 
 
@@ -57,7 +57,7 @@
 
 rasterFromFile <- function(filename, values=FALSE, band=1) {
 	fileext <- toupper(fileExtension(filename)) 
-	if (fileext == ".GRD") {
+	if ( fileext == ".GRD" | fileext == ".GRI" ) {
 		raster <- .rasterFromFile(filename, band) 
 	} else {
 		raster <- .rasterFromGDAL(filename, band) 

Modified: pkg/raster/R/read.inifile.R
===================================================================
--- pkg/raster/R/read.inifile.R	2009-02-23 01:38:12 UTC (rev 294)
+++ pkg/raster/R/read.inifile.R	2009-02-24 05:42:19 UTC (rev 295)
@@ -25,7 +25,7 @@
 readIniFile <- function(filename) {
     if (!file.exists(filename)) { stop(paste(filename, " does not exist")) }
 	
-	Lines <- readLines(filename)
+	Lines <- readLines(filename,  warn = FALSE)
 # ";" is the start of a comment .
 	strsplitcomment <- function(s) {.strSplitOnFirstToken(s, token=";")}
 	ini <- lapply(Lines, strsplitcomment) 

Modified: pkg/raster/R/writeRaster.R
===================================================================
--- pkg/raster/R/writeRaster.R	2009-02-23 01:38:12 UTC (rev 294)
+++ pkg/raster/R/writeRaster.R	2009-02-24 05:42:19 UTC (rev 295)
@@ -1,7 +1,7 @@
 # Author: Robert J. Hijmans, r.hijmans at gmail.com
 # International Rice Research Institute
 # Date :  June 2008
-# Version 0,1
+# Version 0.8
 # Licence GPL v3
 
 
@@ -17,10 +17,12 @@
  
 
 .writeRasterAll <- function(raster, overwrite=FALSE) {
-	raster <- setFilename(raster, .setFileExtensionHeader(filename(raster)))
+	filename(raster) <- trim(filename(raster))
 	if (filename(raster) == "") {
 		stop('first provide a filename. E.g.: raster <- setFilename(raster, "c:/myfile")')
 	}
+	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")) 
 	}
@@ -81,10 +83,13 @@
 
  
  .startRowWriting <- function(raster, overwrite) {
-	raster <- setFilename(raster, .setFileExtensionHeader(filename(raster)))
+	filename(raster) <- trim(filename(raster))
 	if (filename(raster) == "") {
 		stop('first provide a filename. E.g.: raster <- setFilename(raster, "c:/myfile")')
 	}
+	
+	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")) 
 	}



More information about the Raster-commits mailing list