Learn R Programming

⚠️There's a newer version (0.4.3) of this package.Take me there.

dodgr: Distances on Directed Graphs in R

dodgr is an R package for efficient calculation of many-to-many pairwise distances on dual-weighted directed graphs, for aggregation of flows throughout networks, and for highly realistic routing through street networks (time-based routing considering incline, turn-angles, surface quality, everything).

What’s so special?

Four aspects. First, while other packages exist for calculating distances on directed graphs, notably igraph, even that otherwise fabulous package does not (readily) permit analysis of dual-weighted graphs. Dual-weighted graphs have two sets of weights for each edge, so routing can be evaluated with one set of weights, while distances can be calculated with the other. A canonical example is a street network, where weighted distances are assigned depending on mode of transport (for example, weighted distances for pedestrians on multi-lane vehicular roads are longer than equivalent distances along isolated walking paths), yet the desired output remains direct, unweighted distances. Accurate calculation of distances on street networks requires a dual-weighted representation. In R, dodgr is currently the only package that offers this functionality (without excessive data wrangling).

Second, while igraph and almost all other routing packages are primarily designed for one-to-one routing, dodgr is specifically designed for many-to-many routing, and will generally outperform equivalent packages in large routing tasks.

Third, dodgr goes beyond the functionality of comparable packages through including routines to aggregate flows throughout a network, through specifying origins, destinations, and flow densities between each pair of points. Alternatively, flows can be aggregated according to a network dispersal model from a set of origin points and associated densities, and a user-specified dispersal model.

Fourth and finally, dodgr implements highly realistic and fully-customisable profiles for routing through street networks with various modes of transport, and using either distance- or time-based routing. Routing can include such factors as waiting times at traffic lights, delays for turning across oncoming traffic, and the effects of elevation on both cyclists and pedestrians.

Installation

You can install dodgr with:

install.packages("dodgr") # current CRAN version
# install.packages("remotes")
remotes::install_github("ATFutures/dodgr") # Development version

Then load with

library (dodgr)
packageVersion ("dodgr")
#> [1] '0.2.4'

Usage: Sample Data and dodgr networks

To illustrate functionality, the package includes an example data set containing the Open Street Map network for Hampi, India (a primarily pedestrian village in the middle of a large World Heritage zone). These data are in Simple Features (sf) format, as a collection of LINESTRING objects. dodgr represents networks as a simple rectangular graph, with each row representing an edge segment between two points or vertices. sf-format objects can be converted to equivalent dodgr representations with the weight_streetnet() function:

class (hampi)
#> [1] "sf"         "data.frame"
dim (hampi)
#> [1] 203  15
graph <- weight_streetnet (hampi, wt_profile = "foot")
class (graph)
#> [1] "data.frame"      "dodgr_streetnet"
dim (graph)
#> [1] 5973   15

The sf-format network contained 203 LINESTRING objects, with the weight_streetnet() function decomposing these into 5,973 distinct edges, indicating that the sf representation had around 29 edges or segments in each LINESTRING object. The dodgr network then looks like this:

head (graph)
geom_numedge_idfrom_idfrom_lonfrom_latto_idto_lonto_latdd_weightedhighwayway_idcomponenttimetime_weighted
1133931850076.4748915.3416933931850276.4761215.34173132.442169165.55271unclassified28565950195.358362119.197952
1233931850276.4761215.3417333931850076.4748915.34169132.442169165.55271unclassified28565950195.358362119.197952
1333931850276.4761215.34173239895802876.4762115.341748.88867011.11084unclassified2856595016.3998437.999803
14239895802876.4762115.3417433931850276.4761215.341738.88867011.11084unclassified2856595016.3998437.999803
15239895802876.4762115.34174142711607776.4762815.341799.32653611.65817unclassified2856595016.7151068.393882
16142711607776.4762815.34179239895802876.4762115.341749.32653611.65817unclassified2856595016.7151068.393882

The geom_num column maps directly onto the sequence of LINESTRING objects within the sf-formatted data. The highway column is taken directly from Open Street Map, and denotes the kind of “highway” represented by each edge. The component column is an integer value describing which of the connected components of the network each edge belongs to (with 1 always being the largest component; 2 the second largest; and so on).

