as_sfnetwork: Convert a foreign object to a sfnetwork

View source: R/create.R

as_sfnetworkR Documentation

Convert a foreign object to a sfnetwork

Description

Convert a given object into an object of class sfnetwork.

Usage

as_sfnetwork(x, ...)

## Default S3 method:
as_sfnetwork(x, ...)

## S3 method for class 'sf'
as_sfnetwork(x, ...)

## S3 method for class 'sfc'
as_sfnetwork(x, ...)

## S3 method for class 'dodgr_streetnet'
as_sfnetwork(x, ...)

## S3 method for class 'linnet'
as_sfnetwork(x, ...)

## S3 method for class 'psp'
as_sfnetwork(x, ...)

## S3 method for class 'sfNetwork'
as_sfnetwork(x, ...)

## S3 method for class 'tbl_graph'
as_sfnetwork(x, ...)

Arguments

x

Object to be converted into a sfnetwork.

...

Additional arguments passed on to other functions.

Value

An object of class sfnetwork.

Methods (by class)

  • as_sfnetwork(default): By default, the provided object is first converted into a tbl_graph using as_tbl_graph. Further conversion into an sfnetwork will work as long as the nodes can be converted to an sf object through st_as_sf. Arguments to st_as_sf can be provided as additional arguments and will be forwarded to st_as_sf through the sfnetwork construction function.

  • as_sfnetwork(sf): Convert spatial features of class sf directly into a sfnetwork. Supported geometry types are either LINESTRING or POINT. In the first case, the lines become the edges in the network, and nodes are placed at their boundaries. Additional arguments are forwarded to create_from_spatial_lines. In the latter case, the points become the nodes in the network, and are connected by edges according to a specified method. Additional arguments are forwarded to create_from_spatial_points.

  • as_sfnetwork(sfc): Convert spatial geometries of class sfc directly into a sfnetwork. Supported geometry types are either LINESTRING or POINT. In the first case, the lines become the edges in the network, and nodes are placed at their boundaries. Additional arguments are forwarded to create_from_spatial_lines. In the latter case, the points become the nodes in the network, and are connected by edges according to a specified method. Additional arguments are forwarded to create_from_spatial_points.

  • as_sfnetwork(dodgr_streetnet): Convert a directed graph of class dodgr_streetnet directly into a sfnetwork. Additional arguments are forwarded to dodgr_to_sfnetwork. This requires the dodgr package to be installed.

  • as_sfnetwork(linnet): Convert spatial linear networks of class linnet directly into a sfnetwork. Additional arguments are forwarded to create_from_spatial_lines. This requires the spatstat.geom package to be installed.

  • as_sfnetwork(psp): Convert spatial line segments of class psp directly into a sfnetwork. The lines become the edges in the network, and nodes are placed at their boundary points. Additional arguments are forwarded to create_from_spatial_lines.

  • as_sfnetwork(sfNetwork): Convert spatial networks of class sfNetwork from the stplanr package directly into a sfnetwork. This will extract the edges as an sf object and re-create the network structure. Additional arguments are forwarded to create_from_spatial_lines.The directness of the original network is preserved unless specified otherwise through the directed argument.

  • as_sfnetwork(tbl_graph): Convert graph objects of class tbl_graph directly into a sfnetwork. This will work if at least the nodes can be converted to an sf object through st_as_sf. Arguments to st_as_sf can be provided as additional arguments and will be forwarded to st_as_sf through the sfnetwork construction function. The directness of the original graph is preserved unless specified otherwise through the directed argument.

Examples

# From an sf object with LINESTRING geometries.
library(sf, quietly = TRUE)

oldpar = par(no.readonly = TRUE)
par(mar = c(1,1,1,1), mfrow = c(1,2))

as_sfnetwork(roxel)

plot(st_geometry(roxel))
plot(as_sfnetwork(roxel))

# From an sf object with POINT geometries.
# For more examples see ?create_from_spatial_points.
as_sfnetwork(mozart)

plot(st_geometry(mozart))
plot(as_sfnetwork(mozart))

par(oldpar)

# From a dodgr_streetnet object.
if (require(dodgr, quietly = TRUE) & require(geodist, quietly = TRUE)) {
  as_sfnetwork(dodgr::weight_streetnet(hampi))
}

# From a linnet object.
if (require(spatstat.geom, quietly = TRUE)) {
  as_sfnetwork(simplenet)
}

# From a psp object.
if (require(spatstat.geom, quietly = TRUE)) {
  set.seed(42)
  test_psp = psp(runif(10), runif(10), runif(10), runif(10), window=owin())
  as_sfnetwork(test_psp)
}

# From a tbl_graph with coordinate columns.
library(tidygraph, quietly = TRUE)

nodes = data.frame(lat = c(7, 7, 8), lon = c(51, 52, 52))
edges = data.frame(from = c(1, 1, 3), to = c(2, 3, 2))
tbl_net = tbl_graph(nodes, edges)
as_sfnetwork(tbl_net, coords = c("lon", "lat"), crs = 4326)


luukvdmeer/sfnetworks documentation built on Nov. 21, 2024, 4:54 a.m.