对于一个数据集中的每个点,计算到第二个数据集中最近点的距离

For each point in one data set, calculate distance to nearest point in second data set

尝试为 SpatialPointsDataFrame 中的每个点,在一秒内找到到最近点的距离 SpatialPointsDataFrame(相当于 ArcGIS 中的“最近”工具两个 SpatialPointDataFrames).

我可以通过使用 gDistance 计算所有成对距离并采用 min (like answer 1 here) 来实现简单的实现,但我有一些庞大的数据集,正在寻找更多的东西高效。

例如,这里有一个 trick with knearneigh for points in same dataset

交叉发布于 r-sig-geo

SearchTrees 包提供了一种解决方案。引用它的文档,"provides an implementation of the QuadTree data structure [which it] uses to implement fast k-Nearest Neighbor [...] lookups in two dimensions."

下面介绍了如何使用它快速查找 SpatialPoints 对象 b 中的每个点,在一秒钟内找到最近的两个点 SpatialPoints对象 B

library(sp)
library(SearchTrees)

## Example data
set.seed(1)
A <- SpatialPoints(cbind(x=rnorm(100), y=rnorm(100)))
B <- SpatialPoints(cbind(x=c(-1, 0, 1), y=c(1, 0, -1)))

## Find indices of the two nearest points in A to each of the points in B
tree <- createTree(coordinates(A))
inds <- knnLookup(tree, newdat=coordinates(B), k=2)

## Show that it worked
plot(A, pch=1, cex=1.2)
points(B, col=c("blue", "red", "green"), pch=17, cex=1.5)
## Plot two nearest neigbors
points(A[inds[1,],], pch=16, col=adjustcolor("blue", alpha=0.7))
points(A[inds[2,],], pch=16, col=adjustcolor("red", alpha=0.7))
points(A[inds[3,],], pch=16, col=adjustcolor("green", alpha=0.7))

R-Sig-Geo 的另一个建议是 nabor 库中的 knn 函数。