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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Jan 16 00:28:00 CET 2009


Author: rhijmans
Date: 2009-01-16 00:28:00 +0100 (Fri, 16 Jan 2009)
New Revision: 142

Modified:
   pkg/raster/R/get.R
   pkg/raster/man/get.Rd
Log:


Modified: pkg/raster/R/get.R
===================================================================
--- pkg/raster/R/get.R	2009-01-15 17:23:46 UTC (rev 141)
+++ pkg/raster/R/get.R	2009-01-15 23:28:00 UTC (rev 142)
@@ -75,6 +75,8 @@
 }
 
 
+
+
 cellFromRowCol <- function(object, rownr, colnr) {
 	if (.isSPgrid(object)) { object <- asRasterLayer(object, FALSE) }
 	rownr <- round(rownr)
@@ -119,20 +121,25 @@
 	return(xy)
 }  
 	
+
+cellFromBbox <- function(object, bbox) {
+	if (.isSPgrid(object)) { object <- asRasterLayer(object, FALSE) }
+	bbox <- getBbox(bbox)
+	startrow <- rowFromY(object, ymax(bbox))
+	endrow <- rowFromY(object, ymin(bbox))
+	startcol <- colFromX(object, xmin(bbox))
+	endcol <- colFromX(object, xmax(bbox))
+	cols <- rep(startcol:endcol, times=(endrow - startrow))
+	rows <- rep(startrow:endrow, each=(endcol - startcol))
+	cells <- cellFromRowCol(object, rows, cols)
+	return(cells)
+}
+
 	
-cxyFromBox <- function(object, xmn=xmin(object), xmx=xmax(object), ymn=ymin(object), ymx=ymax(object)) {
+cxyFromBbox <- function(object, bbox) {
 	if (.isSPgrid(object)) { object <- asRasterLayer(object, FALSE) }
-	firstrow <- rowFromY(object, ymx)
-	lastrow <- rowFromY(object, ymn)
-	firstcol <- colFromX(object, xmn)
-	lastcol <- colFromX(object, xmx)
-	cells <- vector("integer", length=0)
-# RH: ouch, this should be done with apply 	
-	for (i in firstrow:lastrow) {
-		firstcell <- (i-1) * ncol(object) + firstcol
-		lastcell <- (i-1) * ncol(object) + lastcol
-		cells <- append(cells, c(firstcell:lastcell))
-	}
+	bbox <- getBbox(bbox)
+	cells <- cellFromBbox(object, bbox)
 	cxy <- cbind(cells, xyFromCell(object, cells))
 	colnames(cxy) <- c("cell", "x", "y")
 	return(cxy)

Modified: pkg/raster/man/get.Rd
===================================================================
--- pkg/raster/man/get.Rd	2009-01-15 17:23:46 UTC (rev 141)
+++ pkg/raster/man/get.Rd	2009-01-15 23:28:00 UTC (rev 142)
@@ -2,19 +2,20 @@
 \alias{colFromCell}
 \alias{rowFromCell}
 \alias{cellFromRowCol}
+\alias{cellFromBbox}
 \alias{colFromX}
 \alias{rowFromY}
 \alias{cellFromXY}
 \alias{xFromCol}
 \alias{yFromRow}
 \alias{xyFromCell}
-\alias{cxyFromBox}
+\alias{cxyFromBbox}
 \alias{validCells}
 \alias{validCols}
 \alias{validRows}
 
   
-\title{ Get the column, row, or cell number of a raster from coordinates and vice versa }
+\title{Get the column, row, or cell number of a raster from coordinates and vice versa}
 
 \description{
 These functions get the column, row, or cell number of a Raster* object for a x and/or y coordinate or get the coordinates of the center of a raster cell from a column, or cell number(s)
@@ -24,33 +25,31 @@
 colFromCell(object, cell)
 rowFromCell(object, cell)
 cellFromRowCol(object, rownr, colnr)
+cellFromBbox(object, bbox)
 colFromX(object, x)
 rowFromY(object, y)
 cellFromXY(object, xy)
 xFromCol(object, colnr)
 yFromRow(object, rownr)
 xyFromCell(object, cell, asSpatialPoints, projstring)
-cxyFromBox(object, xmn=xmin(object), xmx=xmax(object), ymn=ymin(object), ymx=ymax(object))
+cxyFromBbox(object, bbox)
 validCells(object, cell) 
 validCols(object, colnr) 
 validRows(object, rownr) 
 }
 
 \arguments{
-  \item{object}{ Raster* object (or a SpatialPixels* or SpatialGrid* object) }
-  \item{cell}{ cell number(s) }
-  \item{x}{ x coordinate(s) }
-  \item{y}{ y coordinate(s) }
-  \item{xy}{ matrix of x and y coordinates, or a SpatialPoints or SpatialPointsDataFrame object }
-  \item{colnr}{ column number(s) }
-  \item{rownr}{ row number(s) }
-  \item{asSpatialPoints}{ return a SpatialPoints object (sp package) instead of a matrix}
-  \item{projstring}{ PROJ4 format character string to set projection for SpatialPoints object }
-  \item{xmn}{ minimum x coordinate of bounding box to get cell numbers for }
-  \item{xmx}{ maximum x coordinate of bounding box to get cell numbers for }
-  \item{ymn}{ minimum y coordinate of bounding box to get cell numbers for }
-  \item{ymx}{ maximum y coordinate of bounding box to get cell numbers for }
- }
+  \item{object}{Raster* object (or a SpatialPixels* or SpatialGrid* object)}
+  \item{cell}{cell number(s)}
+  \item{x}{x coordinate(s)}
+  \item{y}{y coordinate(s)}
+  \item{xy}{matrix of x and y coordinates, or a SpatialPoints or SpatialPointsDataFrame object}
+  \item{colnr}{column number(s)}
+  \item{rownr}{row number(s)}
+  \item{bbox}{A boundinBox object (or an object that can be coerced to a BoundingBox object)}
+  \item{asSpatialPoints}{return a SpatialPoints object (sp package) instead of a matrix}
+  \item{projstring}{PROJ4 format character string to set projection for SpatialPoints object}
+}
   
 \details{
   The colFromCell and similar functions accept a single value (or x, y pair), or a vector or list of these values,
@@ -86,4 +85,4 @@
 #xy for corners of grid:
 xyFromCell(rs, c(1, ncol(rs), nrow(rs), ncells(rs)))
 }
-\keyword{ spatial }
+\keyword{spatial}



More information about the Raster-commits mailing list