Distances on Directed Graphs in R
at main 487 lines 17 kB view raw
1# Generated by using Rcpp::compileAttributes() -> do not edit by hand 2# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393 3 4#' rcpp_centrality - parallel function 5#' 6#' sample is used to estimate timing, by calculating centrality from just a few 7#' vertices. 8#' @noRd 9rcpp_centrality <- function (graph, vert_map_in, heap_type, dist_threshold, edge_centrality, sample) { 10 .Call (`_dodgr_rcpp_centrality`, graph, vert_map_in, heap_type, dist_threshold, edge_centrality, sample) 11} 12 13#' rcpp_concaveman 14#' @noRd 15rcpp_concaveman <- function (xy, hull_in, concavity, length_threshold) { 16 .Call (`_dodgr_rcpp_concaveman`, xy, hull_in, concavity, length_threshold) 17} 18 19#' De-duplicate edges by replacing with minimal weighted distances and times 20#' 21#' @param graph Full graph in any form 22#' @param fr_col Name of column holding from edge labels 23#' @param to_col Name of column holding to edge labels 24#' @param d_col Name of column holding weighted distances 25#' @param t_col Name of column holding weighted times (or "" if no weighted 26#' times). 27#' @return A `data.frame` of 3 columns: 'from', 'to', and 'd', where 'd' is the 28#' minimal value taken from all duplicated edges. If 't_col' is specified, the 29#' equivalent minimal times are in the lower half of the result. 30#' @noRd 31rcpp_deduplicate <- function (graph, fr_col, to_col, d_col, t_col) { 32 .Call (`_dodgr_rcpp_deduplicate`, graph, fr_col, to_col, d_col, t_col) 33} 34 35#' Make unordered_set of all new edge names 36#' @noRd 37NULL 38 39#' Make vector of all new edge names 40#' @noRd 41NULL 42 43#' Get numbers of old edges corresponding to each contracted edge 44#' @noRd 45NULL 46 47#' Collect sets of all from and to vertices for each set of edges corresponding 48#' to each contracted edge. These sets aren't in any particular order, but the 49#' two sets may be used to match the from and to vertices. These sets are then 50#' arranged into sequences in the subsequent function, 51#' \code{order_vert_sequences}. 52#' @noRd 53NULL 54 55#' rcpp_aggregate_to_sf 56#' 57#' Aggregate a dodgr network data.frame to an sf LINESTRING data.frame 58#' 59#' @param graph_full Rcpp::DataFrame containing the **full** graph 60#' @param graph_contr Rcpp::DataFrame containing the **contracted** graph 61#' @param edge_map Rcpp::DataFrame containing the edge map returned from 62#' \code{dodgr_contract_graph} 63#' 64#' @return Rcpp::List object of `sf::LINESTRING` geoms 65#' 66#' @noRd 67rcpp_aggregate_to_sf <- function (graph_full, graph_contr, edge_map) { 68 .Call (`_dodgr_rcpp_aggregate_to_sf`, graph_full, graph_contr, edge_map) 69} 70 71#' rcpp_flows_aggregate_par 72#' 73#' @param graph The data.frame holding the graph edges 74#' @param vert_map_in map from <std::string> vertex ID to (0-indexed) integer 75#' index of vertices 76#' @param fromi Index into vert_map_in of vertex numbers 77#' @param toi Index into vert_map_in of vertex numbers 78#' @param tol Relative tolerance in terms of flows below which targets 79#' (to-vertices) are not considered. 80#' 81#' @note The parallelisation is achieved by dumping the results of each thread 82#' to a file, with aggregation performed at the end by simply reading back and 83#' aggregating all files. There is no way to aggregate into a single vector 84#' because threads have to be independent. The only danger with this approach 85#' is that multiple threads may generate the same file names, but with names 10 86#' characters long, that chance should be 1 / 62 ^ 10. 87#' 88#' @noRd 89rcpp_flows_aggregate_par <- function (graph, vert_map_in, fromi, toi_in, flows, norm_sums, tol, heap_type) { 90 .Call (`_dodgr_rcpp_flows_aggregate_par`, graph, vert_map_in, fromi, toi_in, flows, norm_sums, tol, heap_type) 91} 92 93#' rcpp_flows_aggregate_pairwise 94#' 95#' Pairwise version of flows_aggregate_par 96#' 97#' @param graph The data.frame holding the graph edges 98#' @param vert_map_in map from <std::string> vertex ID to (0-indexed) integer 99#' index of vertices 100#' @param fromi Index into vert_map_in of vertex numbers 101#' @param toi Index into vert_map_in of vertex numbers 102#' @param tol Relative tolerance in terms of flows below which targets 103#' (to-vertices) are not considered. 104#' 105#' @note The parallelisation is achieved by dumping the results of each thread 106#' to a file, with aggregation performed at the end by simply reading back and 107#' aggregating all files. There is no way to aggregate into a single vector 108#' because threads have to be independent. The only danger with this approach 109#' is that multiple threads may generate the same file names, but with names 10 110#' characters long, that chance should be 1 / 62 ^ 10. 111#' 112#' @noRd 113rcpp_flows_aggregate_pairwise <- function (graph, vert_map_in, fromi, toi, flows, norm_sums, tol, heap_type) { 114 .Call (`_dodgr_rcpp_flows_aggregate_pairwise`, graph, vert_map_in, fromi, toi, flows, norm_sums, tol, heap_type) 115} 116 117#' rcpp_flows_disperse_par 118#' 119#' Modified version of \code{rcpp_flows_aggregate} that aggregates flows to all 120#' destinations from given set of origins, with flows attenuated by distance 121#' from those origins. 122#' 123#' @param graph The data.frame holding the graph edges 124#' @param vert_map_in map from <std::string> vertex ID to (0-indexed) integer 125#' index of vertices 126#' @param fromi Index into vert_map_in of vertex numbers 127#' @param k Coefficient of (current proof-of-principle-only) exponential 128#' distance decay function. If value of \code{k<0} is given, a standard 129#' logistic polynomial will be used. 130#' 131#' @note The flow data to be used for aggregation is a matrix mapping flows 132#' between each pair of from and to points. 133#' 134#' @noRd 135rcpp_flows_disperse_par <- function (graph, vert_map_in, fromi, k, dens, tol, heap_type) { 136 .Call (`_dodgr_rcpp_flows_disperse_par`, graph, vert_map_in, fromi, k, dens, tol, heap_type) 137} 138 139#' rcpp_flows_si 140#' 141#' @param graph The data.frame holding the graph edges 142#' @param vert_map_in map from <std::string> vertex ID to (0-indexed) integer 143#' index of vertices 144#' @param fromi Index into vert_map_in of vertex numbers 145#' @param toi Index into vert_map_in of vertex numbers 146#' @param kvec Vector of k-values for each fromi 147#' @param nvec Vector of density-values for each fromi 148#' @param tol Relative tolerance in terms of flows below which targets 149#' (to-vertices) are not considered. 150#' 151#' @noRd 152rcpp_flows_si <- function (graph, vert_map_in, fromi, toi_in, kvec, dens_from, dens_to, norm_sums, tol, heap_type) { 153 .Call (`_dodgr_rcpp_flows_si`, graph, vert_map_in, fromi, toi_in, kvec, dens_from, dens_to, norm_sums, tol, heap_type) 154} 155 156#' @noRd 157rcpp_fundamental_cycles <- function (graph, verts) { 158 .Call (`_dodgr_rcpp_fundamental_cycles`, graph, verts) 159} 160 161#' get_to_from 162#' 163#' Get one pair of two and from edges and vertices. Main task is to make sure 164#' that bi-directed edges ("intermediate_double") correctly return the 165#' **different** values of from and to vertices and edges. 166#' 167#' @noRd 168NULL 169 170#' same_hwy_type 171#' 172#' Determine whether two edges represent the same weight category (type of 173#' highway for street networks, for example). Categories are not retained in 174#' converted graphs, but can be discerned by comparing ratios of weighted to 175#' non-weighted distances. 176#' @noRd 177NULL 178 179#' rcpp_contract_graph 180#' 181#' Removes nodes and edges from a graph that are not needed for routing 182#' 183#' @param graph graph to be processed 184#' 185#' @return \code{Rcpp::List} containing one \code{data.frame} with the 186#' contracted graph, one \code{data.frame} with the original graph and one 187#' \code{data.frame} containing information about the relating edge ids of the 188#' original and contracted graph. 189#' 190#' @noRd 191rcpp_contract_graph <- function (graph, vertlist_in) { 192 .Call (`_dodgr_rcpp_contract_graph`, graph, vertlist_in) 193} 194 195#' rcpp_merge_cols 196#' 197#' Merge columns in directed graph to form aggregate undirected columns, and 198#' return a corresponding undirected graph useful for visualisation. 199#' 200#' @param graph The result of a call to \code{dodgr_flows_aggregate/disperse} 201#' or similar function resuling in columns of directed values. 202#' @return A single vector of aggregate values with non-zero values only for 203#' those edges to be retained in the directed graph. 204#' 205#' @noRd 206rcpp_merge_cols <- function (graph) { 207 .Call (`_dodgr_rcpp_merge_cols`, graph) 208} 209 210#' sample_one_edge_no_comps 211#' 212#' Sample one edge for graph that has no pre-calculated components. Only used 213#' in \code{sample_one_vertex} 214#' 215#' @param edge_map edge_map 216#' @return std::vector of 2 elements: [0] with value of largest connected 217#' component; [1] with random index to one edge that is part of that component. 218#' @noRd 219NULL 220 221#' sample_one_edge_with_comps 222#' 223#' Sample one edge for graph that has pre-calculated components. Only used in 224#' \code{sample_one_vertex} 225#' 226#' @param edge_map edge_map 227#' @return Random index to one edge that is part of the largest connected 228#' component. 229#' @noRd 230NULL 231 232#' rcpp_sample_graph 233#' 234#' Randomly sample one connected componnent of a graph 235#' 236#' @param graph graph to be processed 237#' @param nverts_to_sample Number of vertices to sample 238#' @param quiet If TRUE, display progress 239#' 240#' @return Smaller sub-set of \code{graph} 241#' 242#' @noRd 243rcpp_sample_graph <- function (graph, nverts_to_sample) { 244 .Call (`_dodgr_rcpp_sample_graph`, graph, nverts_to_sample) 245} 246 247#' graph_has_components 248#' 249#' Does a graph have a vector of connected component IDs? Only used in 250#' \code{sample_one_vertex} 251#' @noRd 252NULL 253 254#' @name graph_from_df 255#' 256#' Convert a standard graph data.frame into an object of class graph. Graphs 257#' are standardised with the function \code{dodgr_convert_graph()$graph}, and 258#' contain only the four columns [from, to, d, w] 259#' 260#' @noRd 261NULL 262 263#' identify_graph_components 264#' 265#' Identify initial graph components for each **vertex** 266#' Identification for edges is subsequently perrformed with 267#' \code{rcpp_get_component_vector}. 268#' 269#' @param v unordered_map <vertex_id_t, vertex_t> 270#' @param com component map from each vertex to component numbers 271#' @noRd 272NULL 273 274#' rcpp_get_component_vector 275#' 276#' Get component numbers for each edge of graph 277#' 278#' @param graph graph to be processed; stripped down and standardised to five 279#' columns 280#' 281#' @return Two vectors: one of edge IDs and one of corresponding component 282#' numbers 283#' @noRd 284rcpp_get_component_vector <- function (graph) { 285 .Call (`_dodgr_rcpp_get_component_vector`, graph) 286} 287 288#' rcpp_unique_rownames 289#' 290#' Construct vertex (from, to) ID values from unique pairs of coordinates 291#' rounded to <precision>. Used when vertices have no ID values. 292#' 293#' @noRd 294rcpp_unique_rownames <- function (xyfrom, xyto, precision = 10L) { 295 .Call (`_dodgr_rcpp_unique_rownames`, xyfrom, xyto, precision) 296} 297 298#' Determine which side of intersecting line a point lies on. 299#' 300#' @param (ax, ay) Coordinates of one end of line 301#' @param (bx, by) Coordinates of other end of line 302#' @param (x, y) Coordinates of point 303#' @return 0 if point on line, -1 if to the left; +1 if to the right. 304#' 305#' @noRd 306NULL 307 308#' Simple match of points to nearest vertices 309#' @noRd 310NULL 311 312#' Match points to nearest edge of graph at which perpendicular from point 313#' bisects edges. Uses psuedo-code from 314#' https://stackoverflow.com/a/6853926 315#' @noRd 316NULL 317 318#' rcpp_points_index_par 319#' 320#' Get index of nearest vertices to list of points 321#' 322#' @param xy Rcpp::DataFrame containing the vertex coordinates of the graph 323#' @param pts Rcpp::DataFrame containing the points to be matched 324#' 325#' @return 0-indexed Rcpp::NumericVector index into graph of nearest points 326#' 327#' @noRd 328rcpp_points_index_par <- function (xy, pts) { 329 .Call (`_dodgr_rcpp_points_index_par`, xy, pts) 330} 331 332#' rcpp_points_to_edges_par 333#' 334#' Get index of nearest edges to list of points 335#' 336#' @param graph Rcpp::DataFrame containing the full edge-based graph 337#' @param pts Rcpp::DataFrame containing the points to be matched 338#' 339#' @return 0-indexed Rcpp::NumericVector index into graph of nearest points 340#' 341#' @noRd 342rcpp_points_to_edges_par <- function (graph, pts) { 343 .Call (`_dodgr_rcpp_points_to_edges_par`, graph, pts) 344} 345 346#' rcpp_get_sp_dists_par 347#' 348#' @noRd 349rcpp_get_sp_dists_par <- function (graph, vert_map_in, fromi, toi_in, heap_type, is_spatial) { 350 .Call (`_dodgr_rcpp_get_sp_dists_par`, graph, vert_map_in, fromi, toi_in, heap_type, is_spatial) 351} 352 353#' rcpp_get_sp_dists_nearest 354#' 355#' @noRd 356rcpp_get_sp_dists_nearest <- function (graph, vert_map_in, fromi, toi_in, heap_type) { 357 .Call (`_dodgr_rcpp_get_sp_dists_nearest`, graph, vert_map_in, fromi, toi_in, heap_type) 358} 359 360#' rcpp_get_sp_dists_paired_par 361#' 362#' @noRd 363rcpp_get_sp_dists_paired_par <- function (graph, vert_map_in, fromi, toi, heap_type, is_spatial) { 364 .Call (`_dodgr_rcpp_get_sp_dists_paired_par`, graph, vert_map_in, fromi, toi, heap_type, is_spatial) 365} 366 367#' rcpp_get_iso 368#' 369#' @noRd 370rcpp_get_iso <- function (graph, vert_map_in, fromi, dlim, heap_type) { 371 .Call (`_dodgr_rcpp_get_iso`, graph, vert_map_in, fromi, dlim, heap_type) 372} 373 374#' rcpp_get_sp_dists 375#' 376#' @noRd 377rcpp_get_sp_dists <- function (graph, vert_map_in, fromi, toi_in, heap_type) { 378 .Call (`_dodgr_rcpp_get_sp_dists`, graph, vert_map_in, fromi, toi_in, heap_type) 379} 380 381#' rcpp_get_paths 382#' 383#' @param graph The data.frame holding the graph edges 384#' @param vert_map_in map from <std::string> vertex ID to (0-indexed) integer 385#' index of vertices 386#' @param fromi Index into vert_map_in of vertex numbers 387#' @param toi Index into vert_map_in of vertex numbers 388#' 389#' @note The graph is constructed with 0-indexed vertex numbers contained in 390#' code{vert_map_in}. Both \code{fromi} and \code{toi} already map directly 391#' onto these. The graph has to be constructed by first constructing a 392#' \code{std::map} object (\code{vertmap}) for \code{vert_map_in}, then 393#' translating all \code{graph["from"/"to"]} values into these indices. This 394#' construction is done in \code{inst_graph}. 395#' 396#' @note Returns 1-indexed values indexing directly into the R input 397#' 398#' @noRd 399rcpp_get_paths <- function (graph, vert_map_in, fromi, toi_in, heap_type) { 400 .Call (`_dodgr_rcpp_get_paths`, graph, vert_map_in, fromi, toi_in, heap_type) 401} 402 403rcpp_get_paths_pairwise <- function (graph, vert_map_in, fromi, toi_in, heap_type) { 404 .Call (`_dodgr_rcpp_get_paths_pairwise`, graph, vert_map_in, fromi, toi_in, heap_type) 405} 406 407#' rcpp_get_sp_dists_categorical 408#' 409#' The `graph` must have an `edge_type` column of non-negative integers, 410#' with 0 denoting edges which are not aggregated, and all other values 411#' defining aggregation categories. 412#' 413#' Implemented in parallal form only; no single-threaded version, and 414#' only for AStar (so graphs must be spatial). 415#' @noRd 416rcpp_get_sp_dists_categorical <- function (graph, vert_map_in, fromi, toi_in, heap_type, proportions_only) { 417 .Call (`_dodgr_rcpp_get_sp_dists_categorical`, graph, vert_map_in, fromi, toi_in, heap_type, proportions_only) 418} 419 420#' rcpp_get_sp_dists_categ_paired 421#' 422#' Pairwise version of 'get_sp_dists_categorical'. The `graph` must have an 423#' `edge_type` column of non-negative integers, with 0 denoting edges which are 424#' not aggregated, and all other values defining aggregation categories. 425#' 426#' Implemented in parallal form only; no single-threaded version, and 427#' only for AStar (so graphs must be spatial). 428#' @noRd 429rcpp_get_sp_dists_categ_paired <- function (graph, vert_map_in, fromi, toi_in, heap_type) { 430 .Call (`_dodgr_rcpp_get_sp_dists_categ_paired`, graph, vert_map_in, fromi, toi_in, heap_type) 431} 432 433#' rcpp_get_sp_dists_cat_threshold 434#' 435#' The `graph` must have an `edge_type` column of non-negative integers, 436#' with 0 denoting edges which are not aggregated, and all other values 437#' defining aggregation categories. 438#' 439#' Implemented in parallal form only; no single-threaded version, and 440#' only for AStar (so graphs must be spatial). 441#' @noRd 442rcpp_get_sp_dists_cat_threshold <- function (graph, vert_map_in, fromi, dlimit, heap_type) { 443 .Call (`_dodgr_rcpp_get_sp_dists_cat_threshold`, graph, vert_map_in, fromi, dlimit, heap_type) 444} 445 446#' rcpp_gen_hash 447#' 448#' Efficient generation of long sequences of hash keys 449#' 450#' @noRd 451rcpp_gen_hash <- function (n, hash_len) { 452 .Call (`_dodgr_rcpp_gen_hash`, n, hash_len) 453} 454 455#' rcpp_sf_as_network 456#' 457#' Return OSM data from Simple Features format input 458#' 459#' @param sf_lines An sf collection of LINESTRING objects 460#' @param pr Rcpp::DataFrame containing the weighting profile 461#' 462#' @return Rcpp::List objects of OSM data, one matrix of numeric and one of 463#' character values. The former contain 7 columns: 464#' 1. sf geom index 465#' 2. from longitude 466#' 3. from latitude 467#' 4. to longitude 468#' 5. to latitude 469#' 6. distance 470#' 7. weighted_distance 471#' The character value matrix has 4 columns of: 472#' 1. from ID 473#' 2. to ID 474#' 3. highway type 475#' 4. OSM way ID 476#' 477#' @noRd 478rcpp_sf_as_network <- function (sf_lines, pr) { 479 .Call (`_dodgr_rcpp_sf_as_network`, sf_lines, pr) 480} 481 482#' rcpp_route_times 483#' 484#' @noRd 485rcpp_route_times <- function (graph, left_side, turn_penalty) { 486 .Call (`_dodgr_rcpp_route_times`, graph, left_side, turn_penalty) 487}