luukvdmeer / sfnetworks

Tidy Geospatial Networks in R
https://luukvdmeer.github.io/sfnetworks/
Other
334 stars 20 forks source link

Print method does not work with spatially implicit edges #256

Open JosiahParry opened 10 months ago

JosiahParry commented 10 months ago

Describe the bug

Cannot activate edges when missing geometry. This issue is based on Geographic Data Science with Python

Reproducible example

library(sf)
library(dplyr)
library(igraph)
library(sfnetworks)

gurl <- "https://raw.githubusercontent.com/gdsbook/book/master/data/cache/yoyogi_park_graph.graphml"

g <- igraph::read_graph(gurl, format = "graphml")

# covert to tbl_graph
nodes <- tidygraph::as_tbl_graph(g) 

# create notes geometry
n_geo <- as_tibble(nodes) |>
  transmute(across(c(x, y), as.numeric)) |>
  st_as_sf(coords = c("x", "y"), crs = 4326) |>
  st_geometry()

# move geometry into sfn
g_nodes <- nodes |>
  mutate(geometry = n_geo) 

g_sf <- sfnetworks::as_sfnetwork(g_nodes)
#> Checking if spatial network structure is valid...
#> Spatial network structure is valid

g_sf |>
  activate(edges)
#> # A sfnetwork with 106 nodes and 287 edges
#> #
#> Error: This call requires spatially explicit edges when applied to the edges table. Activate nodes first?

Expected behavior

I expect to have the edges be active.

R Session Info

R version 4.3.0 (2023-04-21)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Ventura 13.3

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: Europe/Berlin
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] dplyr_1.1.2      sf_1.0-12        igraph_1.4.2     sfnetworks_0.6.3

loaded via a namespace (and not attached):
 [1] styler_1.9.1         utf8_1.2.3           generics_0.1.3       tidyr_1.3.0         
 [5] class_7.3-21         xml2_1.3.4           lwgeom_0.2-11        KernSmooth_2.23-20  
 [9] lattice_0.21-8       digest_0.6.31        magrittr_2.0.3       spatstat.utils_3.0-3
[13] evaluate_0.20        grid_4.3.0           bookdown_0.34        fastmap_1.1.1       
[17] R.oo_1.25.0          R.cache_0.16.0       Matrix_1.5-4         processx_3.8.1      
[21] R.utils_2.12.2       e1071_1.7-13         codegrip_0.0.0.9000  DBI_1.1.3           
[25] ps_1.7.5             purrr_1.0.1          fansi_1.0.4          scales_1.2.1        
[29] cli_3.6.1            rlang_1.1.1          crayon_1.5.2         units_0.8-2         
[33] R.methodsS3_1.8.2    reprex_2.0.2         tidygraph_1.2.3      munsell_0.5.0       
[37] withr_2.5.0          yaml_2.3.7           tools_4.3.0          sfheaders_0.4.2     
[41] colorspace_2.1-0     ggplot2_3.4.2        vctrs_0.6.3          R6_2.5.1            
[45] proxy_0.4-27         lifecycle_1.0.3      classInt_0.4-9       fs_1.6.2            
[49] callr_3.7.3          clipr_0.8.0          pkgconfig_2.0.3      pillar_1.9.0        
[53] gtable_0.3.3         glue_1.6.2           Rcpp_1.0.11          xfun_0.39           
[57] tibble_3.2.1         tidyselect_1.2.0     rstudioapi_0.14      knitr_1.42          
[61] htmltools_0.5.5      rmarkdown_2.21       compiler_4.3.0      
idshklein commented 10 months ago

Did you try Net |> convert(to_spatailly_explicit) |> activate (edges)?

JosiahParry commented 10 months ago

No idea what magic incantation that is but

g_sf |>
  tidygraph::convert(to_spatial_explicit) |> 
  activate(edges) 

works! ! !

loreabad6 commented 10 months ago

Even if this works with that workaround, activating the edges with spatially implicit objects should still work. Will look into it, but this is probably a bug.

loreabad6 commented 10 months ago

@JosiahParry there are a couple of issues that opened up with your example that I'd find interesting to look into. Moving this into discussions since it is not directly related to this bug.

loreabad6 commented 10 months ago

Inside print.sfnetwork we use st_crs() to show on the header. But if no geometry column is present, st_crs cannot be called. Working on a quick fix that would activate the nodes to extract the CRS everytime, since CRS should be the same for nodes and edges. @luukvdmeer, should st_crs() work also on sfnetworks with active edges but no geometry?