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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Aug 27 20:31:19 CEST 2009


Author: rhijmans
Date: 2009-08-27 20:31:19 +0200 (Thu, 27 Aug 2009)
New Revision: 471

Added:
   pkg/raster/R/rasterFromNetCDF.R
Modified:
   pkg/
   pkg/raster/DESCRIPTION
   pkg/raster/R/gdalFormats.R
   pkg/raster/R/projectRaster.R
   pkg/raster/R/raster.R
   pkg/raster/R/rasterFromFileGDAL.R
   pkg/raster/R/readCells.R
   pkg/raster/R/writeFormats.R
   pkg/raster/R/writeGDAL.R
Log:



Property changes on: pkg
___________________________________________________________________
Name: svn:ignore
   - *.bat
*.gz
*

   + *
*.bat
*.gz


Modified: pkg/raster/DESCRIPTION
===================================================================
--- pkg/raster/DESCRIPTION	2009-08-10 22:44:36 UTC (rev 470)
+++ pkg/raster/DESCRIPTION	2009-08-27 18:31:19 UTC (rev 471)
@@ -1,9 +1,10 @@
 Package: raster
 Type: Package
 Title: Raster data handling for geographic data analysis and modeling
-Version: 0.8.9-24
-Date: 10-August-2009
-Depends: methods, sp, rgdal (>= 0.5-33), R (>= 2.8.0)
+Version: 0.9.1
+Date: 25-August-2009
+Depends: methods, sp, R (>= 2.8.0)
+Suggests: rgdal (>= 0.5-33), RNetCDF
 Author: Robert J. Hijmans & Jacob van Etten
 Maintainer: Robert J. Hijmans <r.hijmans at gmail.com> 
 Description: Package for reading, writing, and manipulating (geographic, spatial) raster (grid) data

