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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Mar 7 16:11:55 CET 2009


Author: rhijmans
Date: 2009-03-07 16:11:55 +0100 (Sat, 07 Mar 2009)
New Revision: 323

Modified:
   pkg/raster/R/canProcessInMemory.R
   pkg/raster/R/project.R
   pkg/raster/R/replacement.R
   pkg/raster/R/resample.R
   pkg/raster/man/copyRasterFile.Rd
   pkg/raster/man/extent.Rd
   pkg/raster/man/project.Rd
   pkg/raster/man/set.Rd
   pkg/raster/man/setDatatype.Rd
   pkg/raster/man/setValues.Rd
   pkg/raster/man/writeRaster.Rd
   pkg/raster/man/writeadvanced.Rd
Log:


Modified: pkg/raster/R/canProcessInMemory.R
===================================================================
--- pkg/raster/R/canProcessInMemory.R	2009-03-07 12:47:17 UTC (rev 322)
+++ pkg/raster/R/canProcessInMemory.R	2009-03-07 15:11:55 UTC (rev 323)
@@ -6,20 +6,31 @@
 
 
 .CanProcessInMemory <- function(raster, n=4) {
+	gc()
+
 	if (ncell(raster) > 2147483647) {
 		return(FALSE) 
 	}
 	cells <- round(1.05 * ncell(raster))
-	gc()
-	w <- options('warn')[[1]]
-	options('warn'=-1) 
-	r <- try( matrix(NA, ncol=n, nrow=cells), silent=TRUE )
-	options('warn'= w) 
-	if (class(r) == "try-error") {
-#	if (memneed > memory.size(max = T)) {
-		return( FALSE )
+	
+	if (substr( R.Version()$platform, 1, 7) == "i386-pc" ) {
+	# windows, function memory.size  available
+		memneed <- cells * 8 * n / (1024 * 1024)
+		if (memneed > memory.size(max = T)) {
+			return(FALSE)
+		} else {
+			return(TRUE)
+		}
 	} else {
-		return( TRUE ) 
+		w <- options('warn')[[1]]
+		options('warn'=-1) 
+		r <- try( matrix(NA, ncol=n, nrow=cells), silent=TRUE )
+		options('warn'= w) 
+		if (class(r) == "try-error") {
+			return( FALSE )
+		} else {
+			return( TRUE ) 
+		}
 	}
 }
 

Modified: pkg/raster/R/project.R
===================================================================
--- pkg/raster/R/project.R	2009-03-07 12:47:17 UTC (rev 322)
+++ pkg/raster/R/project.R	2009-03-07 15:11:55 UTC (rev 323)
@@ -4,6 +4,7 @@
 # Version 0.8
 # Licence GPL v3
 
+	
 
 
 projectBbox <- function(object, projstring) {
@@ -11,12 +12,15 @@
 	projstring <- projection(projstring)
 	xy <- rbind(c(b at xmin, b at ymax), c(b at xmax, b at ymax), c(b at xmin, b at ymin), c(b at xmax, b at ymin))
 
-	if (isLatLon(projstring)) {
+	if (isLatLon(object)) {
 		p <- project(xy, projstring, inv=FALSE)
 	} else {
-		b <- project(xy, projection(object), inv=TRUE)
-		p <- project(xy, projstring, inv=FALSE)
-	}
+		p <- project(xy, projection(object), inv=TRUE)
+		if (!isLatLon(projstring)) {
+			p <- project(p, projstring, inv=FALSE)
+		}
+	} 
+
 	xmin <- min(p[,1])
 	xmax <- max(p[,1])
 	ymin <- min(p[,2])
@@ -28,18 +32,35 @@
 }
 
 
-projectRaster <- function(from, to, method="nngb", overwrite=FALSE) {
+projectRaster <- function(from, to, method="ngb", filename=NULL, filetype='raster', datatype='FLT4S', overwrite=FALSE, track=-1)  {
+	validObject(to)
+	validObject(projection(from, asText=F))
+	validObject(projection(to, asText=F))
+	if  (projection(from) == "NA") {stop("input projection is NA")}
+	if  (projection(to) == "NA") {stop("output projection is NA")}
+	
+	pbb <- projectBbox(to, projection(from))
+	bb <- intersectBbox(pbb, from)
+	validObject(bb)
 
-# do the bounding boxes overlap at all? 
 
-# get .innerbox first?
+	if (!method %in% c('bilinear', 'ngb')) { stop('invalid method') 	}
+	if (is.null(filename)){filename <- ""}
+	to <- setRaster(to, filename)
+	to <- setDatatype(to, datatype)
 
-# are the projs not NA and valid and not the same?
-
-	validObject(to)
 	rowCells <- 1:ncol(to)
 	inMemory <- filename(to) == ""
 	v <- vector(length=0)
+
+	if (!.CanProcessInMemory(to, 1) && filename(to) == '') {
+		filename <- tempfile()
+		to <- setFilename(to, filename )
+		if (options('verbose')[[1]]) { cat('writing raster to:', filename(to))	}
+	}
+	starttime <- proc.time()
+
+
 	for (r in 1:nrow(to)) {
 		cells <- rowCells + (r-1) * ncol(to)
 		xy <- xyFromCell(to, cells)
@@ -51,6 +72,13 @@
 				unProjXY <- project(unProjXY, projection(to), inv=TRUE )
 			}
 		}
+		
+		if (method=='ngb') {
+			vals <- xyValues(from, xy)
+		} else {
+			vals <- bilinearValue(from, xy)
+		}
+		
 		vals <- xyValues(from, unProjXY)
 		if (inMemory) {
 			v <- c(v, vals)
@@ -58,6 +86,13 @@
 			to <- setValues(to, vals, r)
 			to <- writeRaster(to, overwrite=overwrite)
 		}
+		
+		if (r %in% track) {
+			elapsed <- (proc.time() - starttime)[3]
+			tpr <- elapsed /r
+			ttg <- round(tpr/60 * (nrow(raster) - r), digits=1)
+			cat('row', r, '-', ttg, 'minutes to go\n')
+		}		
 	}
 	if (inMemory) {
 		to <- setValues(to, v) 

Modified: pkg/raster/R/replacement.R
===================================================================
--- pkg/raster/R/replacement.R	2009-03-07 12:47:17 UTC (rev 322)
+++ pkg/raster/R/replacement.R	2009-03-07 15:11:55 UTC (rev 323)
@@ -33,6 +33,8 @@
 				stop('raster too large.')
 			}	
 		}
