Skip to content

Direction/Route calls returning confusing values for duration + distance #58

@mrworthington

Description

@mrworthington

Correct me if I'm wrong, but I noticed yesterday while working with mb_directions() that the function is inaccurately returning values for distance + duration. Here's an example that demonstrates this by mapping the walking distance between Zilker Park + UT Austin:

Screenshot from Mapbox Directions API Demo

Screenshot 2025-01-05 at 12 05 25 PM

Screenshot from mb_directions() in your Demo Shiny App (with duration added in printout)

Added Code To Print Duration
    # Extract and display duration
    total_duration <- sum(new_route()$duration, na.rm = TRUE) / 60  # Convert seconds to minutes

    output$duration <- renderText({
      paste("Total travel time:", round(total_duration, 4), "minutes")
    })
Screenshot 2025-01-05 at 12 17 29 PM

Looking at the underlying code of this function and a few others, I've noticed that distance is divided by 1000 and duration is divided by 60. This, from what I understand in the Mapbox API Docs that say distance is returned in meters or seconds. Dividing the returned duration output by 60 and the distance by 1000 (as seen in the sample snippet below from mb_directions()) is creating an inaccurate representation of both metrics in that these functions are returning distance in kilometers and duration in minutes.

If this was intentional, I think the documentation should be updated which I can happily make a PR for or the code needs to be modified to omit those divisions by 1000 and 60 so that the returned values are in meters + seconds, as the Mapbox API docs states. Can also do a PR for this as well!

Let me know what you think!

route <- purrr::map(geoms, ~ {
          .x %>%
            sf::st_linestring() %>%
            sf::st_sfc(crs = 4326) %>%
            sf::st_sf()
        }) %>%
          purrr::reduce(rbind)

        route$distance <- .x$steps[[1]]$distance / 1000
        route$duration <- .x$steps[[1]]$duration / 60
        route$instruction <- .x$steps[[1]]$maneuver$instruction

        return(route)
      })

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions