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 atbl_graph
usingas_tbl_graph
. Further conversion into ansfnetwork
will work as long as the nodes can be converted to ansf
object throughst_as_sf
. Arguments tost_as_sf
can be provided as additional arguments and will be forwarded tost_as_sf
through thesfnetwork
construction function.as_sfnetwork(sf)
: Convert spatial features of classsf
directly into asfnetwork
. Supported geometry types are eitherLINESTRING
orPOINT
. In the first case, the lines become the edges in the network, and nodes are placed at their boundaries. Additional arguments are forwarded tocreate_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 tocreate_from_spatial_points
.as_sfnetwork(sfc)
: Convert spatial geometries of classsfc
directly into asfnetwork
. Supported geometry types are eitherLINESTRING
orPOINT
. In the first case, the lines become the edges in the network, and nodes are placed at their boundaries. Additional arguments are forwarded tocreate_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 tocreate_from_spatial_points
.as_sfnetwork(dodgr_streetnet)
: Convert a directed graph of classdodgr_streetnet
directly into asfnetwork
. Additional arguments are forwarded tododgr_to_sfnetwork
. This requires thedodgr
package to be installed.as_sfnetwork(linnet)
: Convert spatial linear networks of classlinnet
directly into asfnetwork
. Additional arguments are forwarded tocreate_from_spatial_lines
. This requires thespatstat.geom
package to be installed.as_sfnetwork(psp)
: Convert spatial line segments of classpsp
directly into asfnetwork
. The lines become the edges in the network, and nodes are placed at their boundary points. Additional arguments are forwarded tocreate_from_spatial_lines
.as_sfnetwork(sfNetwork)
: Convert spatial networks of classsfNetwork
from the stplanr package directly into asfnetwork
. This will extract the edges as ansf
object and re-create the network structure. Additional arguments are forwarded tocreate_from_spatial_lines
.The directness of the original network is preserved unless specified otherwise through thedirected
argument.as_sfnetwork(tbl_graph)
: Convert graph objects of classtbl_graph
directly into asfnetwork
. This will work if at least the nodes can be converted to ansf
object throughst_as_sf
. Arguments tost_as_sf
can be provided as additional arguments and will be forwarded tost_as_sf
through thesfnetwork
construction function. The directness of the original graph is preserved unless specified otherwise through thedirected
argument.
Examples
# From an sf object with LINESTRING geometries.
library(sf, quietly = TRUE)
#> Linking to GEOS 3.10.2, GDAL 3.4.1, PROJ 8.2.1; sf_use_s2() is TRUE
oldpar = par(no.readonly = TRUE)
par(mar = c(1,1,1,1), mfrow = c(1,2))
as_sfnetwork(roxel)
#> # A sfnetwork: 987 nodes and 1215 edges
#> #
#> # A directed multigraph with 9 components and spatially explicit edges
#> #
#> # Dimension: XY
#> # Bounding box: xmin: 7.522595 ymin: 51.94151 xmax: 7.546705 ymax: 51.96119
#> # Geodetic CRS: WGS 84
#> #
#> # Node data: 987 × 1 (active)
#> geometry
#> <POINT [°]>
#> 1 (7.538109 51.95286)
#> 2 (7.537867 51.95282)
#> 3 (7.537815 51.95867)
#> 4 (7.537015 51.95848)
#> 5 (7.533441 51.95578)
#> 6 (7.533415 51.95561)
#> # ℹ 981 more rows
#> #
#> # Edge data: 1,215 × 5
#> from to name type geometry
#> <int> <int> <chr> <chr> <LINESTRING [°]>
#> 1 1 2 Hagemanns Kämpken residential (7.538109 51.95286, 7.537867 51.95…
#> 2 3 4 Stiegkamp residential (7.537815 51.95867, 7.537015 51.95…
#> 3 5 6 Havixbecker Straße residential (7.533441 51.95578, 7.533467 51.95…
#> # ℹ 1,212 more rows
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)
#> # A sfnetwork: 17 nodes and 272 edges
#> #
#> # A bipartite simple graph with 1 component and spatially explicit edges
#> #
#> # Dimension: XY
#> # Bounding box: xmin: 4548664 ymin: 2747309 xmax: 4549589 ymax: 2748537
#> # Projected CRS: ETRS89-extended / LAEA Europe
#> #
#> # Node data: 17 × 4 (active)
#> name type website geometry
#> <chr> <chr> <chr> <POINT [m]>
#> 1 Mozartkino cinema https://www.mozartki… (4549504 2747309)
#> 2 Haus für Mozart theatre NA (4549003 2747376)
#> 3 Mozartsteg/Rudolfskai bus_stop NA (4549589 2747507)
#> 4 Mozart Denkmal artwork NA (4549387 2747514)
#> 5 Mozartsteg/Rudolfskai bus_stop NA (4549491 2747551)
#> 6 Mozartsteg bridge NA (4549473 2747624)
#> # ℹ 11 more rows
#> #
#> # Edge data: 272 × 3
#> from to geometry
#> <int> <int> <LINESTRING [m]>
#> 1 1 2 (4549504 2747309, 4549003 2747376)
#> 2 1 3 (4549504 2747309, 4549589 2747507)
#> 3 1 4 (4549504 2747309, 4549387 2747514)
#> # ℹ 269 more rows
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))
}
#> # A sfnetwork: 3337 nodes and 6813 edges
#> #
#> # A directed simple graph with 3 components and spatially explicit edges
#> #
#> # Dimension: XY
#> # Bounding box: xmin: 76.37261 ymin: 15.30104 xmax: 76.49232 ymax: 15.36033
#> # Geodetic CRS: WGS 84
#> #
#> # Node data: 3,337 × 4 (active)
#> name component n geometry
#> <chr> <int> <dbl> <POINT [°]>
#> 1 339318500 1 0 (76.47491 15.34167)
#> 2 339318502 1 1 (76.47612 15.34173)
#> 3 2398958028 1 2 (76.47621 15.34174)
#> 4 1427116077 1 3 (76.47628 15.34179)
#> 5 7799710916 1 4 (76.47634 15.34184)
#> 6 339318503 1 5 (76.47641 15.3419)
#> # ℹ 3,331 more rows
#> #
#> # Edge data: 6,813 × 12
#> from to weight d_weighted time time_weighted xfr yfr xto yto
#> <int> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 1 2 130. 144. 39.0 43.3 76.5 15.3 76.5 15.3
#> 2 2 1 130. 144. 39.0 43.3 76.5 15.3 76.5 15.3
#> 3 2 3 8.89 9.88 2.67 2.96 76.5 15.3 76.5 15.3
#> # ℹ 6,810 more rows
#> # ℹ 2 more variables: component <int>, geometry <LINESTRING [°]>
# From a linnet object.
if (require(spatstat.geom, quietly = TRUE)) {
as_sfnetwork(simplenet)
}
#> spatstat.univar 3.1-1
#> spatstat.geom 3.3-3
#> # A sfnetwork: 10 nodes and 10 edges
#> #
#> # A directed acyclic simple graph with 1 component and spatially explicit edges
#> #
#> # Dimension: XY
#> # Bounding box: xmin: 0.1497036 ymin: 0.1608339 xmax: 0.8418658 ymax: 0.9415173
#> # CRS: NA
#> #
#> # Node data: 10 × 1 (active)
#> 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
#> #
#> # Edge data: 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: 20 nodes and 10 edges
#> #
#> # A rooted forest with 10 trees and spatially explicit edges
#> #
#> # Dimension: XY
#> # Bounding box: xmin: 0.08243756 ymin: 0.003948339 xmax: 0.9888917 ymax: 0.9782264
#> # CRS: NA
#> #
#> # Node data: 20 × 1 (active)
#> 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
#> #
#> # Edge data: 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
# From a tbl_graph with coordinate columns.
library(tidygraph, quietly = TRUE)
#>
#> Attaching package: ‘tidygraph’
#> The following object is masked from ‘package:stats’:
#>
#> filter
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)
#> → Checking node geometry types ...
#> ✔ All nodes have geometry type POINT
#> ✔ Spatial network structure is valid
#> # A sfnetwork: 3 nodes and 3 edges
#> #
#> # A directed acyclic simple graph with 1 component and spatially implicit edges
#> #
#> # Dimension: XY
#> # Bounding box: xmin: 51 ymin: 7 xmax: 52 ymax: 8
#> # Geodetic CRS: WGS 84
#> #
#> # Node data: 3 × 1 (active)
#> geometry
#> <POINT [°]>
#> 1 (51 7)
#> 2 (52 7)
#> 3 (52 8)
#> #
#> # Edge data: 3 × 2
#> from to
#> <int> <int>
#> 1 1 2
#> 2 1 3
#> 3 3 2