Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Update spatial network funs - load sf if needed #243

Merged
merged 4 commits into from
Dec 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 12 additions & 19 deletions R/SpatialLinesNetwork.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ setClass("sfNetwork", representation(sl = "sf",
#' plot(shortpath, col = "red", lwd = 4, add = TRUE)
#' library(sf)
#' SLN_sf <- SpatialLinesNetwork(route_network_sf)
#' plot(SLN_sf@sl$geometry)
#' # shortpath <- sum_network_routes(SLN_sf, 1, 50, sumvars = "length")
#' # plot(shortpath, col = "red", lwd = 4, add = TRUE)
#' plot(SLN_sf)
#' shortpath <- sum_network_routes(SLN_sf, 1, 50, sumvars = "length")
#' plot(shortpath$geometry, col = "red", lwd = 4, add = TRUE)
SpatialLinesNetwork <- function(sl, uselonglat = FALSE, tolerance = 0.000) {
UseMethod("SpatialLinesNetwork")
}
Expand Down Expand Up @@ -120,10 +120,6 @@ SpatialLinesNetwork.Spatial <- function(sl, uselonglat = FALSE, tolerance = 0.00
#' @export
SpatialLinesNetwork.sf <-function(sl, uselonglat = FALSE, tolerance = 0.000) {

if ("sf" %in% (.packages()) == FALSE) {
stop("sf package must be loaded first. Run library(sf)")
}

nodecoords <- as.data.frame(sf::st_coordinates(sl)) %>%
dplyr::group_by(.data$L1) %>%
dplyr::mutate(nrow = n(), rownum = 1:n()) %>%
Expand Down Expand Up @@ -165,10 +161,9 @@ SpatialLinesNetwork.sf <-function(sl, uselonglat = FALSE, tolerance = 0.000) {
#' representation.
#' @param ... Arguments to pass to relevant plot function.
#' @examples
#' data(routes_fast)
#' rnet <- overline(routes_fast, attrib = "length")
#' SLN <- SpatialLinesNetwork(rnet)
#' SLN <- SpatialLinesNetwork(route_network)
#' plot(SLN)
#' plot(SLN, component = "graph")
#' @export
setMethod("plot", signature = c(x="SpatialLinesNetwork"),
definition = function(x, component = "sl", ...){
Expand All @@ -183,23 +178,21 @@ setMethod("plot", signature = c(x="SpatialLinesNetwork"),
}
})

#' Plot a sfNetwork
#' Plot an sfNetwork
#'
#' @param x The sfNetwork to plot
#' @param component The component of the network to plot. Valid values are "sl"
#' for the geographic (sf) representation or "graph" for the graph
#' representation.
#' @param ... Arguments to pass to relevant plot function.
#' @examples
#' data(routes_fast)
#' rnet <- overline(routes_fast, attrib = "length")
#' SLN <- SpatialLinesNetwork(rnet)
#' plot(SLN)
#' SLN_sf <- SpatialLinesNetwork(route_network_sf)
#' plot(SLN_sf)
#' @export
setMethod("plot", signature = c(x="sfNetwork"),
definition = function(x, component = "sl", ...){
if (component == "sl") {
sp::plot(x@sl, ...)
sp::plot(x@sl$geometry, ...)
}
else if (component == "graph") {
igraph::plot.igraph(x@g, ...)
Expand Down Expand Up @@ -482,16 +475,16 @@ sum_network_routes <- function(sln, start, end, sumvars, combinations = FALSE) {
if (length(start) != length(end) && combinations == FALSE) {
stop("start and end not the same length.")
}
if (is(sln, "sfNetwork") & "sf" %in% (.packages()) == FALSE) {
stop("sf package must be loaded first. Run library(sf)")
}

if (combinations == FALSE) {
routesegs <- lapply(1:length(start), function(i) {
unlist(igraph::get.shortest.paths(sln@g, start[i], end[i], output="epath")$epath)
})

if (is(sln, "sfNetwork")) {
if (is(sln, "sfNetwork") & "sf" %in% (.packages()) == FALSE) {
stop("Load the sf package, e.g. with\nlibrary(sf)")
}
routecoords <- mapply(function(routesegs, start) {
linecoords <- sf::st_coordinates(sln@sl[routesegs,])
linecoords <- lapply(1:max(linecoords[,'L1']), function(x){
Expand Down
6 changes: 3 additions & 3 deletions man/SpatialLinesNetwork.Rd

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

5 changes: 2 additions & 3 deletions man/plot-SpatialLinesNetwork-ANY-method.Rd

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

10 changes: 4 additions & 6 deletions man/plot-sfNetwork-ANY-method.Rd

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