查找从一个数据集到另一数据集的最接近点(纬度/经度) [英] Find closest points (lat / lon) from one data set to a second data set

查看:78
本文介绍了查找从一个数据集到另一数据集的最接近点(纬度/经度)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据集A和B,它们分别给出了英国不同点的位置:

  A = data.frame(参考= c(C,D,E),纬度= c(55.32043,55.59062,55.60859),经度= c(-2.3954998,-2.0650243,-2.0650542))B = data.frame(参考= c(C,D,E),纬度= c(55.15858,55.60859,55.59062),经度= c(-2.4252843,-2.0650542,-2.0650243)) 

A具有400行,而B具有1800行.对于A中的所有行,我想找到A中的一个点与B中三个最接近的点中的每一个之间的最短距离(以公里为单位),以及B中这些点的纬度和经度的参考点和坐标./p>

我尝试过这篇文章

R-查找给定半径内的最近邻点和邻居数,坐标为经纬度

但是,即使我遵循所有说明,主要是使用软件包 geosphere 中的命令 distm ,距离也无法以公里.我看不到代码中要更改的内容,特别是因为我对 geo 包一点都不熟悉.

解决方案

这里是使用单个循环并向量化距离计算(转换为km)的解决方案.
该代码使用基数R的 rank 函数对计算出的距离列表进行排序/排序.
索引和3个最短值的计算出的距离被存储回数据帧A中.

 图书馆(地球)A = data.frame(经度= c(-2.3954998,-2.0650243,-2.0650542),纬度= c(55.32043,55.59062,55.60859))B = data.frame(经度= c(-2.4252843,-2.0650542,-2.0650243),纬度= c(55.15858,55.60859,55.59062))for(i in 1:nrow(A)){#针对所有B的距离距离< -geosphere :: distGeo(A [i,],B)/1000#rank计算的距离排名< -rank(距离,ties.method =第一")#找到最短的3并将B的索引存储回AA $ shortest [i]< -which(ranking == 1)#与which.min()相同A $ shorter [i]<-(排名== 2)A $ short [i]<-(排名== 3)#将距离存储在A中A $ shortestD [i]-距离[A $ shortest [i]]#与min()相同A $ shorterD [i]<-距离[A $ shorter [i]]A $ shortD [i]-距离[A $ short [i]]}一种经度纬度最短更短最短D最短D最短D1 -2.395500 55.32043 1 3 2 18.11777 36.633310 38.289522 -2.065024 55.59062 3 2 1 0.00000 2.000682 53.246073 -2.065054 55.60859 2 3 1 0.00000 2.000682 55.05710 

正如M Viking所指出的那样,对于地理包裹数据包,数据必须先放置后再放置.

I have two data sets, A and B, which give locations of different points in the UK as such:

A = data.frame(reference = c(C, D, E), latitude = c(55.32043, 55.59062, 55.60859), longitude = c(-2.3954998, -2.0650243, -2.0650542))

B = data.frame(reference = c(C, D, E), latitude = c(55.15858, 55.60859, 55.59062), longitude = c(-2.4252843, -2.0650542, -2.0650243))

A has 400 rows and B has 1800 rows. For all the rows in A, I would like to find the shortest distance in kilometers between a point in A and each of the three closest points in B, as well as the reference and coordinates in lat and long of these points in B.

I tried using this post

R - Finding closest neighboring point and number of neighbors within a given radius, coordinates lat-long

However, even when I follow all the instructions, mainly using the command distm from the package geosphere, the distance comes up in a unit that can't possibly be kilometers. I don't see what to change in the code, especially since I am not familiar at all with the geo packages.

解决方案

Here is solution using a single loop and vectorizing the distance calculation (converted to km).
The code is using base R's rank function to order/sort the list of calculated distances.
The indexes and the calculated distances of the 3 shortest values are store back in data frame A.

library(geosphere)

A = data.frame(longitude = c(-2.3954998, -2.0650243, -2.0650542), latitude = c(55.32043, 55.59062, 55.60859))
B = data.frame(longitude = c(-2.4252843, -2.0650542, -2.0650243), latitude = c(55.15858, 55.60859, 55.59062))

for(i in 1:nrow(A)){
  #calucate distance against all of B
  distances<-geosphere::distGeo(A[i,], B)/1000
  #rank the calculated distances
  ranking<-rank(distances, ties.method = "first")

  #find the 3 shortest and store the indexes of B back in A
  A$shortest[i]<-which(ranking ==1) #Same as which.min()
  A$shorter[i]<-which(ranking==2)
  A$short[i]<-which(ranking ==3)

  #store the distances back in A
  A$shortestD[i]<-distances[A$shortest[i]] #Same as min()
  A$shorterD[i]<-distances[A$shorter[i]]
  A$shortD[i]<-distances[A$short[i]]
}
A

  longitude latitude shortest shorter short shortestD  shorterD   shortD
1 -2.395500 55.32043        1       3     2  18.11777 36.633310 38.28952
2 -2.065024 55.59062        3       2     1   0.00000  2.000682 53.24607
3 -2.065054 55.60859        2       3     1   0.00000  2.000682 55.05710

As M Viking pointed out, for the geosphere package the data must be arranged Lon then Lat.

这篇关于查找从一个数据集到另一数据集的最接近点(纬度/经度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