本文介绍了k最近的邻居**在一个球体上**的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用R在球面上找到k个近邻.

I am trying to find the k-nearest neighbor on a sphere using R.

当我处理百万分时,不能选择强行使用.

As I deal with million of points brute force is not an option.

有人知道我该怎么做吗?谢谢

Does anyone know how I could do? Thank you

推荐答案

使用Ben Bolker提供的链接,我设法解决了我的问题.

Using the link provided by Ben Bolker I manage to solve my pb.

lonlat2xyz=function (lon, lat, r)
{
lon = lon * pi/180
lat = lat * pi/180
if (missing(r))
    r <- 6378.1
x <- r * cos(lat) * cos(lon)
y <- r * cos(lat) * sin(lon)
z <- r * sin(lat)
return(cbind(x, y, z))
}

lon1=runif(100,-180,180);lon2=runif(100,-180,180);lat1=runif(100,-90,90);lat2=runif(100,-90,90)

xyz1=lonlat2xyz(lon1,lat1)
xyz2=lonlat2xyz(lon2,lat2)

library(nabor)

out=knn(data=xyz1,query = xyz2,k=20)

library(maps)

map()
points(lon1,lat1,pch=16,col="black")
points(lon2[1],lat2[1],pch=16,col="red")
points(lon1[out$nn.idx[1,]],lat1[out$nn.idx[1,]],pch=16,col="blue")

请注意,knn函数给出的距离不是地理距离!

Note the distances given by knn function are NOT the geographical distances !

这篇关于k最近的邻居**在一个球体上**的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 07:46