Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mrworthington opened this issue Jan 5, 2025 · 0 comments

Comments

@mrworthington
Copy link

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)
      })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant