简体   繁体   中英

How to interpolate latitude and longitude coordinates

I need to find interpolation points between a set of two coordinates that lie on a line. The pseudo C# code that I was thinking of is this:

private static List<Coordinate> SampleLine(Coordinate start, Coordinate end, int Samples)
    {

        List<Coordinate> LineStringSample = new List<Coordinate>();
        // Calculate equal interval between two points 
        var diff_X = start.X - end.X; //latitudes
        var diff_Y = start.Y - end.Y; //longitudes
        var length = Math.Sqrt(diff_X * diff_X + diff_Y * diff_Y);
        var interval_X = diff_X / length;
        var interval_Y = diff_Y / length;

        Coordinate last = start;
        for (int i = 1; i <= Samples; i++)
        {
            LineStringSample.Add(new Coordinate(start.X + interval_X * i, start.Y + interval_Y * i));
        }
    }

Where for example start = (49.13512,6.4321) end = (49.13515,6.4333) Samples=1000 ,which are basically the latitude and longitude coordinates. I need to know if this is the right way of interpolating two coordinate points of a polyline, or if there is another way of doing this?

This interpolation is not exact, and errors will increase when coordinate difference grows. It might still be suitable for small lines like your example.

Better approach - find interpolation points at big circle arc. Look here in part "Intermediate point".

Formula:
 a = sin((1−f)⋅δ) / sin δ
 b = sin(f⋅δ) / sin δ
 x = a ⋅ cos φ1 ⋅ cos λ1 + b ⋅ cos φ2 ⋅ cos λ2
 y = a ⋅ cos φ1 ⋅ sin λ1 + b ⋅ cos φ2 ⋅ sin λ2
 z = a ⋅ sin φ1 + b ⋅ sin φ2
 φi = atan2(z, √x² + y²)
 λi = atan2(y, x) 
where   

f is fraction along great circle route (f=0 is point 1, f=1 is point 2),
δ is the angular distance d/R between the two points.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM