首页 > 解决方案 > 从R中的Delaunay三角剖分生成邻接矩阵

问题描述

我有一个带有坐标列表(纬度,经度)的数据框,如下所示:

point lat long
1  51 31
2  52 31
3  52 30
4  56 28
5  57 29
6  53 32
7  54 35
8  52 32
9  48 30
10 49 27

我已经设法使用以下代码生成了 Delaunay 三角剖分:

library(deldir)
vtess <- deldir(df$lat, df$long)
plot(vtess, wlines="triang", wpoints="none", number=FALSE, add=TRUE, lty=1)

我现在想做的是生成一个具有以下单元格值的邻接矩阵(10 x 10 矩阵):

  1. 如果两个节点没有通过 Delaunay 三角剖分中的边链接:单元格的值 = 0
  2. 如果两个节点通过 Delaunay 三角剖分中的一条边链接:单元格的值 = 两个节点之间的地理距离(使用带有 DistVincenty 选项的“geosphere”包中的 distm())

标签: rgraphgeo

解决方案


邻接矩阵在 Delaunay 三角剖分的输出中基本上是可用的,它只需要重新格式化。我们避免使用该distm函数,因为我们不想计算所有点对之间的距离,只计算相邻点对之间的距离。直接调用距离函数更有效。

library(deldir)
library(geosphere)

del = deldir(dd$lat, dd$long)
del$delsgs$dist = with(del$delsgs, 
    distVincentySphere(p1 = cbind(y1, x1), p2 = cbind(y2, x2))
)
# we use y,x because the triangulation was lat,long but 
# distVincentySphere expects long,lat

# create empty adjacency matrix, fill in distances
adj = matrix(0, nrow = nrow(dd), ncol = nrow(dd))
adj[as.matrix(del$delsgs[c("ind1", "ind2")])] = del$delsgs$dist
round(adj)
#        [,1]  [,2]   [,3]   [,4]   [,5]   [,6]   [,7] [,8]   [,9]  [,10]
#  [1,]      0     0 131124      0      0      0      0    0 341685      0
#  [2,] 111319     0  68535      0      0 130321      0    0      0      0
#  [3,]      0     0      0      0      0      0      0    0      0      0
#  [4,]      0     0 464058      0      0      0      0    0      0 782155
#  [5,]      0     0      0 127147      0      0      0    0      0      0
#  [6,]      0     0 175378 422215 484616      0      0    0      0      0
#  [7,]      0     0      0      0 504301 227684      0    0 753748      0
#  [8,] 131124 68535      0      0      0 111319 299883    0 467662      0
#  [9,]      0     0 445278      0      0      0      0    0      0      0
# [10,]      0     0 395715      0      0      0      0    0 247685      0

使用这些数据:

dd = read.table(text = "point lat long
1  51 31
2  52 31
3  52 30
4  56 28
5  57 29
6  53 32
7  54 35
8  52 32
9  48 30
10 49 27", header = T)

推荐阅读