0

I am looking for an efficient way to transfer a linear network (class linnet) from spatstat, e. g. library(spatstat); data('chicago'); net <- spatstat.linnet::domain.lpp(chicago) to a 'graph' object in igraph.

For an object of class lpp, I have some initial understanding:
from lpp back to sf's LINESTRINGs via maptools

library(spatstat)
library(maptools)
library(sf)
data('chicago')
chicago_v1 <- spatstat.geom::as.psp(chicago) # psp
chicago_v2 <- maptools::as.SpatialLines.psp(chicago_v1) # sp 
chicago_v3 <- sf::st_as_sf(chicago_v2)
class(chicago_v3)
#> [1] "sf"         "data.frame"

Is there an elegant way to graph object?

1 Answer 1

0

You can use sfnetworks to add an igraph class to a sf based spatial network.

library(spatstat)
library(maptools)
library(sf)
library(igraph)
library(sfnetworks)

data('chicago')
chicago <- sf::st_as_sf(maptools::as.SpatialLines.psp(
             spatstat.geom::as.psp(chicago)))
  nodes <- st_as_sf(as.data.frame(st_coordinates(chicago)), 
                   coords = c("X","Y"), agr = "constant") 
    node.names <- row.names(nodes)
 
g <- sfnetworks::as_sfnetwork(x = chicago, 
                             directed = FALSE, 
                             length_as_weight = TRUE, 
                             edges_as_lines = TRUE)
  class(g)
    plot(g)

If you want to stay in an sfnetwork class you have to activate which graph element you want to operate on eg., g |> tidygraph::activate("edges")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.