Modified: pkg/raster/R/gdalFormats.R
===================================================================
--- pkg/raster/R/gdalFormats.R	2009-08-10 22:44:36 UTC (rev 470)
+++ pkg/raster/R/gdalFormats.R	2009-08-27 18:31:19 UTC (rev 471)
@@ -6,6 +6,8 @@
 
 
 .isSupportedGDALFormat <- function(dname) {
+	if (!require(rgdal)) { stop() }
+
 	gd <- gdalDrivers()
 	gd <- as.matrix(subset(gd, gd[,3] == T))
 	res <- dname %in% gd

Modified: pkg/raster/R/projectRaster.R
===================================================================
--- pkg/raster/R/projectRaster.R	2009-08-10 22:44:36 UTC (rev 470)
+++ pkg/raster/R/projectRaster.R	2009-08-27 18:31:19 UTC (rev 471)
@@ -6,6 +6,8 @@
 
 
 projectExtent <- function(object, projs) {
+	if (!require(rgdal)) { stop() }
+
 	validObject(projection(object, asText=FALSE))
 	validObject(projection(projs, asText=FALSE))
 	projfrom <- projection(object)

Modified: pkg/raster/R/raster.R
===================================================================
--- pkg/raster/R/raster.R	2009-08-10 22:44:36 UTC (rev 470)
+++ pkg/raster/R/raster.R	2009-08-27 18:31:19 UTC (rev 471)
@@ -1,6 +1,5 @@
 # Author: Robert J. Hijmans, r.hijmans at gmail.com
-# International Rice Research Institute
-# Date : June 2008
+# Date : August 2008
 # Version 0.8
 # Licence GPL v3
 	
@@ -28,12 +27,36 @@
 
 
 setMethod('raster', signature(x='character'), 
-	function(x, values=FALSE, band=1, proj=NULL) {
+	function(x, values=FALSE, band=1, proj=NULL, ...) {
 		fileext <- toupper(ext(x)) 
-		if ( fileext == ".GRD" | fileext == ".GRI" ) {
-			r <- .rasterFromRasterFile(x, band) 
+		if ( fileext == ".GRD" | fileext == ".GRI" | fileext == "" ) {
+			grifile <- .setFileExtensionValues(x)
+			grdfile <- .setFileExtensionHeader(x)
+			if (file.exists( grdfile) ) {
+				if (file.exists( grifile)) {
+					r <- .rasterFromRasterFile(x, band) 
+				} else {
+					# TODO check if the problem is that the .gri is missing
+					#
+					
+					# check if this is a netcdf file
+					fcon <- file("d:/data/ca.grd", "rb")
+					w <- readBin(fcon, what='character', n=1)
+					close(fcon)
+					if (substr(w, 1, 3) == "CDF") { 
+						r <- .rasterFromNetCDF(x, ...) 
+					} else {
+					# perhaps a surfer grid...
+						r <- .rasterFromGDAL(x, band) 
+					}
+				}
+			} else {
+				r <- .rasterFromGDAL(x, band) 
+			}
+		} else if (file.exists( x )){
+			r <- .rasterFromGDAL(x, band) 
 		} else {
-			r <- .rasterFromGDAL(x, band) 
+			stop(paste('file', x, 'does not exist'))
 		}
 		if (values) {
 			r <- readAll(r)

Modified: pkg/raster/R/rasterFromFileGDAL.R
===================================================================
--- pkg/raster/R/rasterFromFileGDAL.R	2009-08-10 22:44:36 UTC (rev 470)
+++ pkg/raster/R/rasterFromFileGDAL.R	2009-08-27 18:31:19 UTC (rev 471)
@@ -6,6 +6,8 @@
 
 
 .rasterFromGDAL <- function(filename, band) {	
+	if (!require(rgdal)) { stop() }
+
 	gdalinfo <- GDALinfo(filename)
 	nc <- as.integer(gdalinfo[["columns"]])
 	nr <- as.integer(gdalinfo[["rows"]])

Added: pkg/raster/R/rasterFromNetCDF.R
===================================================================
--- pkg/raster/R/rasterFromNetCDF.R	                        (rev 0)
+++ pkg/raster/R/rasterFromNetCDF.R	2009-08-27 18:31:19 UTC (rev 471)
@@ -0,0 +1,64 @@
+# Author: Robert J. Hijmans, r.hijmans at gmail.com
+# Date: Aug 2009
+# Version 0.8
+# Licence GPL v3
+
+
+.rasterFromNetCDF <- function(x, band, xvar='', yvar='', zvar='') {
+# to be improved for large files (i.e. do not read all data from file...)
+	if (!require(RNetCDF)) { stop() }
+	nc <- open.nc(x)
+# should do some checks if x, y and z are present. Or perhaps lat and lon in stead of x	
+
+	nv <- file.inq.nc(nc)$nvars
+    vars <- vector()
+	for (i in 1:nv) { vars <- c(var.inq.nc(nc,i-1)$name, vars) }
+     
+	if (xvar == '') {
+		if ('x' %in% vars) { 
+			xvar <- 'x'
+		} else if ('lon' %in% vars) { 
+			xvar <- 'lon' 
+		} else {
+			stop('cannot find xvar in file')
+		}
+	} else {
+		if (!(xvar %in% vars)) { 
+			stop('cannot find xvar in file')
+		}
+	}
+	if (yvar == '') {
+		if ('y' %in% vars){ 
+			yvar <- 'y'
+		} else if ('lat' %in% vars) { 
+			yvar <- 'lat' 
+		} else {
+			stop('cannot find yvar in file')
+		}
+	} else {
+		if (!(yvar %in% vars)) { 
+			stop('cannot find yvar in file')
+		}
+	}
+	
+	if (zvar == '') { zvar <- 'z' }
+	if (!(zvar %in% vars)) { 
+		stop('cannot find zvar in file')
+	}
+	
+	ncols <- dim.inq.nc(nc, xvar)$length
+	nrows <- dim.inq.nc(nc, yvar)$length
+	xrange <- att.get.nc(nc, xvar, 1)
+	yrange <- att.get.nc(nc, yvar, 1)
+	d <- as.vector(var.get.nc(nc, zvar))
+	
+     close.nc(nc)
+    r <- raster(xmn=xrange[1], xmx=xrange[2], ymn=yrange[1], ymx=yrange[2], ncols=ncols, nrows=nrows)
+# y needs to go from big to small
+	d <- matrix(d, ncol=ncols, nrow=nrows, byrow=TRUE)
+	d <- as.vector( t( d[nrows:1,] ) )
+	
+	r <- setValues(r, d)
+	return(r)
+}
+

Modified: pkg/raster/R/readCells.R
===================================================================
--- pkg/raster/R/readCells.R	2009-08-10 22:44:36 UTC (rev 470)
+++ pkg/raster/R/readCells.R	2009-08-27 18:31:19 UTC (rev 471)
@@ -34,6 +34,8 @@
 
 
 .readCellsGDAL <- function(raster, cells) {
+	if (!require(rgdal)) { stop() }
+
 	colrow <- matrix(ncol=5, nrow=length(cells))
 #	valuename <- raster at file@shortname
 #	if (valuename == "") {valuename <- "value" }

Modified: pkg/raster/R/writeFormats.R
===================================================================
--- pkg/raster/R/writeFormats.R	2009-08-10 22:44:36 UTC (rev 470)
+++ pkg/raster/R/writeFormats.R	2009-08-27 18:31:19 UTC (rev 471)
@@ -6,10 +6,16 @@
 
 
 writeFormats <- function() {
-	gd <- gdalDrivers()
-	gd <- as.matrix(subset(gd, gd[,3] == T))
-	short <- c("raster", "ascii", as.vector(gd[,1]))
-	long <- c("raster package format", "Arc ascii", as.vector(gd[,2]))
+	if (require(rgdal)) { 
+		gd <- gdalDrivers()
+		gd <- as.matrix(subset(gd, gd[,3] == T))
+		short <- c("raster", "ascii", as.vector(gd[,1]))
+		long <- c("raster package format", "Arc ascii", as.vector(gd[,2]))
+		m <- cbind(short, long)
+	} else {
+		short <- c("raster", "ascii", "")
+		long <- c("raster package format", "Arc ascii", "rgdal not installed")
+	}
 	m <- cbind(short, long)
 	colnames(m) <- c("name", "long_name")
 	return(m)

Modified: pkg/raster/R/writeGDAL.R
===================================================================
--- pkg/raster/R/writeGDAL.R	2009-08-10 22:44:36 UTC (rev 470)
+++ pkg/raster/R/writeGDAL.R	2009-08-27 18:31:19 UTC (rev 471)
@@ -54,7 +54,8 @@
 
 
 .writeGDALrow <- function(raster, gdalfiletype, overwrite, mvFlag, options ) {
-	
+	if (!require(rgdal)) { stop() }
+
 	rownr <- rowFromCell(raster, dataIndices(raster)[1])
 #	if (rownr %in%  c(1, 10, 50, 100, 250, 500, 1000, 2500, 5000, 10000, 20000, 30000, 40000, 50000, 100000)) {
 #		print( paste("writing row", rownr, "at:", format(Sys.time(), "%H:%M:%S")))
@@ -113,7 +114,8 @@
 #}
 
 .writeGDALall <- function(raster, gdalfiletype, overwrite, mvFlag, options) {
-	
+	if (!require(rgdal)) { stop() }
+
 	transient <- .getGDALtransient(raster, gdalfiletype, overwrite, mvFlag, options)
     for (band in 1:nlayers(raster)) {
 		x <- putRasterData(transient, t(values(raster, format='matrix')), band, c(0, 0)) 



More information about the Raster-commits mailing list