Note that the d_weighted values are often greater than the geometric distances, d. In the example shown, service highways are not ideal for pedestrians, and so weighted distances are slightly greater than actual distances. Compare this with:

head (graph [graph$highway == "path", ])
geom_numedge_idfrom_idfrom_lonfrom_latto_idto_lonto_latdd_weightedhighwayway_idcomponenttimetime_weighted
4724733890522076.4739815.3122433890754376.4740515.3124119.7039919.70399path30643853135.4671835.46718
4824833890754376.4740515.3124133890522076.4739815.3122419.7039919.70399path30643853135.4671835.46718
4924933890754376.4740515.31241239895758576.4741015.3125921.3917221.39172path30643853138.5051038.50510
50250239895758576.4741015.3125933890754376.4740515.3124121.3917221.39172path30643853138.5051038.50510
51251239895758576.4741015.3125933890759776.4741315.3127922.1520522.15205path30643853139.8737039.87370
5225233890759776.4741315.31279239895758576.4741015.3125922.1520522.15205path30643853139.8737039.87370

A "path" offers ideal walking conditions, and so weighted distances are equal to actual distances.

Usage: Distances and Times

The many-to-many nature of dodgr means that the function to calculate distances, dodgr_distances() or, for street networks, times, dodgr_times(), accepts two vectors or matrices of routing points as inputs (describing origins and destinations), and returns a corresponding matrix of pairwise distances. If an input graph has columns for both distances and weighted distances, and/or times and weighted times, the weighted versions are used to determine the effectively shortest or fastest routes through a network, while actual distances or times are summed along the routes to calculate final values. It is of course also possible to calculate distances along fastest routes, times along shortest routes, or any combination thereof, as detailed in the package vignette on street networks and time-based routing.

Routing points can, for example, be randomly selected from the vertices of a graph. The vertices can in turn be extracted with the dodgr_vertices() function:

v <- dodgr_vertices (graph)
head (v)
idxycomponentn
133931850076.4748915.3416910
233931850276.4761215.3417311
4239895802876.4762115.3417412
6142711607776.4762815.3417913
833931850376.4764115.3419014
10239895803476.4765015.3419915

For OSM data extracted with the osmdata package (or, equivalently, via the dodgr::dodgr_streetnet() function), each object (vertices, ways, and high-level relations between these objects) is assigned a unique identifying number. These are retained both in osmdata and dodgr, as the way_id column in the above graph, and as the id column in the vertices. Random vertices may be generated in this case through selecting id values:

from <- sample (v$id, size = 20)
to <- sample (v$id, size = 50)
d <- dodgr_dists (graph = graph, from = from, to = to)
dim (d)
#> [1] 20 50

Alternatively, the points may be specified as matrices of geographic coordinates:

from_x <- min (graph$from_lon) + runif (20) * diff (range (graph$from_lon))
from_y <- min (graph$from_lat) + runif (20) * diff (range (graph$from_lat))
to_x <- min (graph$from_lon) + runif (50) * diff (range (graph$from_lon))
to_y <- min (graph$from_lat) + runif (50) * diff (range (graph$from_lat))
d <- dodgr_dists (graph = graph, from = cbind (from_x, from_y), to = cbind (to_x, to_y))

In this case, the random points will be mapped on to the nearest points on the street network. This may, of course, map some points onto minor, disconnected components of the graph. This can be controlled either by reducing the graph to it’s largest connected component only:

graph <- graph [graph$component == 1, ]
nrow (graph)

or by explicitly using the match_points_to_graph() function with the option connected = TRUE:

from <- match_points_to_graph (v, cbind (from_x, from_y), connected = TRUE)
to <- match_points_to_graph (v, cbind (to_x, to_y), connected = TRUE)

This function returns an index into the result of dodgr_vertices, and so points to use for routing must then be extracted as follows:

from <- v$id [from] # or from <- v [from, c ("x", "y")]
to <- v$id [to]
d <- dodgr_dists (graph = graph, from = from, to = to)

Usage: Flow Aggregation

