Skip to content

Commit

Permalink
d
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed Dec 17, 2024
1 parent c8ba315 commit 02a9fd8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
22 changes: 15 additions & 7 deletions R/relate.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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 {
Expand All @@ -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]))
Expand All @@ -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)
}
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions man/nearby.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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 }
}
Expand Down

0 comments on commit 02a9fd8

Please sign in to comment.