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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri May 8 12:19:42 CEST 2009


Author: rhijmans
Date: 2009-05-08 12:19:42 +0200 (Fri, 08 May 2009)
New Revision: 444

Added:
   pkg/raster/R/projectRaster.R
   pkg/raster/man/projectRaster.Rd
Removed:
   pkg/raster/R/project.R
   pkg/raster/man/project.Rd
Modified:
   pkg/raster/man/projection.Rd
Log:


Deleted: pkg/raster/R/project.R
===================================================================
--- pkg/raster/R/project.R	2009-05-08 08:54:59 UTC (rev 443)
+++ pkg/raster/R/project.R	2009-05-08 10:19:42 UTC (rev 444)
@@ -1,103 +0,0 @@
-# Author: Robert J. Hijmans, r.hijmans at gmail.com
-# International Rice Research Institute
-# Date :  January 2009
-# Version 0.8
-# Licence GPL v3
-
-	
-
-
-projectBbox <- function(object, projs) {
-	b <- extent(object)
-	projs <- projection(projs)
-	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(object)) {
-		p <- project(xy, projs, inv=FALSE)
-	} else {
-		p <- project(xy, projection(object), inv=TRUE)
-		if (!isLatLon(projs)) {
-			p <- project(p, projs, inv=FALSE)
-		}
-	} 
-
-	xmin <- min(p[,1])
-	xmax <- max(p[,1])
-	ymin <- min(p[,2])
-	ymax <- max(p[,2])	
-	bb <- newBbox(xmin, xmax, ymin, ymax)
-	obj <- setExtent(object, bb)
-	projection(obj) <- projs
-	return(obj)
-}
-
-.xyTransform <- function(crds, from, to) {
-# This function was extracted from "spTransform.SpatialPoints" in the rgdal package
-# Copyright (c) 2003-8 by Barry Rowlingson, Roger Bivand, and Edzer Pebesma
-	res <- .Call("transform", from, to, nrow(crds), as.double(crds[,1]), as.double(crds[,2]), PACKAGE="rgdal")
-	if (any(!is.finite(res[[1]])) || any(!is.finite(res[[2]]))) {
-		k <- which(!is.finite(res[[1]]) || !is.finite(res[[2]]))
-		cat("non finite transformation detected:\n")
-		print(cbind(crds, res[[1]], res[[2]])[k,])
-		stop(paste("failure in points", paste(k, collapse=":")))
-	}
-	return(cbind(res[[1]], res[[2]]))
-}
-
-
-projectRaster <- function(from, to, method="ngb", filename="", filetype='raster', datatype='FLT4S', overwrite=FALSE, track=-1)  {
-	validObject(to)
-	validObject(projection(from, asText=FALSE))
-	validObject(projection(to, asText=FALSE))
-	projfrom <- projection(from)
-	projto <- projection(to)
-	if (projfrom == "NA") {stop("input projection is NA")}
-	if (projto == "NA") {stop("output projection is NA")}
-	
-	pbb <- projectBbox(to, projection(from))
-	bb <- intersectBbox(pbb, from)
-	validObject(bb)
-
-
-	if (!method %in% c('bilinear', 'ngb')) { stop('invalid method') 	}
-	filename <- trim(filename)
-	to <- raster(to)
-	filename(to) <- filename
-	dataType(to) <- datatype
-
-	rowCells <- 1:ncol(to)
-
-	if (!canProcessInMemory(to, 1) && filename(to) == "") {
-		filename <- rasterTmpFile()
-		filename(to) <- filename
-		if (getOption('verbose')) { cat('writing raster to:', filename(to))	}
-	}
-	inMemory <- to at file@name == ""
-	v <- vector(length=0)
-
-	starttime <- proc.time()
-	for (r in 1:nrow(to)) {
-		cells <- rowCells + (r-1) * ncol(to)
-		xy <- xyFromCell(to, cells)
-		unProjXY <- .xyTransform(xy, projto, projfrom)
-		
-		if (method=='ngb') {
-			vals <- xyValues(from, unProjXY)
-		} else {
-			vals <- xyValues(from, unProjXY, method='bilinear')
-		}
-
-		if (inMemory) {
-			v <- c(v, vals)
-		} else {
-			to <- setValues(to, vals, r)
-			to <- writeRaster(to, overwrite=overwrite)
-		}
-		if (r %in% track) { .showTrack(r, to at nrows, track, starttime) }
-	}
-	if (inMemory) {
-		to <- setValues(to, v) 
-	}
-	return(to)
-}
-

