12.7 Route networks
The data used in this section was downloaded using osmdata.To avoid having to request the data from OSM repeatedly, we will use the bristol_ways
object, which contains point and line data for the case study area (see ?bristol_ways
):
summary(bristol_ways)
#> highway maxspeed ref geometry
#> cycleway:1317 30 mph : 925 A38 : 214 LINESTRING :4915
#> rail : 832 20 mph : 556 A432 : 146 epsg:4326 : 0
#> road :2766 40 mph : 397 M5 : 144 +proj=long...: 0
#> 70 mph : 328 A4018 : 124
#> 50 mph : 158 A420 : 115
#> (Other): 490 (Other):1877
#> NA's :2061 NA's :2295
The above code chunk loaded a simple feature object representing around 3,000 segments on the transport network.This is an easily manageable dataset size (transport datasets can be large, but it’s best to start small).
As mentioned, route networks can usefully be represented as mathematical graphs, with nodes on the network connected by edges.A number of R packages have been developed for dealing with such graphs, notably igraph.One can manually convert a route network into an igraph
object, but the geographic attributes will be lost.To overcome this issue SpatialLinesNetwork()
was developed in the stplanr package to represent route networks simultaneously as graphs and a set of geographic lines.This function is demonstrated below using a subset of the bristol_ways
object used in previous sections.
ways_freeway = bristol_ways %>% filter(maxspeed == "70 mph")
ways_sln = SpatialLinesNetwork(ways_freeway)
slotNames(ways_sln)
#> [1] "sl" "g" "nb" "weightfield"
weightfield(ways_sln)
#> [1] "length"
class(ways_sln@g)
#> [1] "igraph"
The output of the previous code chunk shows that ways_sln
is a composite object with various ‘slots’.These include: the spatial component of the network (named sl
), the graph component (g
) and the ‘weightfield’, the edge variable used for shortest path calculation (by default segment distance).ways_sln
is of class sfNetwork
, defined by the S4 class system.This means that each component can be accessed using the @
operator, which is used below to extract its graph component and process it using the igraph package, before plotting the results in geographic space.In the example below, the ‘edge betweenness’, meaning the number of shortest paths passing through each edge, is calculated (see ?igraph::betweenness
for further details and Figure 12.5).The results demonstrate that each graph edge represents a segment: the segments near the center of the road network have the greatest betweenness scores.
e = igraph::edge_betweenness(ways_sln@g)
plot(ways_sln@sl$geometry, lwd = e / 500)
Figure 12.5: Illustration of a small route network, with segment thickness proportional to its betweenness, generated using the igraph package and described in the text.
One can also find the shortest route between origins and destinations using this graph representation of the route network.This can be done with functions such as sum_network_routes()
from stplanr, which undertakes ‘local routing’ (see Section 12.5).