views:

48

answers:

1

I'm working on a project that surveys the condition of a road or highway using a calibrated trip computer connected to a rugged-PC. An operator keys in defect codes as they travel along a pre-defined route.

I need to show an indicator on the map screen that shows the vehicles current position, taking into account the distance data from the trip computer.

I know the exact lat lon co-ordinates at the starting point of each section of road, and the road is made up of a series of points.

The question is: how can I calculate the lat lon co-ordinates of the vehicle assuming that it has continued on the route and traveled a certain distance (e.g. 1.4km). The co-ordinates would be 'locked onto' the road line, as shown in blue on the diagram below.

Road diagram

Thanks, Alex

+1  A: 

Here is some Java-ish pseudocode, giving a solution using linear interpolation between points.

inputs: distance, points

// construct a list of segments from the points
segments = [];
for(point in points) {
  if(not first point) {
    seg = new segment(last_point, point)
    add seg to segments
  }
  last_point = point
}

// calculate current lat and lon
for(segment in segments) {
  if(distance < segment.length) {
    alpha = distance / segment.length
    lat = segment.start.lat * (1.0 - alpha) + segment.end.lat * alpha
    lon = segment.start.lon * (1.0 - alpha) + segment.end.lon * alpha
    return (lat, lon)
  } else {
    distance = distance - segment.length
  }
}

You might also want to consider spline interpolation, which could be more accurate. It will require some more maths, but the above idea can still be applied.

James
Excellent, with a slight tweak this works flawlessly. The point locks directly onto the line, so the accuracy is pretty much spot on.I had to switch the [* (1.0 - alpha)] and [* alpha] in the lat lon calculations to the end otherwise it went backwards and jumped around between each line segment.
Alex