[Raster-commits] r356 - pkg/raster/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Mar 14 08:10:17 CET 2009


Author: rhijmans
Date: 2009-03-14 08:10:17 +0100 (Sat, 14 Mar 2009)
New Revision: 356

Modified:
   pkg/raster/R/pointdistance.R
Log:


Modified: pkg/raster/R/pointdistance.R
===================================================================
--- pkg/raster/R/pointdistance.R	2009-03-14 06:41:32 UTC (rev 355)
+++ pkg/raster/R/pointdistance.R	2009-03-14 07:10:17 UTC (rev 356)
@@ -1,51 +1,40 @@
-# Author: Robert J. Hijmans, r.hijmans at gmail.com and Jacob van Etten
+# Author: Robert J. Hijmans  and Jacob van Etten
 # International Rice Research Institute
 # Date :  June 2008
-# Version 0,7
+# Version 0.8
 # Licence GPL v3
 
 
-
 pointDistance <- function (point1, point2, type='Euclidean', ...) {
-		
 	if (!(type %in% c('Euclidean', 'GreatCircle'))) {
 		stop('type should be Euclidean or GreatCircle')
 	}	
 	
-	if (class(point1) == 'SpatialPoints' | class(point1) == 'SpatialPointsDataFrame') {
-		point1 <- coordinates(point1)
-	}
-	if (class(point2) == 'SpatialPoints' | class(point2) == 'SpatialPointsDataFrame') {
-		point2 <- coordinates(point2)
-	}
+	for (i in 1:2) {
+		if (i == 1) { p <- point1 } else { p <- point2  }
+	
+		if (class(p) == 'SpatialPoints' | class(p) == 'SpatialPointsDataFrame') {
+			p <- coordinates(p)
+		}
 
-	if ( (!is.vector(point1) & !is.matrix(point1)) |  (!is.vector(point2) & !is.matrix(point2)) ) {
-		stop('points should be vectors of length 2, matrices with 2 columns, or a SpatialPoints* object')
-	}
-
-	if(is.vector(point1)){
-		if (length(point1) != 2) {
-			stop('point1. Wrong length for a vector, should be 2')
-		} else {
-			point1 <- matrix(point1, ncol=2) 
+		if  (!is.vector(p) & !is.matrix(p))  {
+			stop('points should be vectors of length 2, matrices with 2 columns, or a SpatialPoints* object')
 		}
-	} else {
-		if (length(point1[1,]) != 2) {
-			stop('point1. The matrix should have 2 columns')
-		}
-	}
 
-	if(is.vector(point2)){
-		if (length(point2) != 2) {
-			stop('point2. Wrong length for a vector, should be 2')
+		if(is.vector(p)){
+			if (length(p) != 2) {
+				stop(paste('point', i, '. Wrong length for a vector, should be 2', sep=""))
+			} else {
+				p <- matrix(p, ncol=2) 
+			}
 		} else {
-			point2 <- matrix(point2, ncol=2) 
+			if (length(p[1,]) != 2) {
+				stop(paste('point', i, '. The matrix should have 2 columns', sep=""))
+			}
 		}
-	} else {
-		if (length(point2[1,]) != 2) {
-			stop('point2. The matrix should have 2 columns')
-		}
+		if (i == 1) { point1 <- p } else { point2 <- p }
 	}
+	rm(p)
 	
 	if(length(point1[,1]) != length(point2[,1])) {
 		if(length(point1[,1]) > 1 & length(point2[,1]) > 1) {
@@ -61,7 +50,7 @@
 }
 
 
-.greatCircleDist <- function(x1, y1, x2, y2, r = 6378137) {
+.greatCircleDist <- function(x1, y1, x2, y2, r=6378137) {
 	#	cosd <- sin(y1) * sin(y2) + cos(y1) * cos(y2) * cos(x1-x2);
 	#	distance <- r * acos(cosd);
 	#  the following is supposedly more precise than above (http://en.wikipedia.org/wiki/Great_circle_distance):



More information about the Raster-commits mailing list