UrbanAnalyst / dodgr

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

Problem with walk, bike, tobler cost and dz calculation #219

Closed xtimbeau closed 10 months ago

xtimbeau commented 10 months ago

Hi, working with dodgr on walk and bike profile I came upon a slightly annoying issue. Using standard profile, I get very high travel times, implying very slow speeds. Looking for the source of the problem, I think there is something wrong in the dz calculation. From a silicate network, dz is calculated in sc_edge_dist as the difference between altitude of starting point and ending point. However, dz is then used in tobler cost function (using standard parameters, https://en.wikipedia.org/wiki/Tobler%27s_hiking_function) as such. But Tobler cost function (given those parameters) must rely on the slope, not on the altitude differential. So it would be preferable to caculate dz as a slope, ie. "dz" = (.vx1_z - .vx0_z)/d. I guess that using this formula would solve the issue?

sc_edge_dist <- function (graph) {

    # no visible binding notes:
    .vx0_z <- .vx1_z <- NULL

    xy0 <- as.data.frame (graph [, c (".vx0_x", ".vx0_y")])
    xy1 <- as.data.frame (graph [, c (".vx1_x", ".vx1_y")])
    graph$d <- geodist::geodist (xy0, xy1, paired = TRUE, measure = "geodesic")
    if (".vx0_z" %in% names (graph) && ".vx1_z" %in% names (graph)) {
        graph <- dplyr::mutate (graph, "dz" = .vx1_z - .vx0_z) %>%
            dplyr::select (-c (.vx0_z, .vx1_z))
    }
    return (graph)
}
mpadge commented 10 months ago

You were right @xtimbeau , thanks so much for uncovering that bug, which the commit above fixes. Now it should work as expected.