首页 > 解决方案 > 从 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"))

标签: rgeospatialrastersp

解决方案


使用如何在 R 中接收相交空间线的差异?

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

阴谋


推荐阅读