r - 从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 矩阵):
- 如果两个节点没有通过 Delaunay 三角剖分中的边链接:单元格的值 = 0
- 如果两个节点通过 Delaunay 三角剖分中的一条边链接:单元格的值 = 两个节点之间的地理距离(使用带有 DistVincenty 选项的“geosphere”包中的 distm())
解决方案
邻接矩阵在 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)
推荐阅读
- c++ - 如何在 Qt 中运行多个测试而不多次调用 QTest::qExec?
- python - 没有 openCV 的对象跟踪
- php - 图片上传错误“创建对象时发生类型错误”Magento 2.4.1
- python - ShareplumRequestError: Shareplum HTTP Post 失败: HTTPSConnectionPool(host='xxx', port=443):
- matlab - MATLAB极轴中的沿线颜色
- angular - TypeError:无法读取未定义的属性“elementRef”
- wso2mi - 在 WSO2 微集成器中替换 CAR 时出现重复的服务名称错误
- python - 错误 AttributeError: 'str' 对象在查询模型时没有属性 'year'
- bootstrap-4 - Bootstrap Carousel:变量不循环的指标
- devops - 有没有办法在 YouTrack 7 中一次删除多个问题?