2

What I am trying to do is convert two points in spherical coordinates to geographic coordinates, in order to them make use of the vincenty distance function, in order to accurately measure the distance between the two points on a unit sphere.

The following code fails to transform a pair of spherical points to a pairs of geographic ones, returning inf values for the elements in p1_g and p2_g.

Any suggestion of what I am doing wrong is much appreciated.

VectorXd p1(2) ;
VectorXd p2(2) ;
p1 << -2.35619, 0.955317 ;
p2 << 1.47275, 2.53697 ;

namespace bg = boost::geometry;

typedef boost::geometry::srs::spheroid<double> SpheroidType;
SpheroidType spheriod(1.0,1.0);
typedef boost::geometry::strategy::distance::vincenty<SpheroidType>
    VincentyStrategy;
VincentyStrategy vincenty(spheriod);


bg::model::point<double, 2, bg::cs::spherical<bg::radian>> p1_s(p1(0), p1(1));
bg::model::point<double, 2, bg::cs::spherical<bg::radian>> p2_s(p2(0), p2(1));

bg::model::point<double, 2, bg::cs::geographic<bg::radian> > p1_g;
bg::model::point<double, 2, bg::cs::geographic<bg::radian> > p2_g;
bg::transform(p1_s, p1_g, vincenty);
bg::transform(p2_s, p2_g, vincenty);

auto dist = bg::distance(p1_g, p2_g, vincenty);
oracle3001
  • 1,090
  • 19
  • 31
  • Can't repro. Also, I understand little about the coordinate systems and transformation strategy. So, I can't tell what would be correct use/outcome. See: https://wandbox.org/permlink/n3Io7YwC19JI8zJz (boost 1.63) – sehe May 21 '19 at 05:33
  • However, it seems that the same code doesn't compile anymore with boost 1.64 or higher: https://wandbox.org/permlink/jvqcZwiAfhbUGPN0 it's complaining about the strategy used – sehe May 21 '19 at 05:34

1 Answers1

1

You appear to be confused about spheres and spheroids.

A sphere is effectively a perfectly round ball. While a spheroid is a sphere that has been squashed (or extended) along an axis, see: https://en.wikipedia.org/wiki/Spheroid.
Another name for spheroid is ellipsoid. The best known spheroid is the WGS-84 spheroid used by GPS systems.

Distances between points on a sphere can be calculated relatively simply using the haversine equation, while distances between points on a spheroid require complicated equations such as Vincenty's or (the more accurate) Karney's equations.

To calculate distances on a unit sphere, simply use the boost haversine strategy and then multiply by the radius to convert the distance from radians to your desired units. The non-Cartesian distance example here shows it being performed with coordinates in degrees.

kenba
  • 4,303
  • 1
  • 23
  • 40
  • I was under the impression that the haversine approach fails when points are located at antipodal locations (which in my use case is a possibility). Hence, why I wantd to use the vincenty approach. – oracle3001 May 21 '19 at 12:34
  • The haversine equation is simply spherical trigonometry and works everywhere. The Andoyer strategy has an issue with antipodal points, see: [Coordinate System](https://www.boost.org/doc/libs/1_70_0/libs/geometry/doc/html/geometry/design.html#geometry.design.coordinate_system) and Vincenty isn't great with antipodal points either. The second answer to [this](https://stackoverflow.com/questions/38248046/is-the-haversine-formula-or-the-vincentys-formula-better-for-calculating-distan/38663071) question, explains it far better than me. – kenba May 22 '19 at 05:12