UrbanAnalyst / dodgr

Distances on Directed Graphs in R
https://urbananalyst.github.io/dodgr/
127 stars 16 forks source link

edge_ids differ in contracted graph vs. dodgr_to_sf() when using wt_profile = "motorcar" #216

Closed jucardwell closed 3 months ago

jucardwell commented 1 year ago

Hi,

I am working on a project that requires me to remove road segments based on a set of lat/lons. To do so, I exported a weighted streetnet as an sf object so that I could identify the appropriate segments to remove using spatial queries.

I had initially done testing using the built-in hampi streetnet to ensure that the edge_ids remain consistent between the contracted graph and the sf object, and I determined that they were. For instance, in the case below, both tests return TRUE only

library(dodgr)
library(sf)
library(tidyverse)

#general hampi streetnetwork
hampi_general <- weight_streetnet(hampi)
#contracted
contracted_general <- dodgr_contract_graph(hampi_general)
#sf version
sf_general <- dodgr_to_sf(hampi_general) %>% st_drop_geometry()

#test if edge ids in both
unique(contracted_general$edge_id %in% sf_general$edge_id)
#test if rows are identical
unique(do.call(paste0, contracted_general) %in% do.call(paste0, sf_general))

However, when trying to remove the edges from my own network, I discovered that they actually did not remain consistent. Subsequently I discovered that when using a weighting profile, edge_ids no longer remain consistent between the contracted graph and the sf object. For instance, in the case below, both tests return TRUE and FALSE

hampi_motorcar <- weight_streetnet(hampi, wt_profile = "motorcar")
contracted_motorcar <- dodgr_contract_graph(hampi_motorcar)
sf_motorcar <- dodgr_to_sf(hampi_motorcar) %>% st_drop_geometry()

#test if edge ids in both
unique(contracted_motorcar$edge_id %in% sf_motorcar$edge_id)
#test if rows are identical
unique(do.call(paste0, contracted_motorcar) %in% do.call(paste0, sf_motorcar))

However, it seems that when even using a weighting profile, the from_id and to_id remain consistent

contracted_motorcar_ids <- contracted_motorcar %>% as_tibble() %>% select(from_id, to_id)
sf_motorcar_ids <- sf_motorcar %>% select(from_id, to_id)

unique(do.call(paste0, contracted_motorcar_ids) %in% do.call(paste0, sf_motorcar_ids))
mpadge commented 3 months ago

Sorry @jucardwell that this issue slipped through the gaps for such a long time :scream:. The resolution to your problem is that "edge_id" values only mean something within one single graph, and should never be used to compare graphs. The fact that the first example generates identical edge id values should be considered an accident. (This case will only arise when graph contraction produces the exact format required for representation as sf LINESTRING objects, which is not always the case.)

jucardwell commented 3 months ago

Hi @mpadge, thanks for the response. Just for clarification, the node_ids should remain consistent?

mpadge commented 3 months ago

Yes, they're directly from OSM.