Get shortest/fastest path between two points from SpatialLines(DataFrame)

by Squeezie   Last Updated August 14, 2019 08:22 AM

I have a large rivernetwork as a SpatialLines(DataFrame) and want to subset it so I get a SpatialLine with only the shortest/fastest path between two SpatialPoints(DataFrame) which propably need to be snapped onto the line first.

In the example Code, I want to get the closest line between the red dots, while moving on the lines. Also I want to move between the left red point and the right blue point, which needs to be snapped to the closest SpatialLine

library(sp)    
x <- c(1,5,6,8)
y1 <- c(1,3,4,7)
y2 <- c(5,5,5,2)

L <- SpatialLines(list(Lines(Line(cbind(x,y1)), ID="a"),Lines(Line(cbind(x,y2)), ID="b")))
P <- SpatialPoints(data.frame(x=c(1,8),y=c(1,2)))
P_snap <- SpatialPoints(data.frame(x=c(8),y=c(1)))

plot(L)
points(P,col="red")
points(P_snap,col="blue")
Tags : gdal r line sp


Related Questions


Updated September 05, 2017 21:22 PM

Updated February 02, 2018 18:22 PM

Updated June 17, 2015 03:09 AM

Updated July 17, 2015 13:09 PM

Updated May 30, 2017 16:22 PM