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):

  1. summary(bristol_ways)
  2. #> highway maxspeed ref geometry
  3. #> cycleway:1317 30 mph : 925 A38 : 214 LINESTRING :4915
  4. #> rail : 832 20 mph : 556 A432 : 146 epsg:4326 : 0
  5. #> road :2766 40 mph : 397 M5 : 144 +proj=long...: 0
  6. #> 70 mph : 328 A4018 : 124
  7. #> 50 mph : 158 A420 : 115
  8. #> (Other): 490 (Other):1877
  9. #> 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.

  1. ways_freeway = bristol_ways %>% filter(maxspeed == "70 mph")
  2. ways_sln = SpatialLinesNetwork(ways_freeway)
  3. slotNames(ways_sln)
  4. #> [1] "sl" "g" "nb" "weightfield"
  5. weightfield(ways_sln)
  6. #> [1] "length"
  7. class(ways_sln@g)
  8. #> [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.

  1. e = igraph::edge_betweenness(ways_sln@g)
  2. plot(ways_sln@sl$geometry, lwd = e / 500)

Illustration of a small route network, with segment thickness proportional to its betweenness, generated using the igraph package and described in the text.
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).