Skip to content

Commit

Permalink
knearneigh problem of k less than count of identical points
Browse files Browse the repository at this point in the history
  • Loading branch information
rsbivand committed Apr 24, 2024
1 parent 22f6f5f commit a9e435b
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Version 1.3-4 (development)

* Introduce error in `knearneigh` for `k` less than the largest count of identical points; if encountered, increase `k`

* remove spurious warning in `knearneigh` for longlat geometries

* fix https://github.com/edzer/sdsr/issues/121, wrong assignment of old test names in `lmRStests`
Expand Down
2 changes: 2 additions & 0 deletions R/knearneigh.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ knearneigh <- function(x, k=1, longlat=NULL, use_kd_tree=TRUE)
if (use_s2_ll) {
z <- s2::s2_closest_edges(s2x, s2x, k=(k+1))
z <- lapply(z, sort)
if (any(!(sapply(seq_along(z), function(i) i %in% z[[i]]))))
stop("increase k; k must be at least as large as the largest count of identical points")
z <- lapply(seq_along(z), function(i) setdiff(z[[i]], i))
res <- list(nn=do.call("rbind", z), np=np, k=k,
dimension=dimension, x=x)
Expand Down
2 changes: 1 addition & 1 deletion man/knearneigh.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ knearneigh(x, k=1, longlat = NULL, use_kd_tree=TRUE)

\arguments{
\item{x}{matrix of point coordinates, an object inheriting from SpatialPoints or an \code{"sf"} or \code{"sfc"} object; if the \code{"sf"} or \code{"sfc"} object geometries are in geographical coordinates (\code{sf::st_is_longlat(x) == TRUE} and \code{sf::sf_use_s2() == TRUE}), \pkg{s2} will be used to find the neighbours because it uses spatial indexing \url{https://github.com/r-spatial/s2/issues/125} as opposed to the legacy method which uses brute-force}
\item{k}{number of nearest neighbours to be returned}
\item{k}{number of nearest neighbours to be returned; where identical points are present, \code{k} should be at least as large as the largest count of identical points (if \code{k} is smaller, an error will occur when \code{s2} is used)}
\item{longlat}{TRUE if point coordinates are longitude-latitude decimal degrees, in which case distances are measured in kilometers; if x is a SpatialPoints object, the value is taken from the object itself; longlat will override \code{kd_tree}}
\item{use_kd_tree}{logical value, if the \pkg{dbscan} package is available, use for finding k nearest neighbours when longlat is FALSE, and when there are no identical points; from \url{https://github.com/r-spatial/spdep/issues/38}, the input data may have more than two columns if \pkg{dbscan} is used}
}
Expand Down

0 comments on commit a9e435b

Please sign in to comment.