Flip node/edge behavior #169
-
I have a street network sf object. I want to get the adjacency matrix defined by the streets connectivity. I'm trying to see if there is an easy way to do this using sfnetworks, here is what I have tried so far:
Here A will not give me what I want, as it will give me the adjacency matrix of the vertices defined at the intersections on the streets, rather than the adjacency of the streets --- defined by line string geometries --- themselves. Is there an easy way to flip or switch the edges and nodes within the sfnetworks paradigm? I have some sense of how I might go about doing this through either some manual manipulation of the sfnetwork object and/or just applying st_* operations on the original street graph, but my intuition is that there might be an easy fix here somewhere. I can provide a reprex and further details on my motivation if desired. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi @apeterson91! I think you are looking for the For example, we can create a super simple spatial network with 3 vertices and 2 edges: # packages
library(sf)
#> Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
library(tidygraph)
#>
#> Attaching package: 'tidygraph'
#> The following object is masked from 'package:stats':
#>
#> filter
library(sfnetworks)
# data
my_sfc <- st_sfc(
st_linestring(rbind(c(-1, 0), c(0, 0))),
st_linestring(rbind(c(0, 0), c(0, 1)))
)
# create sfnetwork
my_sfn <- as_sfnetwork(my_sfc, directed = FALSE)
par(mar = rep(0, 4))
plot(my_sfn) we can extract the adjacency matrix among the vertices igraph::as_adjacency_matrix(my_sfn)
#> 3 x 3 sparse Matrix of class "dgCMatrix"
#>
#> [1,] . 1 .
#> [2,] 1 . 1
#> [3,] . 1 . and also extract the adjacency matrix among the edges my_sfn %>%
convert(to_linegraph) %>%
igraph::as_adjacency_matrix()
#> 2 x 2 sparse Matrix of class "dgCMatrix"
#>
#> [1,] . 1
#> [2,] 1 . Created on 2021-08-27 by the reprex package (v2.0.0) |
Beta Was this translation helpful? Give feedback.
Hi @apeterson91! I think you are looking for the
to_linegraph
morpher to convert your graph into its Line graph. See also our vignette on morphers and?igraph::line_graph
.For example, we can create a super simple spatial network with 3 vertices and 2 edges:
…