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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Jan 27 15:45:56 CET 2009


Author: rhijmans
Date: 2009-01-27 15:45:56 +0100 (Tue, 27 Jan 2009)
New Revision: 195

Added:
   pkg/raster/R/pointdistance.R
   pkg/raster/man/pointdistance.Rd
   pkg/raster/man/rasterdistance.Rd
Removed:
   pkg/raster/man/distance.Rd
   pkg/raster/man/distance2.Rd
Modified:
   pkg/raster/R/distance.R
   pkg/raster/R/replacement.R
Log:


Modified: pkg/raster/R/distance.R
===================================================================
--- pkg/raster/R/distance.R	2009-01-27 13:10:16 UTC (rev 194)
+++ pkg/raster/R/distance.R	2009-01-27 14:45:56 UTC (rev 195)
@@ -1,62 +1,3 @@
-# Author: Robert J. Hijmans, r.hijmans at gmail.com
-# International Rice Research Institute
-# Date :  June 2008
-# Version 0,7
-# Licence GPL v3
-
-#should become methods of generic "distance"
-#... could become the method to distinguish between Euclidean and great circle distances. 
-#Methods should be defined for four column matrices, two column matrices (all combinations, resulting in a dist object) and SpatialPoints 
-
-distanceEuclidean <- function (point1, point2) {
-	if (length(point1) == 2) {
-		x1 <- point1[1]
-		y1 <- point1[2]
-	} else {
-		x1 <- point1[,1]
-		y1 <- point1[,2]
-	}
-	if (length(point2) == 2) {
-		x2 <- point2[1]
-		y2 <- point2[2]
-	} else {
-		x2 <- point2[,1]
-		y2 <- point2[,2]
-	}
-	distance <- sqrt((x1 - x2)^2 + (y1 - y2)^2)
-	return(distance)
-}
-
-distanceGreatcircle <- function (point1, point2, r=6378137) {
-# from degrees t o radians
-	if ((length(point1) < 2) | (length(point2) < 2)) { stop('points should have at least 2 elements') } 
-
-	point1 <- point1 * pi / 180
-	point2 <- point2 * pi / 180	
-	if (length(point1) == 2) {
-		x1 <- point1[[1]]
-		y1 <- point1[[2]]
-	} else {
-		x1 <- point1[[,1]]
-		y1 <- point1[[,2]]
-	}
-	if (length(point2) == 2) {
-		x2 <- point2[[1]]
-		y2 <- point2[[2]]
-	} else {
-		x2 <- point2[[,1]]
-		y2 <- point2[[,2]]
-	}
-
-#	cosd <- sin(y1) * sin(y2) + cos(y1) * cos(y2) * cos(x1-x2);
-#	distance <- r * acos(cosd);
-# supposedly more precise than above (http://en.wikipedia.org/wiki/Great_circle_distance):
-	x <- sqrt((cos(y2) * sin(x1-x2))^2 + (cos(y1) * sin(y2) - sin(y1) * cos(y2) * cos(x1-x2))^2)
-	y <- sin(y1) * sin(y2) + cos(y1) * cos(y2) * cos(x1-x2)
-	distance <- r * atan2(x, y)
-	return(distance)
-}
-
 # Author: Jacob van Etten jacobvanetten at yahoo.com
 # International Rice Research Institute
 # Date :  January 2009
@@ -65,14 +6,13 @@
 
 setGeneric("distance", function(object, ...) standardGeneric("distance"))
 
