Skip to content

Commit

Permalink
mv match_points fns to new R/graph-match-points.R for #103 [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
mpadge committed Aug 9, 2022
1 parent 50bf727 commit 580df94
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 95 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.074
Version: 0.2.14.075
Authors@R: c(
person("Mark", "Padgham", , "[email protected]", role = c("aut", "cre")),
person("Andreas", "Petutschnig", role = "aut"),
Expand Down
93 changes: 0 additions & 93 deletions R/graph-functions-misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -246,99 +246,6 @@ find_xy_col_simple <- function (dfr) {
return (c (ix, iy))
}

#' match_pts_to_graph
#'
#' Match spatial points to a spatial graph which contains vertex coordinates
#'
#' @param verts A `data.frame` of vertices obtained from
#' `dodgr_vertices(graph)`.
#' @param xy coordinates of points to be matched to the vertices, either as
#' matrix or \pkg{sf}-formatted `data.frame`.
#' @param connected Should points be matched to the same (largest) connected
#' component of graph? If `FALSE` and these points are to be used for a
#' `dodgr` routing routine (\link{dodgr_dists}, \link{dodgr_paths}, or
#' \link{dodgr_flows_aggregate}), then results may not be returned if points are
#' not part of the same connected component. On the other hand, forcing them to
#' be part of the same connected component may decrease the spatial accuracy of
#' matching.
#'
#' @return A vector index into verts
#' @family misc
#' @export
#' @examples
#' net <- weight_streetnet (hampi, wt_profile = "foot")
#' verts <- dodgr_vertices (net)
#' # Then generate some random points to match to graph
#' 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))
#' )
#' pts <- match_pts_to_graph (verts, xy)
#' pts # an index into verts
#' pts <- verts$id [pts]
#' pts # names of those vertices
match_pts_to_graph <- function (verts, xy, connected = FALSE) {

if (!all (c ("id", "x", "y") %in% names (verts))) {
message (
"First argument to match_pts_to_graph should be result of ",
"dodgr_vertices;\npresuming you've submitted the network ",
"itself and will now try extracting the vertices"
)
verts <- dodgr_vertices (verts)
}
if (!(is.matrix (xy) || is.data.frame (xy))) {
stop ("xy must be a matrix or data.frame")
}
if (!is (xy, "sf")) {
if (ncol (xy) != 2) {
stop ("xy must have only two columns")
}
}

indx <- seq (nrow (verts))
if (connected) {
vertsi <- verts [which (verts$component == 1), ]
indx <- match (vertsi$id, verts$id)
}

xyi <- find_xy_col_simple (verts)
verts <- data.frame (x = verts [indx, xyi [1]], y = verts [indx, xyi [2]])
if (is (xy, "tbl")) {
xy <- data.frame (xy)
}
if (is (xy, "sf")) {
if (!"geometry" %in% names (xy)) {
stop ("xy has no sf geometry column")
} # nocov
if (!is (xy$geometry, "sfc_POINT")) {
stop ("xy$geometry must be a collection of sfc_POINT objects")
}
xy <- unlist (lapply (xy$geometry, as.numeric)) %>%
matrix (nrow = 2) %>%
t ()
xy <- data.frame (x = xy [, 1], y = xy [, 2])
} else {
xyi <- find_xy_col_simple (xy)
xy <- data.frame (x = xy [, xyi [1]], y = xy [, xyi [2]])
}

# rcpp_points_index is 0-indexed, so ...
indx [rcpp_points_index_par (verts, xy) + 1]
}

#' match_points_to_graph
#'
#' Alias for \link{match_points_to_graph}
#' @inherit match_pts_to_graph
#' @family misc
#' @export
match_points_to_graph <- function (verts, xy, connected = FALSE) {

match_pts_to_graph (verts, xy, connected = connected)
}

# vertices randomly selected from a graph without turn penalties may be
# submitted to functions along with the corresponding graph with turn angles.
# The latter version appends vertex IDs with "_start" and "_end" for the starts
Expand Down
92 changes: 92 additions & 0 deletions R/graph-match-points.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#' match_pts_to_graph
#'
#' Match spatial points to a spatial graph which contains vertex coordinates
#'
#' @param verts A `data.frame` of vertices obtained from
#' `dodgr_vertices(graph)`.
#' @param xy coordinates of points to be matched to the vertices, either as
#' matrix or \pkg{sf}-formatted `data.frame`.
#' @param connected Should points be matched to the same (largest) connected
#' component of graph? If `FALSE` and these points are to be used for a
#' `dodgr` routing routine (\link{dodgr_dists}, \link{dodgr_paths}, or
#' \link{dodgr_flows_aggregate}), then results may not be returned if points are
#' not part of the same connected component. On the other hand, forcing them to
#' be part of the same connected component may decrease the spatial accuracy of
#' matching.
#'
#' @return A vector index into verts
#' @family misc
#' @export
#' @examples
#' net <- weight_streetnet (hampi, wt_profile = "foot")
#' verts <- dodgr_vertices (net)
#' # Then generate some random points to match to graph
#' 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))
#' )
#' pts <- match_pts_to_graph (verts, xy)
#' pts # an index into verts
#' pts <- verts$id [pts]
#' pts # names of those vertices
match_pts_to_graph <- function (verts, xy, connected = FALSE) {

if (!all (c ("id", "x", "y") %in% names (verts))) {
message (
"First argument to match_pts_to_graph should be result of ",
"dodgr_vertices;\npresuming you've submitted the network ",
"itself and will now try extracting the vertices"
)
verts <- dodgr_vertices (verts)
}
if (!(is.matrix (xy) || is.data.frame (xy))) {
stop ("xy must be a matrix or data.frame")
}
if (!is (xy, "sf")) {
if (ncol (xy) != 2) {
stop ("xy must have only two columns")
}
}

indx <- seq (nrow (verts))
if (connected) {
vertsi <- verts [which (verts$component == 1), ]
indx <- match (vertsi$id, verts$id)
}

xyi <- find_xy_col_simple (verts)
verts <- data.frame (x = verts [indx, xyi [1]], y = verts [indx, xyi [2]])
if (is (xy, "tbl")) {
xy <- data.frame (xy)
}
if (is (xy, "sf")) {
if (!"geometry" %in% names (xy)) {
stop ("xy has no sf geometry column")
} # nocov
if (!is (xy$geometry, "sfc_POINT")) {
stop ("xy$geometry must be a collection of sfc_POINT objects")
}
xy <- unlist (lapply (xy$geometry, as.numeric)) %>%
matrix (nrow = 2) %>%
t ()
xy <- data.frame (x = xy [, 1], y = xy [, 2])
} else {
xyi <- find_xy_col_simple (xy)
xy <- data.frame (x = xy [, xyi [1]], y = xy [, xyi [2]])
}

# rcpp_points_index is 0-indexed, so ...
indx [rcpp_points_index_par (verts, xy) + 1]
}

#' match_points_to_graph
#'
#' Alias for \link{match_points_to_graph}
#' @inherit match_pts_to_graph
#' @family misc
#' @export
match_points_to_graph <- function (verts, xy, connected = FALSE) {

match_pts_to_graph (verts, xy, connected = connected)
}
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.074",
"version": "0.2.14.075",
"programmingLanguage": {
"@type": "ComputerLanguage",
"name": "R",
Expand Down

0 comments on commit 580df94

Please sign in to comment.