When to_spatial_contracted is used to merge clusters of nodes, with the clusters indicated by a grouping variable that is not in ascending order, the geometries of some nodes are incorrectly assigned to other nodes. This results in
nodes located far from the ends of the spatial edges with which they are associated,
node attributes associated with locations far from those to which they correspond.
Only nodes that are in a single-node cluster appear to be assigned incorrect geometries, though such a node may be assigned the geometry of a node in a multi-node cluster. Internally, I imagine this is a result of the automatic ordering conducted by group_by, perhaps leading to a mismatch at l115 of morphers.R between the ordering of nodes in all_group_idxs (from group_indices(...) at l113) and that required for new_node_geoms to correspond to new_nodes (from as_tbl_graph(contract(...)) at l137). The easiest fix may be to sort the tibble of node data by the grouping variable[s] at the start of the process.
The ordering of the tibble of nodes should not affect the result of to_spatial_contracted, so that the two sets of output above should be identical (perhaps up to permutation of rows).
Nodes' attributes should remain associated with their locations, so that st_as_sf(network, "nodes")$y_coord should remain equal to st_coordinates(network, "nodes")[,"Y"] in the above.
Nodes should remain located at the ends of their edges, so that line 2 should lead to the location of point 3, in the above.
Describe the bug
When
to_spatial_contracted
is used to merge clusters of nodes, with the clusters indicated by a grouping variable that is not in ascending order, the geometries of some nodes are incorrectly assigned to other nodes. This results inOnly nodes that are in a single-node cluster appear to be assigned incorrect geometries, though such a node may be assigned the geometry of a node in a multi-node cluster. Internally, I imagine this is a result of the automatic ordering conducted by
group_by
, perhaps leading to a mismatch at l115 ofmorphers.R
between the ordering of nodes inall_group_idxs
(fromgroup_indices(...)
at l113) and that required fornew_node_geoms
to correspond tonew_nodes
(fromas_tbl_graph(contract(...))
at l137). The easiest fix may be to sort the tibble of node data by the grouping variable[s] at the start of the process.Reproducible example
Created on 2023-05-30 with reprex v2.0.2
Expected behavior
to_spatial_contracted
, so that the two sets of output above should be identical (perhaps up to permutation of rows).st_as_sf(network, "nodes")$y_coord
should remain equal tost_coordinates(network, "nodes")[,"Y"]
in the above.R Session Info
Edit: example changed to illustrate the possibility of a node's location becoming divorced from its edges', and details added to expected behaviour.