as_sfnetwork: Convert a foreign object to a sfnetwork

View source: R/sfnetwork.R

as_sfnetworkR Documentation

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, ...)

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

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

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

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

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

## S3 method for class 'sfNetwork'
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 an sfnetwork.

...

Arguments passed on to the sfnetwork construction function.

Value

An object of class sfnetwork.

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

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


sfnetworks documentation built on May 29, 2024, 8:32 a.m.