vlarmet / cppRouting

Algorithms for Routing and Solving the Traffic Assignment Problem
109 stars 9 forks source link

Support sfnetworks #19

Open latot opened 10 months ago

latot commented 10 months ago

Hi all, actually cppRouting has a lot of tools to internally handle some changes like contraction hierarchies.

At the same time, rn sfnetworks is a popular and very useful library to work on, I think would be a good idea have a function to convert sfnetwork graph to cpprouting graph.

I already have a function for it, but would be good to have for it some extra tests and I don't fully understand all the cppRouting graph structure to construct them for every case that may be of interestest.

The function I have:

sfnet2cppRouting <- ? function(
    network = NULL ? geotypes::sfnetworks_sfnetwork(null_ok = FALSE),
    weight = NULL ? typed::Character(1, anyNA = FALSE, null_ok = FALSE)) {
  cppRouting_graph <- cppRouting::makegraph(
    network %.>%
      sfnetworks::activate(., "edges") %.>%
      sf::st_as_sf(.) %.>%
      sf::st_drop_geometry(.) %.>%
      as.data.frame(.) %.>%
      dplyr::transmute(., .data$from, .data$to, cost = .data[[weight]]),
    directed = igraph::is_directed(network)
  )
  # }
  original_nodes <- network %.>%
    sfnetworks::activate(., "nodes") %.>%
    sf::st_as_sf(.)
  if (nrow(original_nodes) != cppRouting_graph$nbnode) {
    stop("The net was simplified by cppRouting, the nodes assignation can be broken.")
  }
  cppRouting_graph
}

Personally, the latest section, the stop is a need, happened, very low times, that sadly I was not able to reproduce, some nodes disappear using makegraph, so it would helps to detect that cases, has been some time from when that happened, maybe is fixed, sadly I was not able to found any issue here to check it.

Thx!

vlarmet commented 10 months ago

For your information, the next (big) feature is to convert any spatial dataset (shp, geojson, sf, osm.pbf ...) into a routable network.