Return 来自 SpatialLinesDataFrame 的交叉点位置
Return Intersection Locations from 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
我正在分析道路网络文件,并试图获取代表所有交叉路口的坐标(或 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