Problem: I want to extract a route along the rail network between two stations from a shapefile and only plot this particular route, rather than the entire network.
This is what I have so far:
I have a shapefile with the entire rail network of the UK, plotted it looks like this:
library(maptools)
rail <- readShapeSpatial("railnetworkLine.shp")
I also have a list of stations with Eastings and Northings, for example:
1) ABDARE 300400 202800
2) DEIGHTN 416490 419140
I can add them to the map and it looks like this:
plot(rail)
plot(spdf.station, add=TRUE, col="red", pch=20)
So what I don't know, is how I can extract the route between them and just plot that route - the information is obviously in the shapefile and I have the coordinates of the station, but I don't understand how to extract it.
I managed to calculate the distance between them with this code:
SpacingInMetres <- 10000
require(secrlinear)
network <- read.linearmask(data=rail, spacing=SpacingInMetres)
distance <- (networkdistance (stations[1,], stations[2,], network))/1000
# Confirm distance:
distance
>311.7893
And I found that you can get the routes along roads with Google Maps with ggmaps
(see here). But how can you do it when you have a shapefile as the network input rather than Google Maps?
I think maybe the packages 'shp2graph' + 'igraph' are useful, but I just can't figure it out. Any thoughts?