Flow aggregation refers to the procedure of routing along multiple ways according to specified densities of flow between defined origin and destination points, and aggregating flows along each edge of the network. The procedure is functionally similar to the above procedure for distances, with the addition of a matrix specifying pairwise flow densities between the input set of origin (from) and destination (to) points. The following example illustrates use with a random “flow matrix”:

flows <- array (runif (length (from) * length (to)), dim = c (length (from), length (to)))
length (from); length (to); dim (flows)
#> [1] 20
#> [1] 50
#> [1] 20 50
f <- dodgr_flows_aggregate (graph = graph, from = from, to = to, flows = flows)

The result is simply the input graph with an additional column quantifying the aggregate flows along each edge:

head (f)
geom_numedge_idfrom_idfrom_lonfrom_latto_idto_lonto_latdd_weightedhighwayway_idcomponenttimetime_weightedflow
1133931850076.4748915.3416933931850276.4761215.34173132.442169165.55271unclassified28565950195.358362119.1979520
1233931850276.4761215.3417333931850076.4748915.34169132.442169165.55271unclassified28565950195.358362119.1979520
1333931850276.4761215.34173239895802876.4762115.341748.88867011.11084unclassified2856595016.3998437.9998030
14239895802876.4762115.3417433931850276.4761215.341738.88867011.11084unclassified2856595016.3998437.9998030
15239895802876.4762115.34174142711607776.4762815.341799.32653611.65817unclassified2856595016.7151068.3938820
16142711607776.4762815.34179239895802876.4762115.341749.32653611.65817unclassified2856595016.7151068.3938820

An additional flow aggregation function can be applied in cases where only densities at origin points are known, and movement throughout a graph is dispersive:

f <- dodgr_flows_disperse (graph = graph, from = from, dens = runif (length (from)))

Further detail

For more detail, see the main package vignette, the second vignette on street networks and time-based routing, and a third vignette detailing benchmark timings, showing that under many circumstances, dodgr performs considerably faster than equivalent routines from the igraph package.

Copy Link

Version

Install

install.packages('dodgr')

Monthly Downloads

672

Version

0.2.5

License

GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Mark Padgham

Last Published

October 10th, 2019

Functions in dodgr (0.2.5)

dodgr_full_cycles

dodgr_full_cycles
dodgr_flowmap

dodgr_flowmap
dodgr_to_tidygraph

dodgr_to_tidygraph
weighting_profiles

weighting_profiles
dodgr_fundamental_cycles

dodgr_fundamental_cycles
dodgr_to_sf

dodgr_to_sf
dodgr_flows_aggregate

dodgr_flows_aggregate
dodgr_flows_disperse

dodgr_flows_disperse
weight_streetnet

weight_streetnet
dodgr_uncontract_graph

dodgr_uncontract_graph
dodgr_to_sfc

dodgr_to_sfc
os_roads_bristol

os_roads_bristol
dodgr_isochrones

dodgr_isochrones
dodgr_sflines_to_poly

dodgr_sflines_to_poly
dodgr_sample

dodgr_sample
dodgr_isoverts

dodgr_isoverts
dodgr_vertices

dodgr_vertices
dodgr_paths

dodgr_paths
hampi

hampi
match_pts_to_graph

match_pts_to_graph
dodgr_isodists

dodgr_isodists
dodgr_times

dodgr_times
dodgr_streetnet

dodgr_streetnet
merge_directed_flows

merge_directed_flows
dodgr_to_igraph

dodgr_to_igraph
weight_railway

weight_railway
igraph_to_dodgr

igraph_to_dodgr
dodgr_streetnet_sc

dodgr_streetnet_sc
match_points_to_graph

match_points_to_graph
write_dodgr_wt_profile

write_dodgr_wt_profile
dodgr_components

dodgr_components
dodgr_distances

dodgr_distances
dodgr_contract_graph

dodgr_contract_graph
dodgr_cache_on

dodgr_cache_on
dodgr_dists

dodgr_dists
dodgr

dodgr.
dodgr_cache_off

dodgr_cache_off
clear_dodgr_cache

clear_dodgr_cache
compare_heaps

compare_heaps