Skip to content

Commit

Permalink
Merge branch 'main' into oneway
Browse files Browse the repository at this point in the history
  • Loading branch information
mpadge authored Sep 30, 2024
2 parents 15fff69 + b4ec8c7 commit 46036d2
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 27 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: dodgr
Title: Distances on Directed Graphs
Version: 0.4.1.032
Version: 0.4.1.033
Authors@R: c(
person("Mark", "Padgham", , "[email protected]", role = c("aut", "cre")),
person("Andreas", "Petutschnig", role = "aut"),
Expand Down
37 changes: 30 additions & 7 deletions R/dists.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,22 @@
#' combination of names will be accepted (for example, `fromx, fromy`,
#' `from_x, from_y`, or `fr_lat, fr_lon`.)
#'
#' `from` and `to` values can be either two-column matrices or
#' equivalent of longitude and latitude coordinates, or else single columns
#' precisely matching node numbers or names given in `graph$from` or
#' `graph$to`. If `to` is `NULL`, pairwise distances are calculated from all
#' `from` points to all other nodes in `graph`. If both `from` and `to` are
#' `NULL`, pairwise distances are calculated between all nodes in `graph`.
#' The `from` and `to` parameters passed to this function can be either:
#' \itemize{
#' \item Single character vectors precisely matching node numbers or names
#' given in `graph$from` or `graph$to`.
#' \item Single vectors of integer-ish values, in which case these will be
#' presumed to specify indices into \link{dodgr_vertices}, and NOT to
#' correspond to values in the "from" or "to" columns of the graph. See the
#' example below for a demonstration.
#' \item matrices 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.
#' }
#'
#' If `to` is `NULL`, pairwise distances will be calculated from all `from`
#' points to all other nodes in `graph`. If both `from` and `to` are `NULL`,
#' pairwise distances are calculated between all nodes in `graph`.
#'
#' Calculations in parallel (`parallel = TRUE`) ought very generally be
#' advantageous. For small graphs, calculating distances in parallel is likely
Expand All @@ -74,6 +84,19 @@
#' )
#' dodgr_dists (graph)
#'
#' # 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)
#' # That then gives distance from "1" to "3" because the vertices are built
#' # sequentially along "graph$from":
#' dodgr_vertices (graph)
#' # 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)
Expand Down Expand Up @@ -331,7 +354,7 @@ get_pts_index <- function (graph,
pts <- matrix (pts, ncol = 1)
} else {
nms <- names (pts)
if (is.null (nms)) {
if (is.null (nms) && length (pts) > 1L) {
nms <- c ("x", "y")
}
pts <- matrix (pts, nrow = 1) # vector of (x,y) vals
Expand Down
2 changes: 1 addition & 1 deletion codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"codeRepository": "https://github.com/UrbanAnalyst/dodgr",
"issueTracker": "https://github.com/UrbanAnalyst/dodgr/issues",
"license": "https://spdx.org/licenses/GPL-3.0",
"version": "0.4.1.032",
"version": "0.4.1.033",
"programmingLanguage": {
"@type": "ComputerLanguage",
"name": "R",
Expand Down
35 changes: 29 additions & 6 deletions man/dodgr_distances.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 29 additions & 6 deletions man/dodgr_dists.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 29 additions & 6 deletions man/dodgr_times.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions tests/testthat/test-dists.R
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,48 @@ test_that ("all dists", {
expect_equal (nrow (d), nrow (v))
})

test_that ("to-from-as-integerish", {
# Integer-ish to-from cols (#254 from @luukvdmeer)
graph <- data.frame (
from = c (1, 2, 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)
)
expect_silent (d0 <- dodgr_dists (graph, from = 1, to = 2))
expect_silent (d1 <- dodgr_dists (graph, from = 1L, to = 2L))
expect_identical (d0, d1)
v <- dodgr_vertices (graph)
# Verts are numeric, but 'graph$from' has them in sequence, so:
expect_identical (v$id, as.numeric (seq_along (unique (graph$from))))

graph <- data.frame (
from = c (1, 3, 2, 2, 3, 3, 4, 4), # no longer in sequence!
to = c (2, 1, 3, 4, 2, 4, 3, 1),
d = c (1, 2, 1, 3, 2, 1, 2, 1)
)
expect_silent (d2 <- dodgr_dists (graph, from = 1, to = 2))
expect_silent (d3 <- dodgr_dists (graph, from = 1L, to = 2L))
v <- dodgr_vertices (graph)
expect_false (identical (v$id, as.numeric (seq_along (unique (graph$from)))))
expect_true (rownames (d2) == "1")
expect_true (rownames (d3) == "1")
expect_false (colnames (d2) == "2")
expect_false (colnames (d3) == "2")
real_id <- v$id [2] # = 3
expect_true (colnames (d2) == as.character (real_id))
expect_true (colnames (d3) == as.character (real_id))

graph <- data.frame (
from = as.character (c (1, 3, 2, 2, 3, 3, 4, 4)),
to = as.character (c (2, 1, 3, 4, 2, 4, 3, 1)),
d = c (1, 2, 1, 3, 2, 1, 2, 1)
)
d4 <- dodgr_dists (graph, from = "1", to = "2")
expect_equal (rownames (d4), "1")
expect_equal (colnames (d4), "2")

})

test_that ("to-from-cols", {
graph <- weight_streetnet (hampi)
nf <- 100
Expand Down

0 comments on commit 46036d2

Please sign in to comment.