luukvdmeer / sfnetworks

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

Print degenerate sfnetwork objects #69

Closed agila5 closed 3 years ago

agila5 commented 4 years ago

Describe the bug It's not possible to print degenerate sfnetwork objects with 0 edges (that could occur estimating shortest paths))

Reproducible example

# packages
library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(sfnetworks)

# create some fake data
my_net = as_sfnetwork(
  st_sf(
    geometry = st_sfc(st_linestring(rbind(c(0, 0), c(1, 1))))
  ), 
  directed = FALSE
)
# plot
plot(my_net)


# estimate the shortest path and it's clearly composed by only 1 node
st_shortest_paths(my_net, from = st_point(c(1.1, 1)), to = st_point(c(0.9, 1))) # works fine
#> $vpath
#> $vpath[[1]]
#> + 1/2 vertex, from 32e550b:
#> [1] 2
#> 
#> 
#> $epath
#> NULL
#> 
#> $predecessors
#> NULL
#> 
#> $inbound_edges
#> NULL

# convert the graph to it's shortest path but it's not possible to print it
tidygraph::convert(my_net, to_spatial_shortest_paths, from = st_point(c(1.1, 1)), to = st_point(c(0.9, 1)))
#> Error in inactive_geom[[1]]: subscript out of bounds

Created on 2020-07-12 by the reprex package (v0.3.0)

Expected behavior No error in printing that object. The problem should be here:

https://github.com/luukvdmeer/sfnetworks/blob/799203a82e9c755d0a65dc4154db09fba268818a/R/sfnetwork.R#L298-L301

since we should consider the fact that inactive_geom could be an empty list object (since there is no edge there)

R Session Info

Session info ``` r devtools::session_info() #> - Session info --------------------------------------------------------------- #> setting value #> version R version 3.6.3 (2020-02-29) #> os Windows 10 x64 #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate Italian_Italy.1252 #> ctype Italian_Italy.1252 #> tz Europe/Berlin #> date 2020-07-12 #> #> - Packages ------------------------------------------------------------------- #> package * version date lib source #> assertthat 0.2.1 2019-03-21 [1] CRAN (R 3.6.0) #> backports 1.1.8 2020-06-17 [1] CRAN (R 3.6.3) #> callr 3.4.3 2020-03-28 [1] CRAN (R 3.6.3) #> class 7.3-15 2019-01-01 [2] CRAN (R 3.6.3) #> classInt 0.4-3 2020-04-07 [1] CRAN (R 3.6.3) #> cli 2.0.2 2020-02-28 [1] CRAN (R 3.6.3) #> crayon 1.3.4 2017-09-16 [1] CRAN (R 3.6.0) #> curl 4.3 2019-12-02 [1] CRAN (R 3.6.1) #> DBI 1.1.0 2019-12-15 [1] CRAN (R 3.6.3) #> desc 1.2.0 2018-05-01 [1] CRAN (R 3.6.0) #> devtools 2.3.0 2020-04-10 [1] CRAN (R 3.6.3) #> digest 0.6.25 2020-02-23 [1] CRAN (R 3.6.3) #> dplyr 1.0.0 2020-05-29 [1] CRAN (R 3.6.3) #> e1071 1.7-3 2019-11-26 [1] CRAN (R 3.6.1) #> ellipsis 0.3.1 2020-05-15 [1] CRAN (R 3.6.3) #> evaluate 0.14 2019-05-28 [1] CRAN (R 3.6.0) #> fansi 0.4.1 2020-01-08 [1] CRAN (R 3.6.2) #> fs 1.4.1 2020-04-04 [1] CRAN (R 3.6.3) #> generics 0.0.2 2018-11-29 [1] CRAN (R 3.6.0) #> glue 1.4.1 2020-05-13 [1] CRAN (R 3.6.3) #> highr 0.8 2019-03-20 [1] CRAN (R 3.6.0) #> htmltools 0.5.0 2020-06-16 [1] CRAN (R 3.6.3) #> httr 1.4.1 2019-08-05 [1] CRAN (R 3.6.1) #> igraph 1.2.5 2020-03-19 [1] CRAN (R 3.6.3) #> KernSmooth 2.23-16 2019-10-15 [2] CRAN (R 3.6.3) #> knitr 1.28 2020-02-06 [1] CRAN (R 3.6.2) #> lifecycle 0.2.0 2020-03-06 [1] CRAN (R 3.6.2) #> lwgeom 0.2-5 2020-06-12 [1] CRAN (R 3.6.3) #> magrittr 1.5 2014-11-22 [1] CRAN (R 3.6.0) #> memoise 1.1.0 2017-04-21 [1] CRAN (R 3.6.0) #> mime 0.9 2020-02-04 [1] CRAN (R 3.6.2) #> pillar 1.4.6 2020-07-10 [1] CRAN (R 3.6.3) #> pkgbuild 1.0.8 2020-05-07 [1] CRAN (R 3.6.3) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 3.6.1) #> pkgload 1.1.0 2020-05-29 [1] CRAN (R 3.6.3) #> prettyunits 1.1.1 2020-01-24 [1] CRAN (R 3.6.2) #> processx 3.4.3 2020-07-05 [1] CRAN (R 3.6.3) #> ps 1.3.3 2020-05-08 [1] CRAN (R 3.6.3) #> purrr 0.3.4 2020-04-17 [1] CRAN (R 3.6.3) #> R6 2.4.1 2019-11-12 [1] CRAN (R 3.6.1) #> Rcpp 1.0.5 2020-07-06 [1] CRAN (R 3.6.3) #> remotes 2.1.1 2020-02-15 [1] CRAN (R 3.6.2) #> rlang 0.4.7 2020-07-09 [1] CRAN (R 3.6.3) #> rmarkdown 2.2 2020-05-31 [1] CRAN (R 3.6.3) #> rprojroot 1.3-2 2018-01-03 [1] CRAN (R 3.6.0) #> sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 3.6.0) #> sf * 0.9-4 2020-06-13 [1] CRAN (R 3.6.3) #> sfnetworks * 0.3.1 2020-07-12 [1] Github (luukvdmeer/sfnetworks@799203a) #> stringi 1.4.6 2020-02-17 [1] CRAN (R 3.6.2) #> stringr 1.4.0 2019-02-10 [1] CRAN (R 3.6.0) #> testthat 2.3.2 2020-03-02 [1] CRAN (R 3.6.3) #> tibble 3.0.3.9000 2020-07-12 [1] Github (tidyverse/tibble@a57ad4a) #> tidygraph 1.2.0 2020-05-12 [1] CRAN (R 3.6.3) #> tidyr 1.1.0 2020-05-20 [1] CRAN (R 3.6.3) #> tidyselect 1.1.0 2020-05-11 [1] CRAN (R 3.6.3) #> units 0.6-7 2020-06-13 [1] CRAN (R 3.6.3) #> usethis 1.6.1 2020-04-29 [1] CRAN (R 3.6.3) #> vctrs 0.3.1.9000 2020-06-07 [1] Github (r-lib/vctrs@29baab9) #> withr 2.2.0 2020-04-20 [1] CRAN (R 3.6.3) #> xfun 0.15 2020-06-21 [1] CRAN (R 3.6.3) #> xml2 1.3.2 2020-04-23 [1] CRAN (R 3.6.3) #> yaml 2.2.1 2020-02-01 [1] CRAN (R 3.6.2) #> #> [1] C:/Users/Utente/Documents/R/win-library/3.6 #> [2] C:/Program Files/R/R-3.6.3/library ```
luukvdmeer commented 4 years ago

Thanks for noticing! @loreabad6 can I assign you? (no hurry)