简体   繁体   中英

Intermediate points between 2 geographic coordinates

I am trying to develop an algorithm that involves normalizing GPS coordinates (latitude/longitude). That means, that being given two points A (lat1,lon1) and B(lat2,lon2) I would like to insert a point C that is linear with AB (same arc) and is placed at a specific distance from A and B (eg: A to B distance is 0.5km and I want point C to be at 0.1 km from A, on the AB arc). How can I calculate the coordinates for point C? For the purpose given, it is enough to approximate Earth as a perfect spherical object. I have found this article, but it gives the formula for midpoint only (and I don't fully understand it, in order to adapt). midpoint between two latitude and longitude Thank you.

Edit: I tried this but it gives wrong answers

public static void normalizedPoint(double lat1, double lon1, double lat2, double lon2, double dist){
        double constant=Math.PI/180;
        double angular = dist/6371;
        double a = Math.Sin( 0* angular )/Math.Sin(angular);
        double b = Math.Sin(1*angular)/Math.Sin(angular);
        double x = a * Math.Cos(lat1) * Math.Cos(lon1) + b * Math.Cos(lat2) * Math.Cos(lon2);
        double y = a * Math.Cos(lat1) * Math.Sin(lon1) + b * Math.Cos(lat2) * Math.Sin(lon2);
        double z = a * Math.Sin(lat1) + b * Math.Sin (lon2);
        double lat3 = Math.Atan2(z, Math.Sqrt( x*x + y*y ));
        double lon3 = Math.Atan2(y, x);
        Console.WriteLine(lat3/constant + " " + lon3/constant );
    }

As far as I understood the original formulas this should return one of the 2 original points, but it does not(because the fraction used is 1). Also the variable dist is the distance from the 2 points and is properly calculated (checked with the same website).

Edit 2: I am providing as inputs coordinates for 2 geographic points (lat1, lon1, lat2 lon2) and the distance between them. I'm trying to get an intermediary point (lat3,lon3).

As I point out in an answer on the linked to question, you need to change all of your inputs to use radians rather than degrees .

I believe you also had an error for z where you used lon2 rather than lat2 .

With those corrections, I get the answer you're seeking:

    public static void normalizedPoint(double lat1, double lon1,
                                       double lat2, double lon2,
                                       double dist)
    {
        double constant = Math.PI / 180;
        double angular = dist / 6371;
        double a = Math.Sin(0 * angular) / Math.Sin(angular);
        double b = Math.Sin(1 * angular) / Math.Sin(angular);
        double x = a * Math.Cos(lat1* constant) * Math.Cos(lon1* constant) + 
                   b * Math.Cos(lat2* constant) * Math.Cos(lon2* constant);
        double y = a * Math.Cos(lat1* constant) * Math.Sin(lon1* constant) + 
                   b * Math.Cos(lat2* constant) * Math.Sin(lon2* constant);
        double z = a * Math.Sin(lat1* constant) + b * Math.Sin(lat2* constant);
        double lat3 = Math.Atan2(z, Math.Sqrt(x * x + y * y));
        double lon3 = Math.Atan2(y, x);
        Console.WriteLine(lat3 / constant + " " + lon3 / constant);
    }

Of course, the above can be vastly simplified by only converting angles ones, avoiding repeated calculations of the same Sin / Cos values, etc.

Calling:

normalizedPoint(47.20761, 27.02185, 47.20754, 27.02177, 1);

I get the output:

47.20754 27.02177

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