examples/example-TransverseMercator.cpp
Go to the documentation of this file.
1 // Example of using the GeographicLib::TransverseMercator class
2 
3 #include <iostream>
4 #include <iomanip>
5 #include <exception>
7 
8 using namespace std;
9 using namespace GeographicLib;
10 
11 // Define a UTM projection for an arbitrary ellipsoid
12 class UTMalt {
13 private:
14  TransverseMercator _tm; // The projection
15  double _lon0; // Central longitude
16  double _falseeasting, _falsenorthing;
17 public:
18  UTMalt(double a, // equatorial radius
19  double f, // flattening
20  int zone, // the UTM zone + hemisphere
21  bool northp)
22  : _tm(a, f, Constants::UTM_k0())
23  , _lon0(6 * zone - 183)
24  , _falseeasting(5e5)
25  , _falsenorthing(northp ? 0 : 100e5) {
26  if (!(zone >= 1 && zone <= 60))
27  throw GeographicErr("zone not in [1,60]");
28  }
29  void Forward(double lat, double lon, double& x, double& y) {
30  _tm.Forward(_lon0, lat, lon, x, y);
31  x += _falseeasting;
32  y += _falsenorthing;
33  }
34  void Reverse(double x, double y, double& lat, double& lon) {
35  x -= _falseeasting;
36  y -= _falsenorthing;
37  _tm.Reverse(_lon0, x, y, lat, lon);
38  }
39 };
40 
41 int main() {
42  try {
43  UTMalt tm(6378388, 1/297.0, 30, true); // International ellipsoid, zone 30n
44  {
45  // Sample forward calculation
46  double lat = 40.4, lon = -3.7; // Madrid
47  double x, y;
48  tm.Forward(lat, lon, x, y);
49  cout << fixed << setprecision(0) << x << " " << y << "\n";
50  }
51  {
52  // Sample reverse calculation
53  double x = 441e3, y = 4472e3;
54  double lat, lon;
55  tm.Reverse(x, y, lat, lon);
56  cout << fixed << setprecision(5) << lat << " " << lon << "\n";
57  }
58  }
59  catch (const exception& e) {
60  cerr << "Caught exception: " << e.what() << "\n";
61  return 1;
62  }
63 }
Scalar * y
static const double lat
Definition: Half.h:150
void Forward(real lon0, real lat, real lon, real &x, real &y, real &gamma, real &k) const
Transverse Mercator projection.
void Reverse(double x, double y, double &lat, double &lon)
Header for GeographicLib::TransverseMercator class.
Array33i a
void Forward(double lat, double lon, double &x, double &y)
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Namespace for GeographicLib.
Array< double, 1, 3 > e(1./3., 0.5, 2.)
void Reverse(real lon0, real x, real y, real &lat, real &lon, real &gamma, real &k) const
Constants needed by GeographicLib
Definition: Constants.hpp:131
Exception handling for GeographicLib.
Definition: Constants.hpp:389
static const double lon
UTMalt(double a, double f, int zone, bool northp)
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:42:02