Calculate matrix of pair-wise distances between points.
Usage
dodgr_dists(
graph,
from = NULL,
to = NULL,
shortest = TRUE,
pairwise = FALSE,
heap = "BHeap",
parallel = TRUE,
quiet = TRUE
)
Arguments
- graph
data.frame
or equivalent object representing the network graph (see Notes). Fordodgr
street networks, this may be a network derived from either sf or silicate ("sc") data, generated with weight_streetnet.The
from
andto
columns ofgraph
may be either single columns of numeric or character values specifying the numbers or names of graph vertices, or combinations to two columns specifying geographical (longitude and latitude,) coordinates. In the latter case, almost any sensible combination of names will be accepted (for example,fromx, fromy
,from_x, from_y
, orfr_lat, fr_lon
.)Note that longitude and latitude values are always interpreted in 'dodgr' to be in EPSG:4326 / WSG84 coordinates. Any other kinds of coordinates should first be reprojected to EPSG:4326 before submitting to any 'dodgr' routines.
See further information in Details.
- from
Vector or matrix of points from which route distances are to be calculated, specified as one of the following:
Single character vector precisely matching node numbers or names given in
graph$from
orgraph$to
.Single vector of integer-ish values, in which case these will be presumed to specify indices into dodgr_vertices, and NOT to correspond to values in the 'from' or 'to' columns of the graph. See the example below for a demonstration.
Matrix or equivalent of longitude and latitude coordinates, in which case these will be matched on to the nearest coordinates of 'from' and 'to' points in the graph.
- to
Vector or matrix of points to which route distances are to be calculated. If
to
isNULL
, pairwise distances will be calculated from allfrom
points to all other nodes ingraph
. If bothfrom
andto
areNULL
, pairwise distances are calculated between all nodes ingraph
.- shortest
If
FALSE
, calculate distances along the fastest rather than shortest routes. For street networks produced with weight_streetnet, distances may also be calculated along the fastest routes with theshortest = FALSE
option. Graphs must in this case have columns oftime
andtime_weighted
. Note that the fastest routes will only be approximate when derived from sf-format data generated with the osmdata functionosmdata_sf()
, and will be much more accurate when derived fromsc
-format data generated withosmdata_sc()
. See weight_streetnet for details.- pairwise
If
TRUE
, calculate distances only between the ordered pairs offrom
andto
.- heap
Type of heap to use in priority queue. Options include Fibonacci Heap (default;
FHeap
), Binary Heap (BHeap
),Trinomial Heap (
TriHeap), Extended Trinomial Heap (
TriHeapExt, and 2-3 Heap (
Heap23`).- parallel
If
TRUE
, perform routing calculation in parallel. Calculations in parallel ought very generally be advantageous. For small graphs, calculating distances in parallel is likely to offer relatively little gain in speed, but increases from parallel computation will generally markedly increase with increasing graph sizes. By default, parallel computation uses the maximal number of available cores or threads. This number can be reduced by specifying a value viaRcppParallel::setThreadOptions (numThreads = <desired_number>)
. Parallel calculations are, however, not able to be interrupted (for example, byCtrl-C
), and can only be stopped by killing the R process.- quiet
If
FALSE
, display progress messages on screen.
Details
graph
must minimally contain three columns of from
,
to
, dist
. If an additional column named weight
or
wt
is present, shortest paths are calculated according to values
specified in that column; otherwise according to dist
values. Either
way, final distances between from
and to
points are calculated
by default according to values of dist
. That is, paths between any pair of
points will be calculated according to the minimal total sum of weight
values (if present), while reported distances will be total sums of dist
values.
See also
Other distances:
dodgr_distances()
,
dodgr_dists_categorical()
,
dodgr_dists_nearest()
,
dodgr_flows_aggregate()
,
dodgr_flows_disperse()
,
dodgr_flows_si()
,
dodgr_isochrones()
,
dodgr_isodists()
,
dodgr_isoverts()
,
dodgr_paths()
,
dodgr_times()
Examples
# A simple graph
graph <- data.frame (
from = c ("A", "B", "B", "B", "C", "C", "D", "D"),
to = c ("B", "A", "C", "D", "B", "D", "C", "A"),
d = c (1, 2, 1, 3, 2, 1, 2, 1)
)
dodgr_dists (graph)
#> A B C D
#> A 0 1 2 3
#> B 2 0 1 2
#> C 2 2 0 1
#> D 1 2 2 0
# Example of "from" and "to" as integer-ish values, in which case they are
# interpreted to index into "dodgr_vertices()":
graph <- data.frame (
from = c (1, 3, 2, 2, 3, 3, 4, 4),
to = c (2, 1, 3, 4, 2, 4, 3, 1),
d = c (1, 2, 1, 3, 2, 1, 2, 1)
)
dodgr_dists (graph, from = 1, to = 2)
#> 3
#> 1 2
# That then gives distance from "1" to "3" because the vertices are built
# sequentially along "graph$from":
dodgr_vertices (graph)
#> id n
#> 1 1 0
#> 2 3 1
#> 3 2 2
#> 7 4 3
# And vertex$id [2] is "3"
# A larger example from the included [hampi()] data.
graph <- weight_streetnet (hampi)
from <- sample (graph$from_id, size = 100)
to <- sample (graph$to_id, size = 50)
d <- dodgr_dists (graph, from = from, to = to)
# d is a 100-by-50 matrix of distances between `from` and `to`
if (FALSE) { # \dontrun{
# a more complex street network example, thanks to @chrijo; see
# https://github.com/UrbanAnalyst/dodgr/issues/47
xy <- rbind (
c (7.005994, 51.45774), # limbeckerplatz 1 essen germany
c (7.012874, 51.45041)
) # hauptbahnhof essen germany
xy <- data.frame (lon = xy [, 1], lat = xy [, 2])
essen <- dodgr_streetnet (pts = xy, expand = 0.2, quiet = FALSE)
graph <- weight_streetnet (essen, wt_profile = "foot")
d <- dodgr_dists (graph, from = xy, to = xy)
# First reason why this does not work is because the graph has multiple,
# disconnected components.
table (graph$component)
# reduce to largest connected component, which is always number 1
graph <- graph [which (graph$component == 1), ]
d <- dodgr_dists (graph, from = xy, to = xy)
# should work, but even then note that
table (essen$level)
# There are parts of the network on different building levels (because of
# shopping malls and the like). These may or may not be connected, so it may
# be necessary to filter out particular levels
index <- which (!(essen$level == "-1" | essen$level == "1")) # for example
library (sf) # needed for following sub-select operation
essen <- essen [index, ]
graph <- weight_streetnet (essen, wt_profile = "foot")
graph <- graph [which (graph$component == 1), ]
d <- dodgr_dists (graph, from = xy, to = xy)
} # }