Skip to content

Commit

Permalink
test match_pts_to_graph + add_nodes_to_graph for #103
Browse files Browse the repository at this point in the history
  • Loading branch information
mpadge committed Aug 11, 2022
1 parent 03942e2 commit 505613b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 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.2.14.092
Version: 0.2.14.093
Authors@R: c(
person("Mark", "Padgham", , "[email protected]", role = c("aut", "cre")),
person("Andreas", "Petutschnig", role = "aut"),
Expand Down
2 changes: 1 addition & 1 deletion codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"codeRepository": "https://github.com/ATFutures/dodgr",
"issueTracker": "https://github.com/ATFutures/dodgr/issues",
"license": "https://spdx.org/licenses/GPL-3.0",
"version": "0.2.14.092",
"version": "0.2.14.093",
"programmingLanguage": {
"@type": "ComputerLanguage",
"name": "R",
Expand Down
70 changes: 69 additions & 1 deletion tests/testthat/test-match-pts-fns.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ skip_if (!test_all)
dodgr_cache_off ()
clear_dodgr_cache ()

test_that ("points to graph", {
test_that ("points to verts", {

bb <- attr (hampi$geometry, "bbox")
n <- 100
Expand Down Expand Up @@ -64,3 +64,71 @@ test_that ("points to graph", {
# match on the "$" symbol

})

test_that ("points to graph", {

bb <- attr (hampi$geometry, "bbox")
n <- 100
x <- bb [1] + (bb [3] - bb [1]) * runif (n)
y <- bb [2] + (bb [4] - bb [2]) * runif (n)
pts <- data.frame (x = x, y = y)
net <- weight_streetnet (hampi)
verts <- dodgr_vertices (net)

expect_error (
match_pts_to_graph (verts, pts),
"Points may only be matched to spatial graphs."
)

expect_silent (index1 <- match_pts_to_graph (net, pts))

colnames (pts) <- NULL
expect_message (
index2 <- match_pts_to_graph (net, pts),
"xy has no named columns; assuming order is x then y"
)
expect_identical (index1, index2)

pts <- data.frame (x = x, y = y, x2 = x)
expect_error (
match_pts_to_graph (net, list (pts)),
"xy must be a matrix or data.frame"
)
expect_error (
match_pts_to_graph (net, pts),
"xy must have only two columns"
)

pts <- data.frame (x = x, y = y)
expect_silent (index4 <- match_pts_to_graph (net, pts, connected = TRUE))
expect_true (!identical (index1, index4))

class (pts) <- c (class (pts), "tbl")
expect_silent (index5 <- match_pts_to_graph (net, pts, connected = TRUE))
expect_identical (index4, index5)

pts <- sf::st_as_sf (pts, coords = c (1, 2), crs = 4326)
expect_silent (index6 <- match_pts_to_graph (net, pts, connected = TRUE))
expect_identical (index4, index6)
expect_silent (index7 <- match_pts_to_graph (net, pts, connected = TRUE))
expect_identical (index4, index7)

})

test_that ("add nodes to graph", {

graph0 <- weight_streetnet (hampi, wt_profile = "foot")
verts <- dodgr_vertices (graph0)
set.seed (1)
npts <- 10
xy <- data.frame (
x = min (verts$x) + runif (npts) * diff (range (verts$x)),
y = min (verts$y) + runif (npts) * diff (range (verts$y))
)

graph1 <- add_nodes_to_graph (graph0, xy)

expect_identical (colnames (graph0), colnames (graph1))
expect_true ((nrow (graph1) - nrow (graph0)) > npts)
# actually equals 2 * npts when all edges are bi-directional.
})

0 comments on commit 505613b

Please sign in to comment.