views:

148

answers:

3

Suppose I have an array of coordinates, representing a route. I want to decompose this route so that it contains a point, say every 5 miles. How can I do that?

struct Location
{
    double latitude;
    double longitude;
};

vector<Location> route;

vector<Location> computeHigherGranularityRoute(const vector<Location>& oldRoute, double distanceDelta);

Essentially I need two functions, one that can compute the course between any two points

// Returns the course from A to B (0 -- 359.9) degrees
double getCourse(const Location& A, const Location& B);

// Returns the Location obtained from traveling from a point for a given distance along a course
Location getLocationOnCourse(const Location& start, double course, double distance);

Thanks!!

+1  A: 

The first answer on this post has the formula for calculating angles and distance from lat/long pairs.
From the distance and angle calculated here it should be possible to find a point at a specified distance along that angle.
The calculation assumes a perfectly spherical Earth so if your long/lat datum are from a specific projection other than spherical mercator then the calculation may give inaccurate results.

Hamish Smith
+1  A: 

Find the angle and distance between two adjacent points using formulae at MathWorld. Now figure out how much you need to divide the distance in order to get 5-mile chunks, and divide the angle by the same amount. Then reverse the formula at each multiple of that angle to compute the intermediate points.

This gives you the division of each segment on your original route. Repeat for each segment.

There's a degeneracy if you already have three points in a row on the same great circle -- if you care then you can check for that in a greedy manner as you go.

Eric
A: 

You can use Vincenty formula both direct and inverse for your need. The vincenty formula gives more accurate result than Great Circle Distance as it takes into consideration the Earth ellipsoidal shape.

Mohit