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)

  • 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.8.0, GDAL 3.0.4, PROJ 6.3.1

# 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
#> #
#> # Node Data:     701 × 1 (active)
#> # Geometry type: POINT
#> # Dimension:     XY
#> # Bounding box:  xmin: 7.522622 ymin: 51.94151 xmax: 7.546705 ymax: 51.9612
#>              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)
#> # … with 695 more rows
#> #
#> # Edge Data:     851 × 5
#> # Geometry type: LINESTRING
#> # Dimension:     XY
#> # Bounding box:  xmin: 7.522594 ymin: 51.94151 xmax: 7.546705 ymax: 51.9612
#>    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…
#> # … with 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
#> #
#> # Node Data:     3 × 1 (active)
#> # Geometry type: POINT
#> # Dimension:     XY
#> # Bounding box:  xmin: 7 ymin: 51 xmax: 8 ymax: 52
#>         x
#>   <POINT>
#> 1  (7 51)
#> 2  (7 52)
#> 3  (8 52)
#> #
#> # Edge Data:     2 × 3
#> # Geometry type: LINESTRING
#> # Dimension:     XY
#> # Bounding box:  xmin: 7 ymin: 51 xmax: 8 ymax: 52
#>    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, quietly = TRUE)) {
  as_sfnetwork(simplenet)
}
#> spatstat.geom 2.3-0
#> spatstat.core 2.3-2
#> spatstat.linnet 2.3-0
#> 
#> spatstat 2.2-0       (nickname: ‘That's not important right now’) 
#> For an introduction to spatstat, type ‘beginner’ 
#> # A sfnetwork with 10 nodes and 10 edges
#> #
#> # CRS:  NA 
#> #
#> # A directed acyclic simple graph with 1 component with spatially explicit edges
#> #
#> # Node Data:     10 × 1 (active)
#> # Geometry type: POINT
#> # Dimension:     XY
#> # Bounding box:  xmin: 0.1497036 ymin: 0.1608339 xmax: 0.8418658 ymax:
#> #   0.9415173
#>                    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)
#> # … with 4 more rows
#> #
#> # Edge Data:     10 × 4
#> # Geometry type: LINESTRING
#> # Dimension:     XY
#> # Bounding box:  xmin: 0.1497036 ymin: 0.1608339 xmax: 0.8418658 ymax:
#> #   0.9415173
#>    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)
#> # … with 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
#> #
#> # Node Data:     20 × 1 (active)
#> # Geometry type: POINT
#> # Dimension:     XY
#> # Bounding box:  xmin: 0.08243756 ymin: 0.003948339 xmax: 0.9888917 ymax:
#> #   0.9782264
#>                    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)
#> # … with 14 more rows
#> #
#> # Edge Data:     10 × 4
#> # Geometry type: LINESTRING
#> # Dimension:     XY
#> # Bounding box:  xmin: 0.08243756 ymin: 0.003948339 xmax: 0.9888917 ymax:
#> #   0.9782264
#>    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)
#> # … with 7 more rows