Skip to content

Commit

Permalink
No dodgr (#494)
Browse files Browse the repository at this point in the history
  • Loading branch information
Robinlovelace committed Aug 18, 2022
1 parent a417dbd commit f24b8a9
Show file tree
Hide file tree
Showing 6 changed files with 1 addition and 135 deletions.
3 changes: 1 addition & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ Imports:
sfheaders
Suggests:
cyclestreets,
dodgr (>= 0.0.3),
geodist,
igraph (>= 1.2.2),
knitr (>= 1.20),
Expand All @@ -70,5 +69,5 @@ VignetteBuilder:
Encoding: UTF-8
LazyData: yes
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.0
RoxygenNote: 7.2.1
SystemRequirements: GNU make
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ export(rnet_get_nodes)
export(rnet_group)
export(route)
export(route_average_gradient)
export(route_dodgr)
export(route_google)
export(route_nearest_point)
export(route_osrm)
Expand Down
88 changes: 0 additions & 88 deletions R/route.R
Original file line number Diff line number Diff line change
Expand Up @@ -157,91 +157,3 @@ most_common_class_of_list <- function(l, class_to_find = "sf") {
is_class <- class_out == class_to_find
is_class
}
#' Route on local data using the dodgr package
#'
#' @inheritParams route
#' @param net sf object representing the route network
#' @family routes
#' @export
#' @examples
#' if (requireNamespace("dodgr")) {
#' from <- c(-1.5327, 53.8006) # from <- geo_code("pedallers arms leeds")
#' to <- c(-1.5279, 53.8044) # to <- geo_code("gzing")
#' # next 4 lines were used to generate `stplanr::osm_net_example`
#' # pts <- rbind(from, to)
#' # colnames(pts) <- c("X", "Y")
#' # net <- dodgr::dodgr_streetnet(pts = pts, expand = 0.1)
#' # osm_net_example <- net[c("highway", "name", "lanes", "maxspeed")]
#' r <- route_dodgr(from, to, net = osm_net_example)
#' plot(osm_net_example$geometry)
#' plot(r$geometry, add = TRUE, col = "red", lwd = 5)
#' }
route_dodgr <- function(from = NULL,
to = NULL,
l = NULL,
net = NULL
# ,
# return_net = FALSE
) {
if (!requireNamespace("dodgr", quietly = TRUE)) {
stop("dodgr must be installed for this function to work.")
}
od_coordinate_matrix <- od_coords(from, to, l)
to_coords <- od_coordinate_matrix[, 3:4, drop = FALSE]
fm_coords <- od_coordinate_matrix[, 1:2, drop = FALSE]
# Try to get route network if net not provided
if (is.null(net)) {
pts <- rbind(fm_coords, to_coords)
net <- dodgr::dodgr_streetnet(pts = pts, expand = 0.2)
message("Network not provided, fetching network using dodgr_streetnet")
}

ckh <- dodgr::dodgr_cache_off()
suppressMessages(
ways_dg <- dodgr::weight_streetnet(net)
)

verts <- dodgr::dodgr_vertices(ways_dg) # the vertices or points for routing
# suppressMessages ({
from_id <- unique(verts$id[dodgr::match_pts_to_graph(verts, fm_coords,
connected = TRUE
)])
to_id <- unique(verts$id[dodgr::match_pts_to_graph(verts, to_coords,
connected = TRUE
)])
# })
dp <- dodgr::dodgr_paths(ways_dg, from = from_id, to = to_id)
paths <- lapply(dp, function(i) {
lapply(i, function(j) {
if (is.null(j)) {
return(NULL)
}
res <- verts[match(j, verts$id), c("x", "y")]
sf::st_linestring(as.matrix(res))
})
})
nms <- as.character(unlist(lapply(paths, function(i) names(i))))
from_to <- do.call(rbind, strsplit(nms, "-"))
from_xy <- fm_coords[match(from_to[, 1], unique(from_to[, 1])), , drop = FALSE]
to_xy <- fm_coords[match(from_to[, 2], unique(from_to[, 2])), , drop = FALSE]

# remove any NULL paths:
paths <- unlist(paths, recursive = FALSE)
index <- which(vapply(paths, is.null, logical(1)))
if (any(index)) {
message("unable to trace ", length(index), " path(s)")
message("Failed path index numbers are:")
message(list(as.integer(index)))
}
index <- which(!seq(paths) %in% index)
paths <- sf::st_sfc(paths[index], crs = 4326)
sf::st_sf(
from = from_to[index, 1],
from_x = from_xy [index, 1],
from_y = from_xy [index, 2],
to = from_to[index, 2],
to_x = to_xy [index, 1],
to_y = to_xy [index, 2],
geometry = paths
)
}
2 changes: 0 additions & 2 deletions man/route.Rd

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

41 changes: 0 additions & 41 deletions man/route_dodgr.Rd

This file was deleted.

1 change: 0 additions & 1 deletion man/route_osrm.Rd

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

0 comments on commit f24b8a9

Please sign in to comment.