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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Feb 22 04:02:59 CET 2009


Author: rhijmans
Date: 2009-02-22 04:02:58 +0100 (Sun, 22 Feb 2009)
New Revision: 288

Added:
   pkg/raster/R/rasterToPoints.R
   pkg/raster/man/rasterToPoints.Rd
Modified:
   pkg/raster/DESCRIPTION
   pkg/raster/man/PolygonsToRaster.Rd
Log:


Modified: pkg/raster/DESCRIPTION
===================================================================
--- pkg/raster/DESCRIPTION	2009-02-16 02:05:35 UTC (rev 287)
+++ pkg/raster/DESCRIPTION	2009-02-22 03:02:58 UTC (rev 288)
@@ -2,7 +2,7 @@
 Type: Package
 Title: Raster data handling for geographic data analysis and modeling
 Version: 0.8.8-5
-Date: 12-Feb-2009
+Date: 22-Feb-2009
 Depends: methods, sp, rgdal (>= 0.5-33), R (>= 2.8.0)
 Author: Robert J. Hijmans & Jacob van Etten
 Maintainer: Robert J. Hijmans <r.hijmans at gmail.com> 

Added: pkg/raster/R/rasterToPoints.R
===================================================================
--- pkg/raster/R/rasterToPoints.R	                        (rev 0)
+++ pkg/raster/R/rasterToPoints.R	2009-02-22 03:02:58 UTC (rev 288)
@@ -0,0 +1,48 @@
+# Author: Robert J. Hijmans, r.hijmans at gmail.com
+# International Rice Research Institute
+# Date :  February 2009
+# Version 0.8
+# Licence GPL v3
+
+
+
+rasterToPoints <- function(raster, rm.na=TRUE, fun=NULL, asSpatialPoints=FALSE) {
+	if (dataContent(raster) == 'all') {
+		v <- values(raster)
+		raster <- clearValues(raster)
+		xyv <- cbind(xyFromCell(raster, 1:ncell(raster)), v)
+		if (rm.na) {
+			xyv <- subset(xyv, !(is.na(xyv[,3])))
+		}
+		if (!is.null(fun)) {
+			xyv <- subset(xyv, fun(xyv[,3]))
+		}
+	} else {
+		if (dataSource == 'ram') {
+			return(xyFromCell(raster, 1:ncell(raster)))
+		} else {
+			xyv <- matrix(NA, ncol=3, nrow=0)
+			colnames(xyv) <- c('x', 'y', 'v')
+			x <- xFromCol(raster, 1:ncol(raster))
+			for (r in 1:nrow(raster)) {
+				y <- yFromRow(raster, r)
+				raster <- readRow(raster, r)
+				xyvr <- cbind(x, y, values(raster))
+				if (rm.na) {
+					xyvr <- subset(xyvr, !(is.na(xyvr[,3])))
+				}
+				if (!is.null(fun)) {
+					xyvr <- subset(xyvr, fun(xyvr[,3]))
+				}
+				xyv <- rbind(xyv, xyvr)
+			}
+		}
+	}
+	if (asSpatialPoints) {
+		coords <- xyv[,1:2]
+		row.names(coords) <- 1:nrow(coords)
+		return(SpatialPointsDataFrame(coords=coords, data=as.data.frame(xyv[,3]), proj4string=projection(raster, asText=F)))
+	} else {
+		return(xyv)
+	}
+}

Modified: pkg/raster/man/PolygonsToRaster.Rd
===================================================================
--- pkg/raster/man/PolygonsToRaster.Rd	2009-02-16 02:05:35 UTC (rev 287)
+++ pkg/raster/man/PolygonsToRaster.Rd	2009-02-22 03:02:58 UTC (rev 288)
@@ -4,7 +4,7 @@
 
 \title{ Polygons to raster conversion}
 \description{
-Two algorithms for polygon to raster conversion. The polygons are rasterized. Either values associated with each polygon, or a polygon ID is transferred. Holes in polygons are recognized by polygonsToRaster, but not by polygonsToRaster2.
+Polygon to raster conversion. The polygons are rasterized. Either values associated with each polygon, or a polygon ID is transferred. Holes in polygons are recognized if they are correctly specified.
 }
 \usage{
 polygonsToRaster(spPolys, raster, field=0, filename="", overwrite=FALSE, updateRaster=FALSE, updateValue="NA",  filetype='raster',  datatype='FLT4S', track=c(100, 500, 1:(round(nrow(raster)/1000)) * 1000)) 

Added: pkg/raster/man/rasterToPoints.Rd
===================================================================
--- pkg/raster/man/rasterToPoints.Rd	                        (rev 0)
+++ pkg/raster/man/rasterToPoints.Rd	2009-02-22 03:02:58 UTC (rev 288)
@@ -0,0 +1,39 @@
+\name{raster to points}
+
+\alias{rasterToPoints}
+
+\title{ Raster to points conversion}
+\description{
+Raster to point conversion.
+}
+\usage{
+rasterToPoints(raster, rm.na=TRUE, fun=NULL, asSpatialPoints=FALSE)
+}
+
+\arguments{
+  \item{raster}{ a RasterLayer object }
+  \item{rm.na}{ if \code{TRUE}, cells with NA values will not be converted  }
+  \item{fun}{ function to select a subset of raster values}
+  \item{asSpatialPoints}{if \code{TRUE}, the function returns a SpatialPointsDataFrame object }
+}
+
+\details{
+fun should be a simple function returning a logical value
+e.g.:
+ fun=function(x){x==1}
+ fun=function(x){x>3}
+}
+
+\author{Robert J. Hijmans}
+
+
+\examples{ 
+ r <- raster(nrow=18, ncol=36)
+ r[] <- round(10 * runif(ncell(r)))
+ r[r>8] <- NA
+ p <- rasterToPoints(r, fun=function(x){x>5})
+#plot(r)
+#points(p)
+}
+
+\keyword{ spatial }



More information about the Raster-commits mailing list