Menu

GeodesicLine.Position() seems to create wrong s12 values

Help
Master
2022-04-18
2022-04-19
  • Master

    Master - 2022-04-18

    This is a followup question of this one where I try to port a python script to c++. long story short, after running my c++ port which is given below, the distance reported is off by a huge margin!
    This function tries to create a list of intermediate waypoints between the given source and destination gps locations.

    struct Waypoints
    {
        std::vector<std::tuple<double, double>> gps_coordinate;
        double total_distance;
    };
    Waypoints create_waypoints(double lat1, double lon1, double lat2, double lon2, double distance, bool display_result = false)
    {
        std::vector<std::tuple<double, double>> waypoints;
        Geodesic geod(Constants::WGS84_a(), Constants::WGS84_f());
        auto l = GeographicLib::Geodesic::WGS84().InverseLine(lat1, lon1, lat2, lon2);
        auto parts = int(std::ceil(l.Distance() / distance));
        auto total_distance = l.Distance();
    
        std::cout.setf(std::ios::fixed, std::ios::floatfield);
        std::cout.precision(4);
        for (int i = 0; i < parts + 1; i++)
        {
            if (display_result)
                if (i == 0)
                    printf("latitude  longitude  azimuth  distance\n");
    
            auto s = std::min(distance * i, (double)l.Distance());
            double lat2, lon2, azi2, m12, M12, M21, s12;
            auto g = l.Position(s, lat2, lon2, azi2, m12, M12, M21, s12);
            waypoints.emplace_back(std::make_tuple(lat2, lon2));
    
            if (display_result)
                std::cout << lat2 << "   "
                          << lon2 << "   "
                          << azi2 << "   "
                          << s12 << "\n";
        }
        return {waypoints,total_distance};
    }
    
    int main()
    {
        auto r = create_waypoints(25.0, 25.0, 25.001, 25.001, 20, true);
        std::cout<<"\ntotal_distance: "<<r.total_distance<<std::endl;
        return 0;
    }
    

    results in :

    latitude  longitude  azimuth  distance
    25.0000   25.0000   42.3434   -0.0000
    25.0001   25.0001   42.3434   39806275.7158
    25.0003   25.0003   42.3435   79612836.5627
    25.0004   25.0004   42.3435   119419682.5419
    25.0005   25.0005   42.3436   159226813.6544
    25.0007   25.0007   42.3436   199034229.9013
    25.0008   25.0008   42.3437   238841931.2836
    25.0009   25.0009   42.3438   278649917.8026
    25.0010   25.0010   42.3438   298298287.5592
    
    total_distance: 149.8715
    

    Is this a bug or am I missing something? by the way I'm using the latest version(1.52).
    Thanks a lot in advance

     
  • Charles Karney

    Charles Karney - 2022-04-18

    The last argument in you call to Position returns the area, which I'm careful to spell with a capital S, S12 (in m^2 so it's usually a big number). The distance is just the first argument s, of course. You can also then make the simpler call l.Position(s, lat2, lon2, azi). Let me know if this clears up your problem.

     

    Last edit: Charles Karney 2022-04-18
  • Master

    Master - 2022-04-19

    Thanks a lot. Yes that pretty much cleared everything.
    God bless

     

Anonymous
Anonymous

Add attachments
Cancel