| Forum | GitHub | Maps | Blog

Offset of snapped point to an edge in map matching result



I’m using map matching to snap points to a road network. I would like to find the projection of the snapped point (matched GpxEntry) to a matched edge as an offset. How can I do this?



I am using the map matching query results, locating the closest edge for each snapped points and using the way geometry to iterate through the points and summing up the offset in meters for each point uip to the wayIndex.

I am finding though that, for a trajectory where point coordinates are monotonically increasing (direction SW to NE, for instance), the snapped point coordinates are smaller than the coordinate associated with the wayIndex. I would have expected the opposite since the snapped coordinate should be the projection of the original GPX point onto the line segment represented by wayGeom(wayIdx) and wayGeom(wayIdx+1). I’m not exactly sure how to interpret this.

wayGeom=(38.921759029964186,-76.95073974022432), (38.92168601427271,-76.95053596684045)

snappedLat = 38.921750408096365
snappedLon = -76.95071567817484

Here is my code to calculate the offset of a snapped point within an OSM way:

def calculateOffsetMeters(res: QueryResult): Double = {

val wayIdx = res.getWayIndex
val snappedLat = res.getSnappedPoint.getLat
val snappedLon = res.getSnappedPoint.getLon
val snappedPos = res.getSnappedPosition

// get closest edge to query point
val closestEdge = res.getClosestEdge
val edgeDist = closestEdge.getDistance

// fetch point list associated with way
val wayGeom = closestEdge.fetchWayGeometry(3)

// sum up distances to way index inclusive
var sumDist = 0.0
for (idx <- 1 to wayIdx) {
  val prevLat = wayGeom.getLat(idx-1)
  val prevLon = wayGeom.getLon(idx-1)
  val currLat = wayGeom.getLat(idx)
  val currLon = wayGeom.getLon(idx)
  val offsetOnEdgeMeters = CalcUtil.distanceInMeters(prevLat, prevLon, currLat, currLon)
  sumDist += offsetOnEdgeMeters

if (snappedPos != Position.EDGE) {
  // done
  return sumDist

//val coords = toLineString(wayGeom, endIdx)

// get distance in meters of snapped point from start of closest edge
val prevLat = wayGeom.getLat(wayIdx)
val prevLon = wayGeom.getLon(wayIdx)
val offsetOnClosestEdge = CalcUtil.distanceInMeters(prevLat, prevLon,
  snappedLat, snappedLon)

// get total distance
sumDist += offsetOnClosestEdge

return sumDist


Any help would be greatly appreciated.