luukvdmeer / sfnetworks

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

Doubt on the differences between `st_network_cost` and `st_network_paths` #172

Closed agila5 closed 3 years ago

agila5 commented 3 years ago

Describe` the bug I'm not 100% sure that this is a bug (almost surely not), but I don't understand why the following code returns two different results.

Reproducible example

# packages
library(sf)
#> Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
library(tidygraph)
library(sfnetworks)

# convert roxel to undirected sfn with just 1 component
sfn <- as_sfnetwork(roxel, directed = TRUE) %>% 
  convert(to_spatial_subdivision) %>% 
  convert(to_components, .clean = TRUE) %E>% 
  mutate(weight = edge_length())
#> Warning: to_spatial_subdivision assumes attributes are constant over geometries

# estimate cost from 10 to 100
st_network_cost(sfn, from = 10, to = 100)
#>          [,1]
#> [1,] 560.1045

# subset sfn to the shortest path between 10 and 100
shortest <- sfn %>% 
  convert(to_spatial_shortest_paths, from = 10, to = 100)

# Extract the edges/segments and sum their lengths
st_geometry(shortest, "edges") %>% st_length() %>% sum()
#> 1153.212 [m]

Created on 2021-09-03 by the reprex package (v2.0.1)

Expected behavior The same number returned by st_network_cost and the other procedure.

R Session Info

Session info ``` r sessioninfo::session_info() #> - Session info --------------------------------------------------------------- #> setting value #> version R version 4.0.5 (2021-03-31) #> os Windows 10 x64 #> system x86_64, mingw32 #> ui RTerm #> language en #> collate English_United Kingdom.1252 #> ctype English_United Kingdom.1252 #> tz Europe/Berlin #> date 2021-09-03 #> #> - Packages ------------------------------------------------------------------- #> package * version date lib #> abind 1.4-5 2016-07-21 [1] #> assertthat 0.2.1 2019-03-21 [1] #> backports 1.2.1 2020-12-09 [1] #> class 7.3-18 2021-01-24 [2] #> classInt 0.4-3 2020-04-07 [1] #> cli 3.0.1 2021-07-17 [1] #> colorspace 2.0-2 2021-06-24 [1] #> crayon 1.4.1 2021-02-08 [1] #> DBI 1.1.1 2021-01-15 [1] #> deldir 0.2-10 2021-02-16 [1] #> digest 0.6.27 2020-10-24 [1] #> dplyr 1.0.7 2021-06-18 [1] #> e1071 1.7-8 2021-07-28 [1] #> ellipsis 0.3.2 2021-04-29 [1] #> evaluate 0.14 2019-05-28 [1] #> fansi 0.5.0 2021-05-25 [1] #> fs 1.5.0 2020-07-31 [1] #> generics 0.1.0 2020-10-31 [1] #> ggplot2 3.3.5 2021-06-25 [1] #> glue 1.4.2 2020-08-27 [1] #> goftest 1.2-2 2019-12-02 [1] #> gtable 0.3.0 2019-03-25 [1] #> highr 0.9 2021-04-16 [1] #> htmltools 0.5.1.1 2021-01-22 [1] #> igraph 1.2.6 2020-10-06 [1] #> KernSmooth 2.23-18 2020-10-29 [2] #> knitr 1.33 2021-04-24 [1] #> lattice 0.20-41 2020-04-02 [2] #> lifecycle 1.0.0 2021-02-15 [1] #> lwgeom 0.2-7 2021-07-28 [1] #> magrittr 2.0.1 2020-11-17 [1] #> Matrix 1.3-2 2021-01-06 [2] #> mgcv 1.8-36 2021-06-01 [1] #> munsell 0.5.0 2018-06-12 [1] #> nlme 3.1-152 2021-02-04 [2] #> pillar 1.6.2 2021-07-29 [1] #> pkgconfig 2.0.3 2019-09-22 [1] #> polyclip 1.10-0.001 2021-03-16 [1] #> proxy 0.4-26 2021-06-07 [1] #> purrr 0.3.4 2020-04-17 [1] #> R6 2.5.1 2021-08-19 [1] #> Rcpp 1.0.7 2021-07-07 [1] #> reprex 2.0.1 2021-08-05 [1] #> rlang 0.4.11 2021-04-30 [1] #> rmarkdown 2.10 2021-08-06 [1] #> rpart 4.1-15 2019-04-12 [2] #> rstudioapi 0.13 2020-11-12 [1] #> s2 1.0.6 2021-06-17 [1] #> scales 1.1.1 2020-05-11 [1] #> sessioninfo 1.1.1 2018-11-05 [1] #> sf * 1.0-3 2021-08-02 [1] #> sfheaders 0.4.0 2020-12-01 [1] #> sfnetworks * 0.5.2.900 2021-09-03 [1] #> spatstat 2.2-0 2021-06-23 [1] #> spatstat.core 2.3-0 2021-07-16 [1] #> spatstat.data 2.1-0 2021-03-16 [1] #> spatstat.geom 2.2-2 2021-07-12 [1] #> spatstat.linnet 2.3-0.002 2021-08-25 [1] #> spatstat.sparse 2.0-0 2021-03-16 [1] #> spatstat.utils 2.2-0 2021-06-14 [1] #> stringi 1.7.3 2021-07-16 [1] #> stringr 1.4.0 2019-02-10 [1] #> styler 1.5.1 2021-07-13 [1] #> tensor 1.5 2012-05-05 [1] #> tibble 3.1.4 2021-08-25 [1] #> tidygraph * 1.2.0 2020-05-12 [1] #> tidyr 1.1.3 2021-03-03 [1] #> tidyselect 1.1.1 2021-04-30 [1] #> units 0.7-2 2021-06-08 [1] #> utf8 1.2.2 2021-07-24 [1] #> vctrs 0.3.8 2021-04-29 [1] #> withr 2.4.2 2021-04-18 [1] #> wk 0.5.0 2021-07-13 [1] #> xfun 0.24 2021-06-15 [1] #> yaml 2.2.1 2020-02-01 [1] #> source #> CRAN (R 4.0.3) #> CRAN (R 4.0.4) #> CRAN (R 4.0.3) #> CRAN (R 4.0.5) #> CRAN (R 4.0.4) #> CRAN (R 4.0.5) #> CRAN (R 4.0.5) #> CRAN (R 4.0.4) #> CRAN (R 4.0.4) #> CRAN (R 4.0.4) #> CRAN (R 4.0.4) #> CRAN (R 4.0.5) #> CRAN (R 4.0.5) #> CRAN (R 4.0.5) #> CRAN (R 4.0.4) #> CRAN (R 4.0.5) #> CRAN (R 4.0.4) #> CRAN (R 4.0.4) #> CRAN (R 4.0.5) #> CRAN (R 4.0.4) #> CRAN (R 4.0.3) #> CRAN (R 4.0.4) #> CRAN (R 4.0.5) #> CRAN (R 4.0.4) #> CRAN (R 4.0.4) #> CRAN (R 4.0.5) #> CRAN (R 4.0.5) #> CRAN (R 4.0.5) #> CRAN (R 4.0.4) #> CRAN (R 4.0.5) #> CRAN (R 4.0.4) #> CRAN (R 4.0.5) #> CRAN (R 4.0.5) #> CRAN (R 4.0.4) #> CRAN (R 4.0.5) #> CRAN (R 4.0.5) #> CRAN (R 4.0.4) #> Github (baddstats/polyclip@55623e8) #> CRAN (R 4.0.5) #> CRAN (R 4.0.4) #> CRAN (R 4.0.5) #> CRAN (R 4.0.5) #> CRAN (R 4.0.5) #> CRAN (R 4.0.5) #> CRAN (R 4.0.5) #> CRAN (R 4.0.5) #> CRAN (R 4.0.4) #> CRAN (R 4.0.5) #> CRAN (R 4.0.4) #> CRAN (R 4.0.4) #> Github (r-spatial/sf@7e60ab3) #> CRAN (R 4.0.4) #> Github (luukvdmeer/sfnetworks@3774b28) #> CRAN (R 4.0.5) #> CRAN (R 4.0.5) #> Github (spatstat/spatstat.data@1611456) #> CRAN (R 4.0.5) #> Github (spatstat/spatstat.linnet@2b8a23a) #> CRAN (R 4.0.4) #> CRAN (R 4.0.5) #> CRAN (R 4.0.5) #> CRAN (R 4.0.4) #> CRAN (R 4.0.5) #> CRAN (R 4.0.3) #> CRAN (R 4.0.5) #> CRAN (R 4.0.4) #> CRAN (R 4.0.4) #> CRAN (R 4.0.5) #> CRAN (R 4.0.5) #> CRAN (R 4.0.5) #> CRAN (R 4.0.5) #> CRAN (R 4.0.5) #> CRAN (R 4.0.5) #> CRAN (R 4.0.5) #> CRAN (R 4.0.3) #> #> [1] C:/Users/Utente/Documents/R/win-library/4.0 #> [2] C:/Program Files/R/R-4.0.5/library ```
marcoscarloseduardo commented 3 years ago

Hi Andrea, The bug show up you convert roxel to undirect sfn. You used "directed = TRUE" instead of "directed = FALSE". In this case, the paths are different.

Now both number are the same: 560.1045

I started yesterday to learn sfnetworks package and it's amazing! Great job!

agila5 commented 3 years ago

Ah ops, thanks. That's also written in the docs 😅