luukvdmeer / sfnetworks

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

Something wrong with calculating shortest paths #258

Closed SpatLyu closed 10 months ago

SpatLyu commented 10 months ago

Describe the bug When I calculating shortest paths based on point sf and linestring sf, st_network_paths can't get the path and it warns me In shortest_paths(x, from, to, weights = weights, output = "both", : At core/paths/dijkstra.c:446 : Couldn't reach some vertices.

Reproducible example Sorry, i'm new to the reprex package,so i use the traditional way to give anexample~

the example data is here

library(sf)
library(tidygraph)
library(tidyverse)
library(sfnetworks)

plan = read_sf('demo.gpkg',layer='plan') 
road = read_sf('demo.gpkg',layer='road')

net = road %>% 
  as_sfnetwork(directed = FALSE) %>%
  activate("edges") %>%
  mutate(weight = edge_length())

paths = st_network_paths(net, from = plan[1,], to = plan[3,], weights = "weight")
paths

image

R Session Info Include session info of your session, with the function sessionInfo().

R version 4.3.1 (2023-06-16 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)

Matrix products: default

locale:
[1] LC_COLLATE=Chinese (Simplified)_China.utf8  LC_CTYPE=Chinese (Simplified)_China.utf8   
[3] LC_MONETARY=Chinese (Simplified)_China.utf8 LC_NUMERIC=C                               
[5] LC_TIME=Chinese (Simplified)_China.utf8    

time zone: Asia/Shanghai
tzcode source: internal

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

other attached packages:
 [1] sfnetworks_0.6.3 lubridate_1.9.2  forcats_1.0.0    stringr_1.5.0    dplyr_1.1.3     
 [6] purrr_1.0.2      readr_2.1.4      tidyr_1.3.0      tibble_3.2.1     ggplot2_3.4.3   
[11] tidyverse_2.0.0  tidygraph_1.2.3  sf_1.0-14       

loaded via a namespace (and not attached):
 [1] gtable_0.3.4           spatstat.sparse_3.0-2  lattice_0.21-8        
 [4] tzdb_0.4.0             vctrs_0.6.3            tools_4.3.1           
 [7] spatstat.utils_3.0-3   generics_0.1.3         goftest_1.2-3         
[10] proxy_0.4-27           fansi_1.0.4            pkgconfig_2.0.3       
[13] sfheaders_0.4.3        Matrix_1.6-1.1         KernSmooth_2.23-22    
[16] lifecycle_1.0.3        compiler_4.3.1         deldir_1.0-9          
[19] munsell_0.5.0          spatstat.linnet_3.1-1  spatstat.explore_3.2-3
[22] class_7.3-22           pillar_1.9.0           crayon_1.5.2          
[25] classInt_0.4-10        spatstat.model_3.2-6   lwgeom_0.2-13         
[28] rpart_4.1.19           abind_1.4-5            nlme_3.1-163          
[31] spatstat.geom_3.2-5    tidyselect_1.2.0       stringi_1.7.12        
[34] splines_4.3.1          polyclip_1.10-4        grid_4.3.1            
[37] colorspace_2.1-0       cli_3.6.1              magrittr_2.0.3        
[40] utf8_1.2.3             e1071_1.7-13           spatstat.data_3.0-1   
[43] withr_2.5.0            tensor_1.5             scales_1.2.1          
[46] timechange_0.2.0       igraph_1.5.1           hms_1.1.3             
[49] mgcv_1.9-0             rlang_1.1.1            Rcpp_1.0.11           
[52] spatstat.random_3.1-6  glue_1.6.2             DBI_1.1.3             
[55] rstudioapi_0.15.0      R6_2.5.1               units_0.8-4 
SpatLyu commented 10 months ago

I have solved it as the problem of source data. I use ArcPy's SplitLine_management to Split Line At Vertices, then rerun the code and the warning disappeared, I also got the result I wanted! Record it here , hoping to help friends with the same problem!