Added: pkg/raster/R/projectRaster.R
===================================================================
--- pkg/raster/R/projectRaster.R	                        (rev 0)
+++ pkg/raster/R/projectRaster.R	2009-05-08 10:19:42 UTC (rev 444)
@@ -0,0 +1,92 @@
+# Author: Robert J. Hijmans, r.hijmans at gmail.com
+# International Rice Research Institute
+# Date :  January 2009
+# Version 0.8
+# Licence GPL v3
+
+	
+.xyTransform <- function(crds, from, to) {
+# May 2009. This function was extracted from "spTransform.SpatialPoints" in the rgdal package
+# Copyright (c) 2003-8 by Barry Rowlingson, Roger Bivand, and Edzer Pebesma
+	res <- .Call("transform", from, to, nrow(crds), as.double(crds[,1]), as.double(crds[,2]), PACKAGE="rgdal")
+	if (any(!is.finite(res[[1]])) || any(!is.finite(res[[2]]))) {
+		k <- which(!is.finite(res[[1]]) || !is.finite(res[[2]]))
+		cat("non finite transformation detected:\n")
+		print(cbind(crds, res[[1]], res[[2]])[k,])
+		stop(paste("failure in points", paste(k, collapse=":")))
+	}
+	return(cbind(res[[1]], res[[2]]))
+}
+
+
+
+projectBbox <- function(object, projs) {
+	projfrom <- projection(object)
+	projto <- projection(projs)
+	validObject(projection(object, asText=FALSE))
+	validObject(projection(projs, asText=FALSE))
+	b <- extent(object)
+	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))
+	p <- .xyTransform(xy, projfrom, projto)	
+	obj <- setExtent(object, newBbox(min(p[,1]), max(p[,1]), min(p[,2]),  max(p[,2])))
+	projection(obj) <- projs
+	return(obj)
+}
+
+
+projectRaster <- function(from, to, method="ngb", filename="", filetype='raster', datatype='FLT4S', overwrite=FALSE, track=-1)  {
+	validObject(to)
+	validObject(projection(from, asText=FALSE))
+	validObject(projection(to, asText=FALSE))
+	projfrom <- projection(from)
+	projto <- projection(to)
+	if (projfrom == "NA") {stop("input projection is NA")}
+	if (projto == "NA") {stop("output projection is NA")}
+	
+	pbb <- projectBbox(to, projection(from))
+	bb <- intersectBbox(pbb, from)
+	validObject(bb)
+
+
+	if (!method %in% c('bilinear', 'ngb')) { stop('invalid method') 	}
+	filename <- trim(filename)
+	to <- raster(to)
+	filename(to) <- filename
+	dataType(to) <- datatype
+
+	rowCells <- 1:ncol(to)
+
+	if (!canProcessInMemory(to, 1) && filename(to) == "") {
+		filename <- rasterTmpFile()
+		filename(to) <- filename
+		if (getOption('verbose')) { cat('writing raster to:', filename(to))	}
+	}
+	inMemory <- to at file@name == ""
+	v <- vector(length=0)
+
+	starttime <- proc.time()
+	for (r in 1:nrow(to)) {
+		cells <- rowCells + (r-1) * ncol(to)
+		xy <- xyFromCell(to, cells)
+		unProjXY <- .xyTransform(xy, projto, projfrom)
+		
+		if (method=='ngb') {
+			vals <- xyValues(from, unProjXY)
+		} else {
+			vals <- xyValues(from, unProjXY, method='bilinear')
+		}
+
+		if (inMemory) {
+			v <- c(v, vals)
+		} else {
+			to <- setValues(to, vals, r)
+			to <- writeRaster(to, overwrite=overwrite)
+		}
+		if (r %in% track) { .showTrack(r, to at nrows, track, starttime) }
+	}
+	if (inMemory) {
+		to <- setValues(to, v) 
+	}
+	return(to)
+}
+

