Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add as sfnetwork methods #56

Merged
merged 3 commits into from
Jun 13, 2020
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
11 changes: 6 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ Imports:
tidygraph,
utils
Suggests:
dplyr,
fansi,
ggplot2,
knitr,
testthat
dplyr,
fansi,
ggplot2,
knitr,
testthat,
spatstat
RoxygenNote: 7.1.0
VignetteBuilder: knitr
URL: https://luukvdmeer.github.io/sfnetworks/, https://github.com/luukvdmeer/sfnetworks
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
S3method("st_crs<-",sfnetwork)
S3method("st_geometry<-",sfnetwork)
S3method(as_sfnetwork,default)
S3method(as_sfnetwork,linnet)
S3method(as_sfnetwork,psp)
S3method(as_sfnetwork,sf)
S3method(as_sfnetwork,sfNetwork)
S3method(as_sfnetwork,sfnetwork)
Expand Down
57 changes: 53 additions & 4 deletions R/sfnetwork.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' \code{sfnetwork} is a tidy data structure for geospatial networks. It extends
#' the graph manipulation functionalities of the
#' \code{\link[tidygraph]{tidygraph-package}} package into the domain of
#' geospatial networks, where the nodes and optionally also the edges are
#' geospatial networks, where the nodes and optionally also the edges are
#' embedded in geographical space, and enables to apply the spatial analytical
#' function from the \code{\link[sf:sf]{sf-package}} directly to the network.
#'
Expand Down Expand Up @@ -35,7 +35,7 @@
#' constructing the network. These checks guarantee a valid spatial network
#' structure. For the nodes, this means that they all should have \code{POINT}
#' geometries. In the case of spatially explicit edges, it is also checked that
#' all edges have \code{LINESTRING} geometries, nodes and edges have the same
#' all edges have \code{LINESTRING} geometries, nodes and edges have the same
#' CRS and boundary points of edges match their corresponding node coordinates.
#' These checks are important, but also time consuming. If you are already sure
#' your input data meet the requirements, the checks are unneccesary and can be
Expand All @@ -48,7 +48,7 @@
#'
#' @importFrom tidygraph tbl_graph
#' @export
sfnetwork = function(nodes, edges, directed = TRUE, edges_as_lines = NULL,
sfnetwork = function(nodes, edges, directed = TRUE, edges_as_lines = NULL,
force = FALSE, ...) {
# Automatically set edges_as_lines if not given.
if (is.null(edges_as_lines)) {
Expand Down Expand Up @@ -129,7 +129,7 @@ nodes_to_sf = function(nodes, ...) {
#'
#' @param x object to be converted into an \code{\link{sfnetwork}} object.
#'
#' @param ... arguments passed on to the \code{\link{sfnetwork}} construction
#' @param ... arguments passed on to the \code{\link{sfnetwork}} construction
#' function.
#'
#' @return An object of class \code{\link{sfnetwork}}.
Expand Down Expand Up @@ -200,6 +200,55 @@ as_sfnetwork.tbl_graph = function(x, ...) {
do.call("sfnetwork", args)
}

#' @name as_sfnetwork
#' @importFrom sf st_as_sf st_collection_extract
#' @export
#' @examples
#' # Examples for psp method
#' if (require(spatstat)) {
#' set.seed(42)
#' test_psp = psp(runif(10), runif(10), runif(10), runif(10), window=owin())
#' plot(test_psp, main = "spatstat input")
#' test_psp_as_sfnetwork = as_sfnetwork(test_psp)
#' plot(test_psp_as_sfnetwork, main = "sfnetworks output")
#' }
#'
as_sfnetwork.psp = function(x, ...) {
# I think that the easiest method for transforming a Line Segment Pattern
# (psp) object into sfnetwork format is to transform it into sf format and
# then apply the usual methods.
x_sf = sf::st_as_sf(x)

# x_sf is an sf object composed by 1 POLYGON (the window of the psp object)
# and several LINESTRINGs (the line segments). I'm not sure if and how we can
# use the window object so I will extract only the LINESTRINGs.
x_linestring = sf::st_collection_extract(x_sf, "LINESTRING")

# apply as_sfnetwork.sf
as_sfnetwork(x_linestring, ...)
}

#' @name as_sfnetwork
#' @export
#' @examples
#' # Examples for linnet method
#' if (require(spatstat)) {
#' plot(simplenet, main = "spatstat input")
#' simplenet_as_sfnetwork = as_sfnetwork(simplenet)
#' plot(simplenet_as_sfnetwork, main = "sfnetworks output")
#' }
#'
as_sfnetwork.linnet = function(x, ...) {
# IMO the easiest approach is the same as for psp objects, i.e. converting the
# linnet object into a psp format and then applying the corresponding method.
if (!requireNamespace("spatstat", quietly = TRUE)) {
stop("package spatstat required, please install it first")
}

x_psp = spatstat::as.psp(x)
as_sfnetwork(x_psp, ...)
}

#' @importFrom sf st_as_sf st_crs st_geometry
#' @importFrom rlang !!
#' @importFrom tibble trunc_mat
Expand Down
25 changes: 24 additions & 1 deletion man/as_sfnetwork.Rd

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

4 changes: 2 additions & 2 deletions man/sfnetwork.Rd

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