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.

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

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)
#> Linking to GEOS 3.10.2, GDAL 3.4.1, PROJ 8.2.1; sf_use_s2() is TRUE

# With LINESTRING geometries.
as_sfnetwork(roxel)
#> # A sfnetwork with 701 nodes and 851 edges
#> #
#> # CRS:  EPSG:4326 
#> #
#> # A directed multigraph with 14 components with spatially explicit edges
#> #
#> # A tibble: 701 × 1
#>              geometry
#>           <POINT [°]>
#> 1 (7.533722 51.95556)
#> 2 (7.533461 51.95576)
#> 3 (7.532442 51.95422)
#> 4  (7.53209 51.95328)
#> 5 (7.532709 51.95209)
#> 6 (7.532869 51.95257)
#> # ℹ 695 more rows
#> #
#> # A tibble: 851 × 5
#>    from    to name                  type                                geometry
#>   <int> <int> <chr>                 <fct>                       <LINESTRING [°]>
#> 1     1     2 Havixbecker Strasse   residential (7.533722 51.95556, 7.533461 51…
#> 2     3     4 Pienersallee          secondary   (7.532442 51.95422, 7.53236 51.…
#> 3     5     6 Schulte-Bernd-Strasse residential (7.532709 51.95209, 7.532823 51…
#> # ℹ 848 more rows

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)
#> # A sfnetwork with 3 nodes and 2 edges
#> #
#> # CRS:  NA 
#> #
#> # A rooted tree with spatially explicit edges
#> #
#> # A tibble: 3 × 1
#>         x
#>   <POINT>
#> 1  (7 51)
#> 2  (7 52)
#> 3  (8 52)
#> #
#> # A tibble: 2 × 3
#>    from    to            x
#>   <int> <int> <LINESTRING>
#> 1     1     2 (7 51, 7 52)
#> 2     2     3 (7 52, 8 52)

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)
}
#> spatstat.geom 3.1-0
#> # A sfnetwork with 10 nodes and 10 edges
#> #
#> # CRS:  NA 
#> #
#> # A directed acyclic simple graph with 1 component with spatially explicit edges
#> #
#> # A tibble: 10 × 1
#>                    geom
#>                 <POINT>
#> 1  (0.181276 0.8345078)
#> 2 (0.3254689 0.4607012)
#> 3 (0.1497036 0.2119066)
#> 4 (0.5358572 0.8028913)
#> 5 (0.4411403 0.1608339)
#> 6 (0.3795058 0.3206155)
#> # ℹ 4 more rows
#> #
#> # A tibble: 10 × 4
#>    from    to label                                         geom
#>   <int> <int> <chr>                                 <LINESTRING>
#> 1     1     2 segment  (0.181276 0.8345078, 0.3254689 0.4607012)
#> 2     3     2 segment (0.1497036 0.2119066, 0.3254689 0.4607012)
#> 3     2     4 segment (0.3254689 0.4607012, 0.5358572 0.8028913)
#> # ℹ 7 more rows

# 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)
}
#> # A sfnetwork with 20 nodes and 10 edges
#> #
#> # CRS:  NA 
#> #
#> # A rooted forest with 10 trees with spatially explicit edges
#> #
#> # A tibble: 20 × 1
#>                    geom
#>                 <POINT>
#> 1  (0.914806 0.4577418)
#> 2 (0.9040314 0.7375956)
#> 3 (0.9370754 0.7191123)
#> 4 (0.1387102 0.8110551)
#> 5 (0.2861395 0.9346722)
#> 6 (0.9888917 0.3881083)
#> # ℹ 14 more rows
#> #
#> # A tibble: 10 × 4
#>    from    to label                                         geom
#>   <int> <int> <chr>                                 <LINESTRING>
#> 1     1     2 segment  (0.914806 0.4577418, 0.9040314 0.7375956)
#> 2     3     4 segment (0.9370754 0.7191123, 0.1387102 0.8110551)
#> 3     5     6 segment (0.2861395 0.9346722, 0.9888917 0.3881083)
#> # ℹ 7 more rows