luukvdmeer / sfnetworks

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

`st_network_join()` returns a directed network although both inputs are undirected #174

Open loreabad6 opened 2 years ago

loreabad6 commented 2 years ago

Describe the bug When giving two unidrected networks to st_network_join() the result seems to always be a directed network.

Reproducible example

library(sfnetworks)
library(tidyverse)
(test1 = roxel %>% 
  slice(1:100) %>% 
  as_sfnetwork(directed = F))
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> # A sfnetwork with 175 nodes and 100 edges
#> #
#> # CRS:  EPSG:4326 
#> #
#> # An unrooted forest with 75 trees with spatially explicit edges
#> #
#> # Node Data:     175 x 1 (active)
#> # Geometry type: POINT
#> # Dimension:     XY
#> # Bounding box:  xmin: 7.522743 ymin: 51.94151 xmax: 7.546705 ymax: 51.95971
#>              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 169 more rows
#> #
#> # Edge Data:     100 x 5
#> # Geometry type: LINESTRING
#> # Dimension:     XY
#> # Bounding box:  xmin: 7.522743 ymin: 51.94151 xmax: 7.546705 ymax: 51.95971
#>    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 97 more rows
(test2 = roxel %>% 
  slice(100:200) %>% 
  as_sfnetwork(directed = F))
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> # A sfnetwork with 186 nodes and 101 edges
#> #
#> # CRS:  EPSG:4326 
#> #
#> # An unrooted forest with 85 trees with spatially explicit edges
#> #
#> # Node Data:     186 x 1 (active)
#> # Geometry type: POINT
#> # Dimension:     XY
#> # Bounding box:  xmin: 7.525152 ymin: 51.94213 xmax: 7.544025 ymax: 51.95979
#>              geometry
#>           <POINT [°]>
#> 1  (7.54049 51.95219)
#> 2 (7.540778 51.95161)
#> 3 (7.533461 51.95576)
#> 4 (7.533447 51.95562)
#> 5 (7.537679 51.94391)
#> 6 (7.538673 51.94424)
#> # ... with 180 more rows
#> #
#> # Edge Data:     101 x 5
#> # Geometry type: LINESTRING
#> # Dimension:     XY
#> # Bounding box:  xmin: 7.525152 ymin: 51.94213 xmax: 7.544025 ymax: 51.95979
#>    from    to name                type                                  geometry
#>   <int> <int> <chr>               <fct>                         <LINESTRING [°]>
#> 1     1     2 <NA>                service     (7.54049 51.95219, 7.540682 51.95~
#> 2     3     4 Havixbecker Strasse residential (7.533461 51.95576, 7.533447 51.9~
#> 3     5     6 Welsingheide        service     (7.537679 51.94391, 7.538673 51.9~
#> # ... with 98 more rows
st_network_join(test1, test2)
#> # A sfnetwork with 341 nodes and 201 edges
#> #
#> # CRS:  EPSG:4326 
#> #
#> # A directed multigraph with 142 components with spatially explicit edges
#> #
#> # Node Data:     341 x 1 (active)
#> # Geometry type: POINT
#> # Dimension:     XY
#> # Bounding box:  xmin: 7.522743 ymin: 51.94151 xmax: 7.546705 ymax: 51.95971
#>              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 335 more rows
#> #
#> # Edge Data:     201 x 5
#> # Geometry type: LINESTRING
#> # Dimension:     XY
#> # Bounding box:  xmin: 7.522743 ymin: 51.94151 xmax: 7.546705 ymax: 51.95979
#>    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 198 more rows

Created on 2021-09-23 by the reprex package (v0.3.0)

Expected behavior Direction of the input networks should be preserved after performing a network join. Workaround now is to run convert(to_undirected) to the result.

R Session Info ``` r sessionInfo() #> R version 4.0.3 (2020-10-10) #> Platform: x86_64-w64-mingw32/x64 (64-bit) #> Running under: Windows 10 x64 (build 19043) #> #> Matrix products: default #> #> locale: #> [1] LC_COLLATE=English_Austria.1252 LC_CTYPE=English_Austria.1252 #> [3] LC_MONETARY=English_Austria.1252 LC_NUMERIC=C #> [5] LC_TIME=English_Austria.1252 #> #> attached base packages: #> [1] stats graphics grDevices utils datasets methods base #> #> loaded via a namespace (and not attached): #> [1] compiler_4.0.3 magrittr_2.0.1 tools_4.0.3 htmltools_0.5.0 #> [5] yaml_2.2.1 stringi_1.7.3 rmarkdown_2.4 highr_0.8 #> [9] knitr_1.30 stringr_1.4.0 xfun_0.19 digest_0.6.27 #> [13] rlang_0.4.11 evaluate_0.14 ```
luukvdmeer commented 2 years ago

We rely on tidygraph::graph_join() to do the joining. But indeed this should not happen I would say. Does raise the question: what to do when joining a directed with an undirected network? Raise an error? Or just let tidygraph decide on this?