r - 从 SpatialLinesDataFrame 返回交叉点位置
问题描述
我正在分析一个道路网络文件,并试图获取代表所有交叉点的坐标(或 spdf)。我查看了 sp、rgeos 和栅格,但似乎找不到一个合适的解决方案,它只需要1 个对象并分析其几何形状的交叉点。
目标是找到所有类型的交叉点:
是否有专门用于道路网络分析的软件包可以做到这一点?(如果您知道可以实现这一点及更多的东西(弯曲度计算、长度等),我会全神贯注。
简单的空间线数据框:
library(sp)
library(rgeos)
## Roughly taken from the sp vignette:
l1 <- cbind(c(-79.81022, -79.80993), c(43.24589, 43.24654))
l2 <- cbind(c(-79.81022, -79.80993), c(43.24654, 43.24589))
l3 <- cbind(c(-79.81022, -79.80990), c(43.24589, 43.24589))
Sl1 <- Line(l1)
Sl2 <- Line(l2)
Sl3 <- Line(l3)
S1 <- Lines(list(Sl1), ID = "a")
S2 <- Lines(list(Sl2), ID = "b")
S3 <- Lines(list(Sl3), ID = "c")
Sl <- SpatialLines(list(S1, S2, S3))
## sample data: line lengths
df <- data.frame(len = sapply(1:length(Sl), function(i) gLength(Sl[i, ])))
rownames(df) <- sapply(1:length(Sl), function(i) Sl@lines[[i]]@ID)
## SpatialLines to SpatialLinesDataFrame
sampleLines <- SpatialLinesDataFrame(Sl, data = df)
plot(sampleLines, col = c("red", "blue", "green"))
解决方案
intersections <- gIntersects(Sl, byid = TRUE)
intersections[lower.tri(intersections, diag = TRUE)] <- NA
intersections <- reshape2::melt(intersections, na.rm = TRUE)
t(apply(intersections, 1,
function(x) coordinates(gIntersection(Sl[x[1]], Sl[x[2]]))))
# [,1] [,2]
# 4 -79.810075 43.246215
# 7 -79.810220 43.245890
# 8 -79.809930 43.245890
推荐阅读
- java - 以编程方式为 JFileChooser 设置文件过滤器
- javascript - 初级 JS 练习:Change Maker
- c# - 使用转义单引号调用函数
- postgresql - 新表的 Postgres Where 语句
- javascript - 如何在 Netbeans 中使用 JSP 标签维护 javascirpt 的代码格式
- azure-active-directory - 将 AD 与 IBM Tivoli 访问管理器同步的最佳方式
- c++ - C++ 无处引用
- appium - 如何更改 Appium Desktop 的 appium 版本
- javascript - browser.getTitle() is returning TypeError: Converting circular structure to JSON - Protractor project
- node.js - 模式拼接两个远程 Prisma/GraphQL 模式