Deleted: pkg/raster/man/project.Rd
===================================================================
--- pkg/raster/man/project.Rd	2009-05-08 08:54:59 UTC (rev 443)
+++ pkg/raster/man/project.Rd	2009-05-08 10:19:42 UTC (rev 444)
@@ -1,71 +0,0 @@
-\name{projectRaster}
-
-\alias{projectRaster}
-\alias{projectBbox}
-
-\title{project a RasterLayer}
-
-\description{
-Change the projection of a RasterLayer. 
-}
-
-\usage{
-projectRaster(from, to, method="ngb", filename="", filetype='raster', datatype='FLT4S', overwrite=FALSE, track=-1) 
-projectBbox(object, projs)
-}
-
-\arguments{
-  \item{from}{a RasterLayer object}
-  \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]{dataType}}}
-  \item{track}{vector of row numbers for which the function will report that they have been processed}  
-  \item{object}{BoundingBox or Raster* object}
-  \item{projs}{a character string describing a projection and datum in PROJ4 format}
-
-}
-
-\details{
-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. 
-  
-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.
- 
-Projection is performed using the PROJ.4 library accesed through the rgdal package. Also see \code{?CRS}
-}
-
-\value{
-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-class}}, \code{\link[rgdal]{projInfo}}}
-
-\examples{
-# create a new (not projected) RasterLayer with cellnumbers as values
-r <- raster(xmn=-110, xmx=-90, ymn=40, ymx=60, ncols=40, nrows=30)
-r <- setValues(r, 1:ncell(r))
-# proj.4 projection description
-newproj <- "+proj=lcc +lat_1=48 +lat_2=33 +lon_0=-100 +ellps=WGS84"
-
-# create a new RasterLayer with a BoundingBox with the new projection
-projras <- projectBbox(r, newproj)
-# Adjust the cell size 
-res(projras) <- 100000
-# project the values of RasterLayer 'r' to the new RasterLayer 'projras'
-projras <- projectRaster(r, projras)
-# inverse projection, back to the properties of 'r'
-inverse <- projectRaster(projras, r)
-
-}
-
-\keyword{spatial}
-

Added: pkg/raster/man/projectRaster.Rd
===================================================================
--- pkg/raster/man/projectRaster.Rd	                        (rev 0)
+++ pkg/raster/man/projectRaster.Rd	2009-05-08 10:19:42 UTC (rev 444)
@@ -0,0 +1,71 @@
+\name{projectRaster}
+
+\alias{projectRaster}
+\alias{projectBbox}
+
+\title{project a RasterLayer}
+
+\description{
+Change the projection of a RasterLayer. 
+}
+
+\usage{
+projectRaster(from, to, method="ngb", filename="", filetype='raster', datatype='FLT4S', overwrite=FALSE, track=-1) 
+projectBbox(object, projs)
+}
+
+\arguments{
+  \item{from}{a RasterLayer object}
+  \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]{dataType}}}
+  \item{track}{vector of row numbers for which the function will report that they have been processed}  
+  \item{object}{BoundingBox or Raster* object}
+  \item{projs}{a character string describing a projection and datum in PROJ4 format}
+
+}
+
+\details{
+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. 
+  
+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.
+ 
+Projection is performed using the PROJ.4 library accesed through the rgdal package. Also see \code{?CRS}
+}
+
+\value{
+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-class}}, \code{\link[rgdal]{projInfo}}}
+
+\examples{
+# create a new (not projected) RasterLayer with cellnumbers as values
+r <- raster(xmn=-110, xmx=-90, ymn=40, ymx=60, ncols=40, nrows=40)
+r <- setValues(r, 1:ncell(r))
+# proj.4 projection description
+newproj <- "+proj=lcc +lat_1=48 +lat_2=33 +lon_0=-100 +ellps=WGS84"
+
+# create a new RasterLayer with a BoundingBox with the new projection
+projras <- projectBbox(r, newproj)
+# Adjust the cell size 
+res(projras) <- 100000
+# project the values of RasterLayer 'r' to the new RasterLayer 'projras'
+projras <- projectRaster(r, projras)
+# inverse projection, back to the properties of 'r'
+inverse <- projectRaster(projras, r)
+
+}
+
+\keyword{spatial}
+

Modified: pkg/raster/man/projection.Rd
===================================================================
--- pkg/raster/man/projection.Rd	2009-05-08 08:54:59 UTC (rev 443)
+++ pkg/raster/man/projection.Rd	2009-05-08 10:19:42 UTC (rev 444)
@@ -25,8 +25,10 @@
 a Raster* object
 }
 
-\seealso{ \code{\link[raster]{project}, \link[rgdal]{CRS-class}, \link[rgdal]{projInfo}}}
+\seealso{ \code{\link[raster]{projectRaster}, \link[rgdal]{CRS-class}, \link[rgdal]{spTransform-methods}, \link[rgdal]{projInfo}}}
 
+\details{ projections are done by with the PROJ.4 library exposed by rgdal }
+
 \author{ Robert J. Hijmans }
 
 \examples{ 



More information about the Raster-commits mailing list