thomasp85 / tidygraph

A tidy API for graph manipulation
https://tidygraph.data-imaginist.com
Other
546 stars 61 forks source link

add_vertices error inconsistent between networks using tidygraph and sfnetworks #146

Closed benjaminhlina closed 3 years ago

benjaminhlina commented 3 years ago

This issue seems to be somewhat related to #89 . I'm currently trying to use luukvdmeer/sfnetworks which I understand to allow for sf objects to be passed to tidygraph and igraph like functions to create spatial networks. I have opened the issue on sfnetwork, luukvdmeer/sfnetworks#173 as well but thought I'd make one here as it seems like a tidygraph issue. If this is not appropriate at the moment I will remove the issue. I'm using sfnetworks to determine seasonal fish movement within a mulitbasin lake. Depending on the fish, some networks work and others do not, and the ones that do not always pop up with the following error.

Error in add_vertices(gr, nrow(nodes) - gorder(gr)) : 
  At type_indexededgelist.c:369 : cannot add negative number of vertices, Invalid value

I cannot seem to figure out why the error occurs, but based on #89 this seems to be an issue with the name of the column of the node data which doesn't seem to add up to me, considering mine is called rec_group. If this is more a stackoverflow question then I can move this over there, just thought I'd create an issue report as the error message seems to be related to tidygraph issue #89 . I have created the following reprex with the following data: I have provided dropbox links to the example data.

I've wondered if this is an issue surrounding the fact that the edge linestring geometry, does not match exactly the node point geometry, hence why I use force = TRUE within my sfnetwork() call. This is due to fact that separately I've done a cost distance analysis between each node which takes in account the lake shape, as I don't want edges on land, and have created a separate sf object with those linestrings and shortest paths. I then create an edge list (to-from) per fish per season and then join this cost distance sf object to each unique to-from combination. This results in the start and ends of the edge linestring geometry being just slightly different than the node sf object geometry. However, I don't believe this is the issue as if I make a straight linestring, across land, for example from node a to b, that matches the node lat and long, I still get the issue. You'll also notice that there are a some empty linestrings due to self loops within the network, not sure if this also poses a problem.

Links to network for fish 05550 that succeeds: edges_sf_scd nodes_sf_scd

edges_sf_scd <- readr::read_rds(file = "edges_sf_scd.rds")

nodes_sf_scd <- readr::read_rds(file = "nodes_sf_scd.rds")

net_scd <- sfnetwork(nodes = nodes_sf_scd, 
                     edges = edges_sf_scd, 
                     directed = TRUE, 
                     edges_as_lines = TRUE,
                     node_key = "rec_group",
                     force = TRUE)

net_scd
str(net_scd)

Links to network for fish 05802 that fails : edges_sf_fail nodes_sf_fail

edges_sf_fail <- readr::read_rds(file = "edges_sf_fail.rds")

nodes_sf_fail <- readr::read_rds(file = "nodes_sf_fail.rds")

net_fail <- sfnetwork(nodes = nodes_sf_fail, 
                      edges = edges_sf_fail, 
                      directed = TRUE, 
                      edges_as_lines = TRUE,
                      node_key = "rec_group",
                      force = TRUE)

Error in add_vertices(gr, nrow(nodes) - gorder(gr)) : 
  At type_indexededgelist.c:369 : cannot add negative number of vertices, Invalid value