+		i <- subset(i, (i <= ncell(x)))
+		i <- subset(i, (i >= 1))
 		x at data@values[i] <- value
 		x at data@source <- 'ram'
 		x <- setFilename(x, "")

Modified: pkg/raster/R/resample.R
===================================================================
--- pkg/raster/R/resample.R	2009-03-07 12:47:17 UTC (rev 322)
+++ pkg/raster/R/resample.R	2009-03-07 15:11:55 UTC (rev 323)
@@ -20,7 +20,7 @@
 		to <- setFilename(to, filename )
 		if (options('verbose')[[1]]) { cat('writing raster to:', filename(to))	}
 	}
-	if (filename(to) == "") { inMemory <- TRUE} else { inMemory <- FALSE }
+	inMemory <- filename(to) == ""
 
 	v <- vector(length=0)
 	rowCells <- 1:ncol(to)

Modified: pkg/raster/man/copyRasterFile.Rd
===================================================================
--- pkg/raster/man/copyRasterFile.Rd	2009-03-07 12:47:17 UTC (rev 322)
+++ pkg/raster/man/copyRasterFile.Rd	2009-03-07 15:11:55 UTC (rev 323)
@@ -6,7 +6,7 @@
 \title{move or copy a raster file}
 
 \description{
-  Move or copy a raster file to a different location (filename). 
+Move or copy a raster file to a different location (filename). 
 }
 
 \usage{
@@ -20,8 +20,6 @@
   \item{overwrite}{Logical. If \code{TRUE} existing files are overwritten}
 }
 
