luukvdmeer / sfnetworks

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

Passing a character type column to weights in st_network_paths does not give an error #100

Closed loreabad6 closed 3 years ago

loreabad6 commented 3 years ago

Describe the bug When a character column is passed as a weights argument in st_network_paths(), it seems to be internally (by igraph) converted into a numeric vector, introducing NA values.

Reproducible example

library(sfnetworks)
library(dplyr)

(net = roxel %>% 
  as_sfnetwork() %>% 
  activate('edges') %>%
  mutate(char = sample(letters, nrow(roxel), replace = T))) 
#> # A sfnetwork with 701 nodes and 851 edges
#> #
#> # CRS:  EPSG:4326 
#> #
#> # A directed multigraph with 14 components with spatially explicit edges
#> #
#> # Edge Data:     851 x 6 (active)
#> # Geometry type: LINESTRING
#> # Dimension:     XY
#> # Bounding box:  xmin: 7.522594 ymin: 51.94151 xmax: 7.546705 ymax: 51.9612
#>    from    to name       type                                     geometry char 
#>   <int> <int> <chr>      <fct>                            <LINESTRING [°]> <chr>
#> 1     1     2 Havixbeck~ reside~    (7.533722 51.95556, 7.533461 51.95576) x    
#> 2     3     4 Pienersal~ second~ (7.532442 51.95422, 7.53236 51.95377, 7.~ k    
#> 3     5     6 Schulte-B~ reside~ (7.532709 51.95209, 7.532823 51.95239, 7~ i    
#> 4     7     8 <NA>       path    (7.540063 51.94468, 7.539696 51.94479, 7~ u    
#> 5     9    10 Welsinghe~ reside~     (7.537673 51.9475, 7.537614 51.94562) n    
#> 6    11    12 <NA>       footway (7.543791 51.94733, 7.54369 51.94686, 7.~ d    
#> # ... with 845 more rows
#> #
#> # Node Data:     701 x 1
#> # Geometry type: POINT
#> # Dimension:     XY
#> # Bounding box:  xmin: 7.522622 ymin: 51.94151 xmax: 7.546705 ymax: 51.9612
#>              geometry
#>           <POINT [°]>
#> 1 (7.533722 51.95556)
#> 2 (7.533461 51.95576)
#> 3 (7.532442 51.95422)
#> # ... with 698 more rows
net %>% st_network_paths(1, 8, weights = 'char')
#> Warning in (function (graph, from, to = V(graph), mode = c("out", "all", : NAs
#> introduced by coercion
#> # A tibble: 1 x 2
#>   node_paths edge_paths
#>   <list>     <list>    
#> 1 <int [33]> <int [32]>

Expected behavior Return an error or alternatively an informative warning when this happens.

luukvdmeer commented 3 years ago

I don't think this is a bug. This is just how igraph chose to handle character encoded weights. I would say we just rely on igraph for that and not change the behaviour.

The only thing is we need to update the following line in the routing vignette:

https://github.com/luukvdmeer/sfnetworks/blob/3cafc6a3e767cf2a01bf773c2c33665875685001/vignettes/routing.Rmd#L37

luukvdmeer commented 3 years ago

Fixed by updating the vignette