Learn R Programming

sfnetworks (version 0.6.5)

as_sfnetwork: Convert a foreign object to a sfnetwork

Description

Convert a given object into an object of class sfnetwork. If an object can be read by as_tbl_graph and the nodes can be read by st_as_sf, it is automatically supported.

Usage

as_sfnetwork(x, ...)

# S3 method for default as_sfnetwork(x, ...)

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

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

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

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

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

# S3 method for sfnetwork as_sfnetwork(x, ...)

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

Value

An object of class sfnetwork.

Arguments

x

Object to be converted into an sfnetwork.

...

Arguments passed on to the sfnetwork construction function.

Methods (by class)

  • as_sfnetwork(sf): Only sf objects with either exclusively geometries of type LINESTRING or exclusively geometries of type POINT are supported. For lines, is assumed that the given features form the edges. Nodes are created at the endpoints of the lines. Endpoints which are shared between multiple edges become a single node. For points, it is assumed that the given features geometries form the nodes. They will be connected by edges sequentially. Hence, point 1 to point 2, point 2 to point 3, etc.

Examples

Run this code
# From an sf object.
library(sf, quietly = TRUE)

# With LINESTRING geometries.
as_sfnetwork(roxel)

oldpar = par(no.readonly = TRUE)
par(mar = c(1,1,1,1), mfrow = c(1,2))
plot(st_geometry(roxel))
plot(as_sfnetwork(roxel))
par(oldpar)

# With POINT geometries.
p1 = st_point(c(7, 51))
p2 = st_point(c(7, 52))
p3 = st_point(c(8, 52))
points = st_as_sf(st_sfc(p1, p2, p3))
as_sfnetwork(points)

oldpar = par(no.readonly = TRUE)
par(mar = c(1,1,1,1), mfrow = c(1,2))
plot(st_geometry(points))
plot(as_sfnetwork(points))
par(oldpar)

# 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)
}

Run the code above in your browser using DataLab