纬度/经度点(坐标)的2个列表之间的地理/地理空间距离

gra*_*mar 35 r distance latitude-longitude

我有2个列表(list1,list2),其中包含各种位置的纬度/经度.一个list(list2)具有没有的位置名称list1.

我想要list1中每个点的近似位置.所以我想指出一点list1,试着寻找最近的点,list2然后选择那个地方.我重申每一点list1.它还需要距离(以米为单位)和点的索引(in list1),因此我可以围绕它构建一些业务规则 - 基本上这些是应该添加到list1(near_dist,indx)的2个新cols .

我正在使用该gdist功能,但我无法使用它来处理数据帧输入.

示例输入列表:

list1 <- data.frame(longitude = c(80.15998, 72.89125, 77.65032, 77.60599, 
                                  72.88120, 76.65460, 72.88232, 77.49186, 
                                  72.82228, 72.88871), 
                    latitude = c(12.90524, 19.08120, 12.97238, 12.90927, 
                                 19.08225, 12.81447, 19.08241, 13.00984,
                                 18.99347, 19.07990))
list2 <- data.frame(longitude = c(72.89537, 77.65094, 73.95325, 72.96746, 
                                  77.65058, 77.66715, 77.64214, 77.58415,
                                  77.76180, 76.65460), 
                    latitude = c(19.07726, 13.03902, 18.50330, 19.16764, 
                                 12.90871, 13.01693, 13.00954, 12.92079,
                                 13.02212, 12.81447), 
                    locality = c("A", "A", "B", "B", "C", "C", "C", "D", "D", "E"))
Run Code Online (Sandbox Code Playgroud)

Jaa*_*aap 53

要使用纬度/经度坐标计算两个点之间的地理距离,您可以使用多个公式.该封装geosphere具有distCosine,distHaversine,distVincentySpheredistVincentyEllipsoid用于计算的距离.其中,distVincentyEllipsoid被认为是最准确的,但计算上比其他更密集.

使用这些功能之一,您可以创建距离矩阵.基于该矩阵,您可以locality根据最短距离which.min和相应距离分配名称min(参见答案的最后部分),如下所示:

library(geosphere)

# create distance matrix
mat <- distm(list1[,c('longitude','latitude')], list2[,c('longitude','latitude')], fun=distVincentyEllipsoid)

# assign the name to the point in list1 based on shortest distance in the matrix
list1$locality <- list2$locality[max.col(-mat)]
Run Code Online (Sandbox Code Playgroud)

这给了:

> list1
   longitude latitude locality
1   80.15998 12.90524        D
2   72.89125 19.08120        A
3   77.65032 12.97238        C
4   77.60599 12.90927        D
5   72.88120 19.08225        A
6   76.65460 12.81447        E
7   72.88232 19.08241        A
8   77.49186 13.00984        D
9   72.82228 18.99347        A
10  72.88871 19.07990        A
Run Code Online (Sandbox Code Playgroud)

另一种可能性是locality根据localitys 的平均经度和纬度值分配list2:

library(dplyr)
list2a <- list2 %>% group_by(locality) %>% summarise_each(funs(mean)) %>% ungroup()
mat2 <- distm(list1[,c('longitude','latitude')], list2a[,c('longitude','latitude')], fun=distVincentyEllipsoid)
list1 <- list1 %>% mutate(locality2 = list2a$locality[max.col(-mat2)])
Run Code Online (Sandbox Code Playgroud)

或者data.table:

library(data.table)
list2a <- setDT(list2)[,lapply(.SD, mean), by=locality]
mat2 <- distm(setDT(list1)[,.(longitude,latitude)], list2a[,.(longitude,latitude)], fun=distVincentyEllipsoid)
list1[, locality2 := list2a$locality[max.col(-mat2)] ]
Run Code Online (Sandbox Code Playgroud)

这给了:

> list1
   longitude latitude locality locality2
1   80.15998 12.90524        D         D
2   72.89125 19.08120        A         B
3   77.65032 12.97238        C         C
4   77.60599 12.90927        D         C
5   72.88120 19.08225        A         B
6   76.65460 12.81447        E         E
7   72.88232 19.08241        A         B
8   77.49186 13.00984        D         C
9   72.82228 18.99347        A         B
10  72.88871 19.07990        A         B
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,这导致大多数(10个中的7个)场合被分配给另一个locality.


您可以添加距离:

list1$near_dist <- apply(mat2, 1, min)
Run Code Online (Sandbox Code Playgroud)

或其他方法max.col(很可能更快):

list1$near_dist <- mat2[matrix(c(1:10, max.col(-mat2)), ncol = 2)]

# or using dplyr
list1 <- list1 %>% mutate(near_dist = mat2[matrix(c(1:10, max.col(-mat2)), ncol = 2)])
# or using data.table (if not already a data.table, convert it with 'setDT(list1)' )
list1[, near_dist := mat2[matrix(c(1:10, max.col(-mat2)), ncol = 2)] ]
Run Code Online (Sandbox Code Playgroud)

结果:

> list1
    longitude latitude locality locality2   near_dist
 1:  80.15998 12.90524        D         D 269966.8970
 2:  72.89125 19.08120        A         B  65820.2047
 3:  77.65032 12.97238        C         C    739.1885
 4:  77.60599 12.90927        D         C   9209.8165
 5:  72.88120 19.08225        A         B  66832.7223
 6:  76.65460 12.81447        E         E      0.0000
 7:  72.88232 19.08241        A         B  66732.3127
 8:  77.49186 13.00984        D         C  17855.3083
 9:  72.82228 18.99347        A         B  69456.3382
10:  72.88871 19.07990        A         B  66004.9900
Run Code Online (Sandbox Code Playgroud)


小智 7

感谢 Martin Haringa 提供的这个解决方案,当您需要通过遍历Mark Needham 博客上的数据框来执行此功能时,可以更轻松地实现此方法

library(dplyr)
library(geosphere)

df %>%
  rowwise() %>%
  mutate(newcolumn_distance = distHaversine(c(df$long1, df$lat1), 
                                            c(df$long2, df$lat2)))
Run Code Online (Sandbox Code Playgroud)

我在现实世界数据集中的大样本上分别使用 distm 和 distHaversine 这两个函数进行了测试,并且 distHaversine 似乎比 distm 函数快得多。我很惊讶,因为我认为这两者只是两种格式的相同功能。

  • @AlexanderKielland 谢谢。更简单的是使用 hasrsine 函数的矢量化版本,例如: df %&gt;% mutate(newcolumn_distance = Spatialrisk::haversine(lat1, long1, lat2, long2)) (2认同)