I am analyzing a road network file, and am trying to get coordinates (or spdf) that represents all the intersections. I have looked through sp, rgeos, & raster, but can't seem to find an appropriate solution that will take just 1 object and analyze its geometry for intersections.
The goal is to find all types of intersections:
Is there a package specifically for road network analysis that will do this? (If you know of something that will achieve this & more (sinuosity calculations, length, etc.), I'm all ears.
Simple spatialLinesDataframe:
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"))
Using the approach from How to receive differences of intersecting SpatialLines in 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