-\details{
-}
 
 \value{
 a RasterLayer

Modified: pkg/raster/man/extent.Rd
===================================================================
--- pkg/raster/man/extent.Rd	2009-03-07 12:47:17 UTC (rev 322)
+++ pkg/raster/man/extent.Rd	2009-03-07 15:11:55 UTC (rev 323)
@@ -11,7 +11,6 @@
 \alias{isLatLon,Raster-method}
 \alias{isLatLon,CRS-method}
 \alias{isLatLon,character-method}
-\alias{projection<-}
 
   
 \title{Raster extent}

Modified: pkg/raster/man/project.Rd
===================================================================
--- pkg/raster/man/project.Rd	2009-03-07 12:47:17 UTC (rev 322)
+++ pkg/raster/man/project.Rd	2009-03-07 15:11:55 UTC (rev 323)
@@ -1,42 +1,53 @@
-\name{project}
+\name{projectRaster}
+
 \alias{projectRaster}
 \alias{projectBbox}
 
-\title{Change the projection of a RasterLayer}
+\title{project a RasterLayer}
+
 \description{
-	Change the projection of a RasterLayer. 
+Change the projection of a RasterLayer. 
 }
+
 \usage{
-projectRaster(from, to, method="nngb", overwrite=FALSE)
+projectRaster(from, to, method="ngb", filename=NULL, filetype='raster', datatype='FLT4S', overwrite=FALSE, track=-1) 
 projectBbox(object, projstring)
 }
 
 \arguments{
   \item{from}{a RasterLayer object}
-  \item{to}{a BasicRaster object (or any Raster* object}
-  \item{method}{character. Only 'nngb', or neirest neighbor, has been implmented sofar}
-  \item{overwrite}{if \code{TRUE}, the file will be overwritten if it exists}
+  \item{to}{a Raster* object}
+  \item{method}{method used to compute values for the new RasterLayer}
+  \item{filename}{character. output filename}  
+  \item{overwrite}{Logical. If \code{TRUE}, the output file is overwritten if it already exists}
+  \item{filetype}{output file type. Either 'raster', 'ascii' or a supported GDAL 'driver' name see \code{\link[raster]{writeRaster}}}
+  \item{datatype}{output data type; see \code{\link[raster]{setDatatype}}}
+  \item{track}{vector of row numbers for which the function will report that they have been processed}  
   \item{object}{BoundingBox or Raster* object}
-  \item{projstring}{a character string describing a projection and datum in PROJ4 format }
+  \item{projstring}{a character string describing a projection and datum in PROJ4 format}
+
 }
 
 \details{
- First create projected bbox (in BasicRaster object). Then project. 
+First create a RasterLayer with a projected BoundingBox; also set the number of rows and columns. 
+Then use that object to project the input RasterLayer to. 
   
- See \code{projInfo('proj')}, \code{projInfo('ellps')}, and \code{projInfo('datum')} for options for valid PROJ.4 strings. 
+Valid values for \code{method} are \code{'ngb'} (nearest neighbor) and \code{'bilinear'} (bilinear interpolation).
+
+See \code{projInfo('proj')}, \code{projInfo('ellps')}, and \code{projInfo('datum')} for options for valid PROJ.4 strings. 
  
- You can consult this page: \url{http://www.remotesensing.org/geotiff/proj_list/} to find the parameter options and names for projections.
+You can consult this page: \url{http://www.remotesensing.org/geotiff/proj_list/} to find the parameter options and names for projections.
  
- Projection is performed using the PROJ.4 library accesed through the rgdal package. Also see \code{?CRS}
+Projection is performed using the PROJ.4 library accesed through the rgdal package. Also see \code{?CRS}
 }
 
 \value{
-  a new RasterLayer object (in the R environment), with, in some cases, the side-effect of a raster file written to disk 
+a RasterLayer object, and, in some cases, the side-effect of a raster file written to disk 
 }
 
 \author{Robert J. Hijmans}
 
-\seealso{ \code{\link[rgdal]{CRS}}, \code{\link[rgdal]{projInfo}}}
+\seealso{ \code{\link[rgdal]{CRS-class}}, \code{\link[rgdal]{projInfo}}}
 
 \examples{
 # create a new (not projected) RasterLayer with cellnumbers as values
@@ -45,10 +56,6 @@
 # proj.4 projection description
 newproj <- "+proj=lcc +lat_1=48 +lat_2=33 +lon_0=-100 +ellps=WGS84"
 
-
-# for debugging R-Forge...
- drop(read.dcf(file=system.file("DESCRIPTION", package="rgdal"), fields=c("Version","Date")))
-
 # create a new RasterLayer with a BoundingBox with the new projection
 projras <- projectBbox(r, newproj)
 # Adjust the cell size 

Modified: pkg/raster/man/set.Rd
===================================================================
--- pkg/raster/man/set.Rd	2009-03-07 12:47:17 UTC (rev 322)
+++ pkg/raster/man/set.Rd	2009-03-07 15:11:55 UTC (rev 323)
@@ -3,6 +3,7 @@
 \alias{setFilename}
 \alias{filename<-}
 \alias{setProjection}
+\alias{projection<-}
 \alias{setRowCol}
 \alias{ncol<-}
 \alias{nrow<-}
@@ -11,7 +12,7 @@
 \alias{setMinMax}
 \alias{setNAvalue}
 
-\title{ Set properties of a RasterLayer object }
+\title{ Set properties of a RasterLayer }
 
 \description{
 Use these functions to set certain properties of a RasterLayer object. 
@@ -49,8 +50,11 @@
 }
 
 \value{
-	a Raster* object
+a Raster* object
 }
+
+\seealso{ \code{\link[raster]{extent}}, \code{\link[raster]{dimensions}}  }
+
 \author{ Robert J. Hijmans }
 
 \examples{ 

Modified: pkg/raster/man/setDatatype.Rd
===================================================================
--- pkg/raster/man/setDatatype.Rd	2009-03-07 12:47:17 UTC (rev 322)
+++ pkg/raster/man/setDatatype.Rd	2009-03-07 15:11:55 UTC (rev 323)
@@ -1,11 +1,12 @@
-\name{SetDatatype}
+\name{setDatatype}
 \alias{datatype<-}
 \alias{setDatatype}
 
-\title{ Set data type of a RasterLayer}
+\title{ Set data type }
 
 \description{
 Use these functions to set the datatype properties of a RasterLayer object. The datatype determines how values are written to disk. 
+It does not affect the way they are stored in memory.
 }
 
 \usage{
@@ -27,32 +28,23 @@
 
 The following datatypes are available.
 
-datatype, size, signed: range of possible values
+\tabular{rll}{
+\bold{datatype \tab min \tab max} \cr
+LOG1S \tab  FALSE (0)\tab TRUE (1) \cr
+INT1S \tab -127 \tab  127\cr
+INT1U \tab 0 \tab  255\cr
+INT2S \tab -32,767\tab  32,767\cr
+INT2U \tab  0 \tab  65,534\cr
+INT4S \tab  -2,147,483,647 \tab  2,147,483,647\cr
+INT4U \tab  0 \tab  4,294,967,294\cr
+INT8S \tab  -9,223,372,036,854,775,807 \tab  9,223,372,036,854,775,807\cr
+INT4U \tab  0 \tab  18,446,744,073,709,551,614\cr
+FLT4S \tab  -3.4E38 \tab  3.4E38\cr
+FLT8S \tab   -1.7E308 \tab   1.7E308\cr
+}
 
-'LOG1S', FALSE (0) to TRUE (1)
-
-'INT1S: -127 to 127
-
-'INT1U': 0 to 255
-
-'INT2S': -32,767 to 32,767
-
-'INT2U': 0 to 65,534
-
-'INT4S': -2,147,483,647 to 2,147,483,647
-
-'INT4U': 0 to 4,294,967,294
-
-'INT8S': -9,223,372,036,854,775,807 to 9,223,372,036,854,775,807
-
-'INT4U': 0 to 18,446,744,073,709,551,614
-
-'FLT4S': -3.4E38 to 3.4E38
-
-'FLT8S':  -1.7E308 to  1.7E308
-
-
-For all integer types, except the single byte types, the lowest (signed) or highest (unsigned) value is used to store NA. Single byte files do not have an NA.
+For all integer types, except the single byte types, the lowest (signed) or highest (unsigned) value is used to store NA. 
+Single byte files do not have NA values.
 Logical values are stored as signed single byte integers, they do have an NA value (-127)
 }
 

Modified: pkg/raster/man/setValues.Rd
===================================================================
--- pkg/raster/man/setValues.Rd	2009-03-07 12:47:17 UTC (rev 322)
+++ pkg/raster/man/setValues.Rd	2009-03-07 15:11:55 UTC (rev 323)
@@ -36,11 +36,9 @@
   \item{cellnumbers}{ the cell numbers corresponding to the sparse data }
 }
 
-\details{
-}
 
 \value{
-	a Raster* object
+a Raster* object
 }
 \author{ Robert J. Hijmans }
 

Modified: pkg/raster/man/writeRaster.Rd
===================================================================
--- pkg/raster/man/writeRaster.Rd	2009-03-07 12:47:17 UTC (rev 322)
+++ pkg/raster/man/writeRaster.Rd	2009-03-07 15:11:55 UTC (rev 323)
@@ -5,12 +5,12 @@
 \title{Write raster data to a file}
 
 \description{
-  Write raster values to a file. Either as binary ('raster') format, Ascii (ESRI Arc Ascii) format, and other GDAL supported formats. 
-  You can write an entire raster dataset at once, or by row.
+Write raster values to a file. Either as binary ('raster') format, Ascii (ESRI Arc Ascii) format, and other GDAL supported formats. 
+You can write an entire raster dataset at once, or by row.
 }
 
 \usage{
-  writeRaster(raster, filetype='raster', overwrite=FALSE)
+writeRaster(raster, filetype='raster', overwrite=FALSE)
 }
 
 \arguments{
@@ -20,15 +20,15 @@
 }
 
 \details{
-	Values in a \code{RasterLayer} object are written to a file. Relatively small datasets can be loaded into memory, manipulated, and saved to disk in their entirety. 
-	Large datasets can be read and written row by row. 
-	When writing by row, you must write all rows, and you must write them in sequence. Start at row=1, and end at row=nrow(raster). You cannot overwrite a single row in an existing raster file.
-	See \code{writeFormats} for supported file types ("formats", "drivers").
+Values in a \code{RasterLayer} object are written to a file. Relatively small datasets can be loaded into memory, manipulated, and saved to disk in their entirety. 
+Large datasets can be read and written row by row. 
+When writing by row, you must write all rows, and you must write them in sequence. Start at row=1, and end at row=nrow(raster). You cannot overwrite a single row in an existing raster file.
+See \code{writeFormats} for supported file types ("formats", "drivers").
 
 Supported file types include:
 
 \tabular{rll}{
-	\tab \code{\bold{file type}} \tab \bold{Long name} \cr
+	\tab \bold{file type \tab Long name} \cr
     \tab \code{raster} \tab 'Native' raster package format \cr
     \tab \code{ascii}  \tab ESRI Ascii \cr
     \tab \code{ENVI}   \tab ENVI .hdr Labelled \cr
@@ -97,8 +97,6 @@
 	binras <- setValues(binras, values(rs), r)
 	binras <- writeRaster(binras, filetype="GTiff", overwrite=TRUE) 
 }
-  
- 
 }
 
 \keyword{ spatial }

Modified: pkg/raster/man/writeadvanced.Rd
===================================================================
--- pkg/raster/man/writeadvanced.Rd	2009-03-07 12:47:17 UTC (rev 322)
+++ pkg/raster/man/writeadvanced.Rd	2009-03-07 15:11:55 UTC (rev 323)
@@ -1,7 +1,7 @@
-\name{advanced raster file writing}
+\name{advanced writing}
 \alias{closeHandle}
 
-\title{ Create a new RasterLayer object }
+\title{ advanced writing }
 \description{ low level functions for programmers 
 }
 \usage{
@@ -12,7 +12,7 @@
   \item{raster}{a RasterLayer object}
 }
 \details{
-  closeHandle removes the handle to the file on disk that was established by rasterFromFile(). 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). 
+closeHandle removes the handle to the file on disk that was established by rasterFromFile(). 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). 
 }
 
 \value{
@@ -20,6 +20,4 @@
 }
 \author{Robert J. Hijmans}
 
-\examples{
-}
 \keyword{ spatial }



More information about the Raster-commits mailing list