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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jan 28 17:16:17 CET 2009


Author: rhijmans
Date: 2009-01-28 17:16:17 +0100 (Wed, 28 Jan 2009)
New Revision: 205

Modified:
   pkg/raster/R/polygonToRaster.R
   pkg/raster/man/PolygonsToRaster.Rd
Log:


Modified: pkg/raster/R/polygonToRaster.R
===================================================================
--- pkg/raster/R/polygonToRaster.R	2009-01-28 16:04:10 UTC (rev 204)
+++ pkg/raster/R/polygonToRaster.R	2009-01-28 16:16:17 UTC (rev 205)
@@ -150,7 +150,7 @@
 
 polygonsToRaster2 <- function(sppoly, raster, field=0, filename="", overwrite=FALSE) {
 #  This is based on sampling by points. Should be slower except when  polygons very detailed and raster las ow resolution
-# but it could be optimized using the bounding boxes ofindividual polygons.
+# but it could be optimized further
 
 # check if bbox of raster and sppoly overlap
 	filename <- trim(filename)
@@ -176,14 +176,22 @@
 	v <- vector(length=0)
 	rowcol <- cbind(0, 1:ncol(raster))
 
+	firstrow <- rowFromY(raster, spbb[2,2])
+	lastrow <- rowFromY(raster, spbb[2,1])
+	
 	for (r in 1:nrow(raster)) {
-		rowcol[,1] <- r
-		sppoints <- xyFromCell(raster, cellFromRowCol(raster, rowcol[,1], rowcol[,2]), TRUE)
-		over <- overlay(sppoints, sppoly)
+		if (r < firstrow | r > lastrow) {
+			vals <- rep(NA, times=ncol(raster))
+		} else {
+			rowcol[,1] <- r
+			sppoints <- xyFromCell(raster, cellFromRowCol(raster, rowcol[,1], rowcol[,2]), TRUE)
+			over <- overlay(sppoints, sppoly)
+			vals <- putvals[over]
+		}
 		if (filename == "") {
-			v <- c(v, over)
+			v <- c(v, vals)
 		} else {
-			raster <- setValues(raster, putvals[over], r)
+			raster <- setValues(raster, vals, r)
 			raster <- writeRaster(raster)
 		}
 	}

Modified: pkg/raster/man/PolygonsToRaster.Rd
===================================================================
--- pkg/raster/man/PolygonsToRaster.Rd	2009-01-28 16:04:10 UTC (rev 204)
+++ pkg/raster/man/PolygonsToRaster.Rd	2009-01-28 16:16:17 UTC (rev 205)
@@ -1,6 +1,7 @@
 \name{polygons to raster}
 
 \alias{polygonsToRaster}
+\alias{polygonsToRaster2}
 
 \title{ Transform polygons to a raster }
 \description{



More information about the Raster-commits mailing list