Adding snapping distances to route results#136
Adding snapping distances to route results#136e-kotov wants to merge 9 commits intoriatelab:masterfrom
Conversation
… source/destination snapping distances
Expose OSRM waypoint snapping distances in route results: add `src_snapping_distance` and `dst_snapping_distance` fields to the returned sf object, attach full snapped waypoints as a `snapping` attribute, and include snapping distances in source/destination coordinate formatting. Update documentation and tests accordingly.
…e documentation Convert the raw distance in the snapping waypoints from meters to kilometers, rename it to `snapping_distance`, and update documentation and tests accordingly.
There was a problem hiding this comment.
Pull request overview
This PR exposes OSRM “snapping” (distance from input point to the snapped network point) directly in osrmRoute, osrmTable, and osrmTrip results so users don’t need separate osrmNearest calls to quantify point displacement.
Changes:
osrmRoute: addssrc_snapping_distance/dst_snapping_distancecolumns and asnappingattribute containing waypoint snap metadata.osrmTable: addsmeasure = "total_distance"and includes snapping distances insources/destinations.osrmTrip: returns snappedwaypoints(sf POINT) withsnapping_distance.
Reviewed changes
Copilot reviewed 10 out of 11 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| man/osrmTrip.Rd | Documents new waypoints element for trip results. |
| man/osrmTable.Rd | Updates docs for new snapping-inclusive distance measure/output (currently inconsistent with implementation). |
| man/osrmRoute.Rd | Documents new snapping distance columns and snapping attribute. |
| R/osrmTrip.R | Constructs waypoint snapping distances and returns them as sf points per trip. |
| R/osrmTable.R | Implements measure="total_distance" and computes total_distances including snapping. |
| R/osrmRoute.R | Adds snapping distance columns and stores full waypoint snap info as an attribute. |
| R/utils.R | Adds snapping_distance fields to coord_format() outputs. |
| inst/tinytest/test_osrmTrip.R | Adds assertions for returned waypoints and snapping_distance. |
| inst/tinytest/test_osrmTable.R | Adds coverage for measure="total_distance" and expected arithmetic. |
| inst/tinytest/test_osrmRoute.R | Updates expectations for new columns + snapping attribute. |
| inst/tinytest/coord_format_out.rds | Updates test fixture for coordinate formatting output. |
Comments suppressed due to low confidence (1)
R/osrmRoute.R:225
rosfis reprojected back to the input CRS whenoprjis set, but the newly addedsnappingattribute remains in the OSRM response coordinate space (EPSG:4326). This makes the route geometry CRS and waypoint coordinates inconsistent for sf inputs in other CRSs. Consider transforming the snapped waypoint locations tooprjas well (e.g., store them as ansfPOINT object likeosrmTripdoes), or explicitly document that thesnappingattribute is always WGS84.
snapping <- res$waypoints
snapping$snapping_distance <- snapping$distance / 1000
snapping$distance <- NULL
attr(rosf, "snapping") <- snapping
# prj
if (!is.na(oprj)) {
rosf <- st_transform(rosf, oprj)
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
Hello @e-kotov , For For For |
Good call. I agree attribute is very awkward. I just was not sure if you would want to alter the
Sure, will do.
But what if someone is using
Ok, will do. |
Ok you're right, let's keep it where it is then. |
OSRM snaps input coordinates to the nearest road to calculate a route. Previously, the package didn't show how far the points were moved unless you made extra calls to
osrmNearestand/or pre-snapped the points before routing.I updated
osrmRoute,osrmTable, andosrmTripto include these snapping distances directly in the output.osrmRoutenow includessrc_snapping_distanceanddst_snapping_distancecolumns (in km). For routes with multiple points, the full metadata for every point is stored in asnappingattribute, which can be accessed withattr(res, "snapping").osrmTablenow supportsmeasure = "total_distance". This returns a matrix where the snapping distances of the source and destination are already added to the network distance.osrmTripnow includes awaypointselement in the returned list, showing thesnapping_distancefor every stop in the trip.These changes make it easier to see the total distance without making additional API calls as all of the information is already in the response anyway.