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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Jan 18 14:52:31 CET 2009


Author: rhijmans
Date: 2009-01-18 14:52:30 +0100 (Sun, 18 Jan 2009)
New Revision: 152

Modified:
   pkg/raster/DESCRIPTION
   pkg/raster/NAMESPACE
   pkg/raster/R/bounding.box.R
   pkg/raster/R/exportGDAL.R
   pkg/raster/man/bbox.Rd
   pkg/raster/man/export.Rd
Log:


Modified: pkg/raster/DESCRIPTION
===================================================================
--- pkg/raster/DESCRIPTION	2009-01-18 06:47:03 UTC (rev 151)
+++ pkg/raster/DESCRIPTION	2009-01-18 13:52:30 UTC (rev 152)
@@ -1,7 +1,7 @@
 Package: raster
 Type: Package
 Title: Raster data handling for geographic data analysis and modeling
-Version: 0.8.5
+Version: 0.8.6
 Date: 18-Jan-2009
 Depends: methods, sp, rgdal (>= 0.5-33), R (>= 2.8.0)
 Author: Robert J. Hijmans & Jacob van Etten

Modified: pkg/raster/NAMESPACE
===================================================================
--- pkg/raster/NAMESPACE	2009-01-18 06:47:03 UTC (rev 151)
+++ pkg/raster/NAMESPACE	2009-01-18 13:52:30 UTC (rev 152)
@@ -1,6 +1,6 @@
 importFrom("methods", Arith, Compare, Logic, Math)
 importFrom("graphics", hist, plot)
-importFrom("sp", Spatial, SpatialPixels, SpatialPixelsDataFrame, SpatialGrid, SpatialGridDataFrame, overlay)
+importFrom("sp", bbox, overlay, Spatial, SpatialPixels, SpatialPixelsDataFrame, SpatialGrid, SpatialGridDataFrame)
 exportClasses(BoundingBox, BasicRaster, Raster, RasterLayer, RasterBrick, RasterStack)
-exportMethods(show, summary, plot, hist, ncol, nrow, dim)
+exportMethods(show, summary, plot, hist, ncol, nrow, dim, overlay, bbox)
 exportPattern("^[^\\.]")
\ No newline at end of file

Modified: pkg/raster/R/bounding.box.R
===================================================================
--- pkg/raster/R/bounding.box.R	2009-01-18 06:47:03 UTC (rev 151)
+++ pkg/raster/R/bounding.box.R	2009-01-18 13:52:30 UTC (rev 152)
@@ -19,6 +19,20 @@
 }
 
 
+setMethod('bbox', signature(obj='Raster'), 
+	function(obj) {
+		bb <- matrix(ncol=2, nrow=2)
+		colnames(bb) <- c("min","max")
+		rownames(bb) <- c("s1","s2")
+		bb[1,1] <- xmin(obj)
+		bb[1,2] <- xmax(obj)
+		bb[2,1] <- ymin(obj)
+		bb[2,2] <- ymax(obj)
+		return(bb)
+	}	
+)
+
+
 if (!isGeneric("getBbox")) {
 	setGeneric("getBbox", function(object)
 		standardGeneric("getBbox"))

Modified: pkg/raster/R/exportGDAL.R
===================================================================
--- pkg/raster/R/exportGDAL.R	2009-01-18 06:47:03 UTC (rev 151)
+++ pkg/raster/R/exportGDAL.R	2009-01-18 13:52:30 UTC (rev 152)
@@ -1,23 +1,91 @@
+# R function for the raster package
+# Author: Robert J. Hijmans
+# International Rice Research Institute. Philippines
+# contact: r.hijmans at gmail.com
+# Date : January 2009
+# Version 0.8
+# Licence GPL v3
 
-exportGDAL <- function(raster, driver="HFA", filename, overwrite=FALSE, ForceIntOutput = FALSE) { 
-	if (file.exists(filename) & !overwrite) {
-		stop("filename exists; use overwrite=TRUE")
+
+# based on  create2GDAL and saveDataset from the rgdal package
+# authors: Timothy H. Keitt, Roger Bivand, Edzer Pebesma, Barry Rowlingson
+
+.isSupportedGDALdriver <- function(dname) {
+	gdrivers <- c("ADRG", "BMP", "BT", "EHdr", "ELAS", "ENVI", "ERS", "GSBG", "GTiff", "HFA", "IDA", "ILWIS", "INGR", "Leveller", "MEM", "MFF", "MFF2", "NITF", "PAux", "PCIDSK", "PNM", "RMF", "RST", "SGI", "Terragen", "VRT")
+	res <- dname %in% gdrivers
+	if (!res) { stop(paste(dname,"is not a supported GDAL file format. Choose from: \n    ADRG, BMP, BT, EHdr, ELAS, ENVI, ERS, GSBG, GTiff, HFA, IDA, ILWIS,\n    INGR, Leveller, MEM, MFF, MFF2, NITF, PAux, PCIDSK, PNM, RMF, RST, SGI, Terragen, VRT" ) ) }
+	return(res)
+}
+
+
+exportGDAL <- function(raster, filename, gdalfiletype = "GTiff", overwrite=FALSE, ForceIntOutput = FALSE ) {
+	mvFlag = NA
+	options = NULL
+	.isSupportedGDALdriver(gdalfiletype)
+	
+# this is a RasterLayer hence:
+    nbands = 1
+# but we keep this for later (stack, brick)
+
+	if (file.exists(filename)) {
+		if (!overwrite) {
+			stop("filename exists; use overwrite=TRUE")
+		} else if (!file.remove(filename)) {
+			stop("cannot delete file. permissin denied.")
+		}
 	}	
 
-	# this needs to get fancier; depending on raster and the abilties of the drivers
+#.GDALDataTypes <- c('Unknown', 'Byte', 'UInt16', 'Int16', 'UInt32','Int32', 'Float32', 'Float64', 'CInt16', 'CInt32',   'CFloat32', 'CFloat64')	
+# this needs to get fancier; depending on object and the abilties of the drivers
 	dataformat <- 'Int32'
-	if (dataType(raster) == 'numeric' & !ForceIntOutput) {
-		dataformat <- 'Float32'
-	} 
-	driver <- new("GDALDriver", driver) 
-	export <- new("GDALTransientDataset", driver, nrow(raster), ncol(raster), 1, dataformat) 
+	if (dataType(raster) == 'integer' | !ForceIntOutput) {
+		if (raster at data@haveminmax) {
+			if (minValue(raster) > -32768 & maxValue(raster) <= 32767) {
+				dataformat <- 'Int16'
+			}
+		}
+	} else { dataformat <- 'Float32' }
 
-	for (r in 1:nrow(raster)) {
-		x <- putRasterData(export, values(readRow(raster, r)), 1, c((r-1), 0)) 
-	}	
+	driver = new("GDALDriver", gdalfiletype)
+    if (!is.null(options) && !is.character(options)) { stop("options not character") }
+    transient = new("GDALTransientDataset", driver = driver, rows = nrow(raster), cols = ncol(raster), bands = nbands, type = dataformat, options = options, handle = NULL)
+ 
+	gt <- c(xmin(raster), xres(raster), 0, ymax(raster), 0, -yres(raster))
+    .Call("RGDAL_SetGeoTransform", transient, gt, PACKAGE = "rgdal")
+    p4s <- projection(raster)
+    .Call("RGDAL_SetProject", transient, p4s, PACKAGE = "rgdal")
+	
+	
+    for (band in 1:nbands) {
 
-	saveDataset(export, filename) 
-	GDAL.close(export) 
-	GDAL.close(driver) 
-}	
+		if (dataContent(raster)=='all') {
+#			if (!is.na(mvFlag)) vals[is.na(vals)] = mvFlag
+			x <- putRasterData(transient, t(values(raster, format='matrix')), band, c(0, 0)) 
+		} else {
+			if (dataSource(raster)=='ram') {
+				stop("No data on disk, and not all values in memory. Cannot write the file")
+			}
+			for (r in 1:nrow(raster)) {
+				x <- putRasterData(transient, values(readRow(raster, r)), band, c((r-1), 0)) 
+			}	
+		}	
 
+#        if (!is.na(mvFlag)) {
+#            transient_b <- getRasterBand(dataset = transient, band = band)
+#            .Call("RGDAL_SetNoDataValue", transient_b, as.double(mvFlag), PACKAGE = "rgdal")
+#       }
+    }
+    saveDataset(transient, filename)
+	GDAL.close(transient) 
+	
+#  do NOT do this, it removes the driver for future use!!! ????	
+#	GDAL.close(driverobj) 
+
+	outras <- rasterFromFile(filename)
+	outras at data@min <- raster at data@min
+	outras at data@max <- raster at data@max
+	if (!is.na((outras at data@min))) {	outras at data@haveminmax <- TRUE }
+	
+	return(outras)
+}
+

Modified: pkg/raster/man/bbox.Rd
===================================================================
--- pkg/raster/man/bbox.Rd	2009-01-18 06:47:03 UTC (rev 151)
+++ pkg/raster/man/bbox.Rd	2009-01-18 13:52:30 UTC (rev 152)
@@ -5,6 +5,7 @@
 \alias{getBbox,Spatial-method}
 \alias{getBbox,matrix-method}
 \alias{getBbox,vector-method}
+\alias{bbox,Raster-method}
 
 \alias{newBbox}
 \alias{setBbox}
@@ -13,10 +14,12 @@
   
 \title{Bounding box functions}
 \description{
-	getBbox extracts a bounding box from a Raster* or Spatial* object (or from a BoundingBox object). It will also create a BoundingBox object from a matrix (2 x 2) or vector (length=4)
-	newBbox creates a new bounding box (as in the "Spatial" object from the SP package)
-	setBbox sets the bounding box of a Raster* object
-	changeBbox changes the bounding box of a Raster* object
+getBbox extracts a bounding box from a Raster* or Spatial* object (or from a BoundingBox object). It will also create a BoundingBox object from a matrix (2 x 2) or vector (length=4)
+newBbox creates a new bounding box (as in the "Spatial" object from the SP package)
+setBbox sets the bounding box of a Raster* object
+changeBbox changes the bounding box of a Raster* object
+
+You can also use bbox(object) to get a sp type bbox (a 2 x 2 matrix), but such an object cannot be used as a bounding box in this package.
 }
 
 \usage{

Modified: pkg/raster/man/export.Rd
===================================================================
--- pkg/raster/man/export.Rd	2009-01-18 06:47:03 UTC (rev 151)
+++ pkg/raster/man/export.Rd	2009-01-18 13:52:30 UTC (rev 152)
@@ -1,4 +1,4 @@
-\name{write}
+\name{export}
 
 \alias{export}
 \alias{exportGDAL}
@@ -12,8 +12,8 @@
   Or create header files associated with other formats for use in different software (use ENVI or ErdasRaw for ArcGIS).
 }
 \usage{
-export(raster, filename="", filetype='ascii', keepGRD=TRUE, overwrite=FALSE)
-exportGDAL <- function(raster, driver, filename, overwrite=FALSE)
+export(raster, filename="", filetype="ascii", keepGRD=TRUE, overwrite=FALSE)
+exportGDAL(raster, filename, gdalfiletype="GTiff", overwrite=FALSE, ForceIntOutput = FALSE)
 writeHeader(raster, type) 
 writeWorldfile(raster, extension=".wld") 
 }
@@ -23,7 +23,8 @@
   \item{overwrite}{ Logical. If \code{TRUE} existing files are overwritten }
   \item{filename}{ The file resulting from import or export }
   \item{filetype}{ The type of file to export to ('ascii' or 'bil')}
-  \item{driver}{ GDAL driver that can create raster datasets (see below for list)}
+  \item{gdalfiletype}{ GDAL file type (driver) name. See below for list of supported file types }
+  \item{ForceIntOutput}{save as integer}
   \item{keepGRD}{ Logical. If \code{FALSE} the existing raster file (internal format) will not be kept after exporting; only applied to other formats that can use the same binary format. }
   \item{type}{ Type of header file: 'BIL', 'ENVI', 'ErdasRaw', 'raster' }
   \item{extension}{ extension of header file. Only implemented for worldfile. }



More information about the Raster-commits mailing list