diff --git a/R/relate.R b/R/relate.R index 8849c8397..0b83e7ac4 100644 --- a/R/relate.R +++ b/R/relate.R @@ -273,13 +273,17 @@ setMethod("adjacent", signature(x="SpatVector"), setMethod("nearby", signature(x="SpatVector"), - function(x, y=NULL, distance=0, k=1, centroids=TRUE, symmetrical=TRUE, haversine=FALSE) { + function(x, y=NULL, distance=0, k=1, centroids=TRUE, symmetrical=TRUE, method="geo") { k <- round(k) if (distance <= 0 && k < 1) { error("nearby", "either distance or k must be a positive number") } + if (!(method %in% c("geo", "haversine", "cosine"))) { + error("nearby", "not a valid method. Should be one of: 'geo', 'haversine', 'cosine'") + } + if ((geomtype(x) == "polygons") && centroids) { x <- centroids(x) } @@ -291,10 +295,10 @@ setMethod("nearby", signature(x="SpatVector"), } if (distance > 0) { if (hasy) { - d <- distance(x, y, haversine=haversine) + d <- distance(x, y, method=method) d <- cbind(from_id=rep(1:nrow(d), ncol(d)), to_id=rep(1:ncol(d), each=nrow(d)), distance=as.vector(d)) } else { - d <- distance(x, pairs=TRUE, symmetrical=symmetrical, haversine=haversine) + d <- distance(x, pairs=TRUE, symmetrical=symmetrical, method=method) } d[d[,3] <= distance, 1:2, drop=FALSE] } else { @@ -307,7 +311,7 @@ setMethod("nearby", signature(x="SpatVector"), if (hasy) { d <- distance(x, y) } else { - d <- as.matrix(distance(x, pairs=FALSE)) + d <- as.matrix(distance(x, pairs=FALSE, method=method)) diag(d) <- NA } d <- t(apply(d, 1, function(i) order(i)[1:k])) @@ -326,7 +330,7 @@ setMethod("nearby", signature(x="SpatVector"), setMethod("nearest", signature(x="SpatVector"), - function(x, y=NULL, pairs=FALSE, centroids=TRUE, lines=FALSE, haversine=FALSE) { + function(x, y=NULL, pairs=FALSE, centroids=TRUE, lines=FALSE, method="geo") { if ((geomtype(x) == "polygons") && centroids) { x <- centroids(x) } @@ -338,10 +342,14 @@ setMethod("nearest", signature(x="SpatVector"), y <- centroids(y) } z <- x + if (!(method %in% c("geo", "haversine", "cosine"))) { + error("nearest", "not a valid method. Should be one of: 'geo', 'haversine', 'cosine'") + } + if (within) { - z@pntr <- x@pntr$near_within() + z@pntr <- x@pntr$near_within(method) } else { - z@pntr <- x@pntr$near_between(y@pntr, pairs, havesine) + z@pntr <- x@pntr$near_between(y@pntr, pairs, method) } z <- messages(z, "nearest") if (geomtype(z) == "points") { #lonlat points diff --git a/man/nearby.Rd b/man/nearby.Rd index 101f6c6de..448cc5b3e 100644 --- a/man/nearby.Rd +++ b/man/nearby.Rd @@ -14,9 +14,9 @@ Identify geometries that are near to each other. Either get the index of all geo } \usage{ -\S4method{nearby}{SpatVector}(x, y=NULL, distance=0, k=1, centroids=TRUE, symmetrical=TRUE, bool haversine) +\S4method{nearby}{SpatVector}(x, y=NULL, distance=0, k=1, centroids=TRUE, symmetrical=TRUE, method="geo") -\S4method{nearest}{SpatVector}(x, y, pairs=FALSE, centroids=TRUE, lines=FALSE, bool haversine) +\S4method{nearest}{SpatVector}(x, y, pairs=FALSE, centroids=TRUE, lines=FALSE, method="geo") } \arguments{ @@ -26,7 +26,7 @@ Identify geometries that are near to each other. Either get the index of all geo \item{k}{positive integer. number of neighbors. Ignored if \code{distance > 0}} \item{centroids}{logical. Should the centroids of polygons be used?} \item{symmetrical}{logical. If \code{TRUE}, a near pair is only included once. That is, if geometry 1 is near to geometry 3, the implied nearness between 3 and 1 is not reported. Ignored if \code{k} neighbors are returned} - \item{haversine}{logical. Use the haversine formula for lon/lat data? If \code{FALSE}, the more precise but slower method of Karney (2003) is used} + \item{method}{character. One of "geo", "haversine", "cosine". With "geo" the most precise but slower method of Karney (2003) is used. The other two methods are faster but less precise} \item{pairs}{logical. If \code{TRUE} pairwise nearest points are returned (only relevant when using at least one SpatVector of lines or polygons} \item{lines}{logical. If \code{TRUE} lines between the nearest points instead of (the nearest) points } }