-setMethod("distance", signature(object = "RasterLayer"), def = function(object)
-	{
+setMethod("distance", signature(object = "RasterLayer"), def = 
+	function(object) {
 		n <- ncell(object)
 		fromCells <- which(!is.na(values(object)))
 		toCells <- which(is.na(values(object)))
 		accDist <- rep(0,times=n)
-		if (isLatLon(object))
-		{
+		if (isLatLon(object)) {
 			while(length(toCells)>0)
 			{			
 				adj <- adjacency(object,fromCells=fromCells,toCells=toCells,directions=8)
@@ -85,11 +25,8 @@
 				accDist[fromCells] <- transitionValues 
 				toCells <- toCells[!(toCells %in% fromCells)]
 			}
-		}
-		if (!(isLatLon(object)))
-		{
-			while(length(toCells)>0)
-			{			
+		} else {
+			while(length(toCells)>0) {			
 				adj1 <- adjacency(object,fromCells=fromCells,toCells=toCells,directions=4)
 				adj2 <- adjacency(object,fromCells=fromCells,toCells=toCells,directions="Bishop")
 				distance <- c(rep(1,length=length(adj1[,1])),rep(sqrt(2),length=length(adj1[,1])))

Added: pkg/raster/R/pointdistance.R
===================================================================
--- pkg/raster/R/pointdistance.R	                        (rev 0)
+++ pkg/raster/R/pointdistance.R	2009-01-27 14:45:56 UTC (rev 195)
@@ -0,0 +1,53 @@
+# Author: Robert J. Hijmans, r.hijmans at gmail.com
+# International Rice Research Institute
+# Date :  June 2008
+# Version 0,7
+# Licence GPL v3
+
+distanceEuclidean <- function (point1, point2) {
+	if (length(point1) == 2) {
+		x1 <- point1[1]
+		y1 <- point1[2]
+	} else {
+		x1 <- point1[,1]
+		y1 <- point1[,2]
+	}
+	if (length(point2) == 2) {
+		x2 <- point2[1]
+		y2 <- point2[2]
+	} else {
+		x2 <- point2[,1]
+		y2 <- point2[,2]
+	}
+	distance <- sqrt((x1 - x2)^2 + (y1 - y2)^2)
+	return(distance)
+}
+
+distanceGreatcircle <- function (point1, point2, r=6378137) {
+	if ((length(point1) < 2) | (length(point2) < 2)) { stop('points should have at least 2 elements') } 
+# from degrees t o radians
+	point1 <- point1 * pi / 180
+	point2 <- point2 * pi / 180	
+	if (length(point1) == 2) {
+		x1 <- point1[[1]]
+		y1 <- point1[[2]]
+	} else {
+		x1 <- point1[[,1]]
+		y1 <- point1[[,2]]
+	}
+	if (length(point2) == 2) {
+		x2 <- point2[[1]]
+		y2 <- point2[[2]]
+	} else {
+		x2 <- point2[[,1]]
+		y2 <- point2[[,2]]
+	}
+
+#	cosd <- sin(y1) * sin(y2) + cos(y1) * cos(y2) * cos(x1-x2);
+#	distance <- r * acos(cosd);
+# supposedly more precise than above (http://en.wikipedia.org/wiki/Great_circle_distance):
+	x <- sqrt((cos(y2) * sin(x1-x2))^2 + (cos(y1) * sin(y2) - sin(y1) * cos(y2) * cos(x1-x2))^2)
+	y <- sin(y1) * sin(y2) + cos(y1) * cos(y2) * cos(x1-x2)
+	distance <- r * atan2(x, y)
+	return(distance)
+}

Modified: pkg/raster/R/replacement.R
===================================================================
--- pkg/raster/R/replacement.R	2009-01-27 13:10:16 UTC (rev 194)
+++ pkg/raster/R/replacement.R	2009-01-27 14:45:56 UTC (rev 195)
@@ -60,8 +60,7 @@
 				if (dataSource(x) == 'disk') {
 					x <- readAll(x)
 				} else {
-					x <- setValues(x, vector(length=ncell(x)), v)
-					x at data@values[] <- NA
+					x <- setValues(x, rep(NA, times=ncell(x)))
 				}
 			} else {
 				stop('Large raster with no data in memory, use readAll() first')
@@ -70,6 +69,7 @@
 		x at data@values[i] <- value
 		x at data@source <- 'ram'
 		x <- setFilename(x, "")
+		x <- setMinMax(x)
 		return(x)
 	}
 )

Deleted: pkg/raster/man/distance.Rd
===================================================================
--- pkg/raster/man/distance.Rd	2009-01-27 13:10:16 UTC (rev 194)
+++ pkg/raster/man/distance.Rd	2009-01-27 14:45:56 UTC (rev 195)
@@ -1,33 +0,0 @@
-\name{distance}
-\alias{distanceEuclidean}
-\alias{distanceGreatcircle}
-\title{ Calculate geographic distance }
-\description{
-  Calculate geographic distance between two points on a sphere (distanceGreatcircle) or on a plane (distanceEuclidean).
-}
-\usage{
-distanceEuclidean(point1, point2)
-distanceGreatcircle(point1, point2, r = 6378137)
-}
-\arguments{
-  \item{point1}{  x and y coordinate of first (set of) point(s), either as c(x, y) or as matrix(ncol=2) in degrees for greatcircle, in meters (or similar) for euclidean }
-  \item{point2}{  x and y coordinate of second (set of) second point(s). Like point1 }
-  \item{r}{  radius of the world (modeled as a sphere), in meters }
-}
-\details{
-  A sphere is an approximation for the earth (a spheroid) and distanceGreatcircle can thus be used with geographic 
-  coordinates (latitude & longitude). There are probably better functions available, that use a datum, in other packages.
-  You can use distanceEuclidean for coordinates of a map projection such as UTM (but note
-  that in some projections distance is distorted). 
-}
-\value{
-  Distance in meters (great circle distance) or map-units (typically meters)
-}
-\author{Robert J. Hijmans \email{r.hijmans at gmail.com}}
-
-
-\examples{
-   distanceEuclidean(c(0, 0), c(1, 1))
-   distanceGreatcircle(c(0, 0), c(1, 1))
-}
-\keyword{ spatial }

Deleted: pkg/raster/man/distance2.Rd
===================================================================
--- pkg/raster/man/distance2.Rd	2009-01-27 13:10:16 UTC (rev 194)
+++ pkg/raster/man/distance2.Rd	2009-01-27 14:45:56 UTC (rev 195)
@@ -1,23 +0,0 @@
-\name{distance-methods}
-\docType{methods}
-\alias{distance-methods}
-\alias{distance,RasterLayer-method}
-\title{ Calculate distance}
-\description{
- ~~ Methods for function \code{distance}  ~~
-}
-\section{Methods}{
-\describe{
-
-\item{raster = "RasterLayer"}{ ~~describe this method here }
-}}
-\examples{
-r1 <- newRaster(ncol=36,nrow=18)
-data <- rep(NA, times=ncell(r1))
-data[345:355] <- 1
-r1 <- setValues(r1,data)
-distmap <- distance(r1)
-map(distmap)
-}
-\keyword{methods}
-\keyword{spatial}

Added: pkg/raster/man/pointdistance.Rd
===================================================================
--- pkg/raster/man/pointdistance.Rd	                        (rev 0)
+++ pkg/raster/man/pointdistance.Rd	2009-01-27 14:45:56 UTC (rev 195)
@@ -0,0 +1,33 @@
+\name{distance}
+\alias{distanceEuclidean}
+\alias{distanceGreatcircle}
+\title{ Calculate geographic distance }
+\description{
+  Calculate geographic distance between two points on a sphere (distanceGreatcircle) or on a plane (distanceEuclidean).
+}
+\usage{
+distanceEuclidean(point1, point2)
+distanceGreatcircle(point1, point2, r = 6378137)
+}
+\arguments{
+  \item{point1}{  x and y coordinate of first (set of) point(s), either as c(x, y) or as matrix(ncol=2) in degrees for greatcircle, in meters (or similar) for euclidean }
+  \item{point2}{  x and y coordinate of second (set of) second point(s). Like point1 }
+  \item{r}{  radius of the world (modeled as a sphere), in meters }
+}
+\details{
+  A sphere is an approximation for the earth (a spheroid) and distanceGreatcircle can thus be used with geographic 
+  coordinates (latitude & longitude). There are probably better functions available, that use a datum, in other packages.
+  You can use distanceEuclidean for coordinates of a map projection such as UTM (but note
+  that in some projections distance is distorted). 
+}
+\value{
+  Distance in meters (great circle distance) or map-units (typically meters)
+}
+\author{Robert J. Hijmans \email{r.hijmans at gmail.com}}
+
+
+\examples{
+   distanceEuclidean(c(0, 0), c(1, 1))
+   distanceGreatcircle(c(0, 0), c(1, 1))
+}
+\keyword{ spatial }

Added: pkg/raster/man/rasterdistance.Rd
===================================================================
--- pkg/raster/man/rasterdistance.Rd	                        (rev 0)
+++ pkg/raster/man/rasterdistance.Rd	2009-01-27 14:45:56 UTC (rev 195)
@@ -0,0 +1,22 @@
+\name{distance-methods}
+\docType{methods}
+\alias{distance-methods}
+\alias{distance}
+\alias{distance,RasterLayer-method}
+\title{ Calculate distance}
+\description{
+ ~~ Methods for function \code{distance}  ~~
+}
+\section{Methods}{
+\describe{
+
+\item{raster = "RasterLayer"}{ ~~describe this method here }
+}}
+\examples{
+r1 <- newRaster(ncol=36,nrow=18)
+r1[345:355] <- 1
+distmap <- distance(r1) 
+#plot(distmap)
+}
+\keyword{methods}
+\keyword{spatial}



More information about the Raster-commits mailing list