GeographicLib  1.43
TransverseMercatorProj.cpp
Go to the documentation of this file.
1 /**
2  * \file TransverseMercatorProj.cpp
3  * \brief Command line utility for transverse Mercator projections
4  *
5  * Copyright (c) Charles Karney (2008-2012) <charles@karney.com> and licensed
6  * under the MIT/X11 License. For more information, see
7  * http://geographiclib.sourceforge.net/
8  *
9  * See the <a href="TransverseMercatorProj.1.html">man page</a> for usage
10  * information.
11  **********************************************************************/
12 
13 #include <iostream>
14 #include <sstream>
15 #include <string>
16 #include <sstream>
17 #include <fstream>
21 #include <GeographicLib/DMS.hpp>
23 
24 #if defined(_MSC_VER)
25 // Squelch warnings about constant conditional expressions and potentially
26 // uninitialized local variables
27 # pragma warning (disable: 4127 4701)
28 #endif
29 
30 #include "TransverseMercatorProj.usage"
31 
32 int main(int argc, char* argv[]) {
33  try {
34  using namespace GeographicLib;
35  typedef Math::real real;
37  bool exact = true, extended = false, series = false, reverse = false;
38  real
39  a = Constants::WGS84_a(),
40  f = Constants::WGS84_f(),
41  k0 = Constants::UTM_k0(),
42  lon0 = 0;
43  int prec = 6;
44  std::string istring, ifile, ofile, cdelim;
45  char lsep = ';';
46 
47  for (int m = 1; m < argc; ++m) {
48  std::string arg(argv[m]);
49  if (arg == "-r")
50  reverse = true;
51  else if (arg == "-t") {
52  exact = true;
53  extended = true;
54  series = false;
55  } else if (arg == "-s") {
56  exact = false;
57  extended = false;
58  series = true;
59  } else if (arg == "-l") {
60  if (++m >= argc) return usage(1, true);
61  try {
62  DMS::flag ind;
63  lon0 = DMS::Decode(std::string(argv[m]), ind);
64  if (ind == DMS::LATITUDE)
65  throw GeographicErr("Bad hemisphere");
66  if (!(lon0 >= -540 && lon0 < 540))
67  throw GeographicErr("Bad longitude");
68  lon0 = Math::AngNormalize(lon0);
69  }
70  catch (const std::exception& e) {
71  std::cerr << "Error decoding argument of " << arg << ": "
72  << e.what() << "\n";
73  return 1;
74  }
75  } else if (arg == "-k") {
76  if (++m >= argc) return usage(1, true);
77  try {
78  k0 = Utility::num<real>(std::string(argv[m]));
79  }
80  catch (const std::exception& e) {
81  std::cerr << "Error decoding argument of " << arg << ": "
82  << e.what() << "\n";
83  return 1;
84  }
85  } else if (arg == "-e") {
86  if (m + 2 >= argc) return usage(1, true);
87  try {
88  a = Utility::num<real>(std::string(argv[m + 1]));
89  f = Utility::fract<real>(std::string(argv[m + 2]));
90  }
91  catch (const std::exception& e) {
92  std::cerr << "Error decoding arguments of -e: " << e.what() << "\n";
93  return 1;
94  }
95  m += 2;
96  } else if (arg == "-p") {
97  if (++m == argc) return usage(1, true);
98  try {
99  prec = Utility::num<int>(std::string(argv[m]));
100  }
101  catch (const std::exception&) {
102  std::cerr << "Precision " << argv[m] << " is not a number\n";
103  return 1;
104  }
105  } else if (arg == "--input-string") {
106  if (++m == argc) return usage(1, true);
107  istring = argv[m];
108  } else if (arg == "--input-file") {
109  if (++m == argc) return usage(1, true);
110  ifile = argv[m];
111  } else if (arg == "--output-file") {
112  if (++m == argc) return usage(1, true);
113  ofile = argv[m];
114  } else if (arg == "--line-separator") {
115  if (++m == argc) return usage(1, true);
116  if (std::string(argv[m]).size() != 1) {
117  std::cerr << "Line separator must be a single character\n";
118  return 1;
119  }
120  lsep = argv[m][0];
121  } else if (arg == "--comment-delimiter") {
122  if (++m == argc) return usage(1, true);
123  cdelim = argv[m];
124  } else if (arg == "--version") {
125  std::cout << argv[0] << ": GeographicLib version "
126  << GEOGRAPHICLIB_VERSION_STRING << "\n";
127  return 0;
128  } else
129  return usage(!(arg == "-h" || arg == "--help"), arg != "--help");
130  }
131 
132  if (!ifile.empty() && !istring.empty()) {
133  std::cerr << "Cannot specify --input-string and --input-file together\n";
134  return 1;
135  }
136  if (ifile == "-") ifile.clear();
137  std::ifstream infile;
138  std::istringstream instring;
139  if (!ifile.empty()) {
140  infile.open(ifile.c_str());
141  if (!infile.is_open()) {
142  std::cerr << "Cannot open " << ifile << " for reading\n";
143  return 1;
144  }
145  } else if (!istring.empty()) {
146  std::string::size_type m = 0;
147  while (true) {
148  m = istring.find(lsep, m);
149  if (m == std::string::npos)
150  break;
151  istring[m] = '\n';
152  }
153  instring.str(istring);
154  }
155  std::istream* input = !ifile.empty() ? &infile :
156  (!istring.empty() ? &instring : &std::cin);
157 
158  std::ofstream outfile;
159  if (ofile == "-") ofile.clear();
160  if (!ofile.empty()) {
161  outfile.open(ofile.c_str());
162  if (!outfile.is_open()) {
163  std::cerr << "Cannot open " << ofile << " for writing\n";
164  return 1;
165  }
166  }
167  std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
168 
169  const TransverseMercator& TMS =
170  series ? TransverseMercator(a, f, k0) : TransverseMercator(1, 0, 1);
171 
172  const TransverseMercatorExact& TME =
173  exact ? TransverseMercatorExact(a, f, k0, extended)
174  : TransverseMercatorExact(1, real(0.1), 1, false);
175 
176  // Max precision = 10: 0.1 nm in distance, 10^-15 deg (= 0.11 nm),
177  // 10^-11 sec (= 0.3 nm).
178  prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
179  std::string s;
180  int retval = 0;
181  std::cout << std::fixed;
182  while (std::getline(*input, s)) {
183  try {
184  std::string eol("\n");
185  if (!cdelim.empty()) {
186  std::string::size_type m = s.find(cdelim);
187  if (m != std::string::npos) {
188  eol = " " + s.substr(m) + "\n";
189  s = s.substr(0, m);
190  }
191  }
192  std::istringstream str(s);
193  real lat, lon, x, y;
194  std::string stra, strb;
195  if (!(str >> stra >> strb))
196  throw GeographicErr("Incomplete input: " + s);
197  if (reverse) {
198  x = Utility::num<real>(stra);
199  y = Utility::num<real>(strb);
200  } else
201  DMS::DecodeLatLon(stra, strb, lat, lon);
202  std::string strc;
203  if (str >> strc)
204  throw GeographicErr("Extraneous input: " + strc);
205  real gamma, k;
206  if (reverse) {
207  if (series)
208  TMS.Reverse(lon0, x, y, lat, lon, gamma, k);
209  else
210  TME.Reverse(lon0, x, y, lat, lon, gamma, k);
211  *output << Utility::str(lat, prec + 5) << " "
212  << Utility::str(lon, prec + 5) << " "
213  << Utility::str(gamma, prec + 6) << " "
214  << Utility::str(k, prec + 6) << eol;
215  } else {
216  if (series)
217  TMS.Forward(lon0, lat, lon, x, y, gamma, k);
218  else
219  TME.Forward(lon0, lat, lon, x, y, gamma, k);
220  *output << Utility::str(x, prec) << " "
221  << Utility::str(y, prec) << " "
222  << Utility::str(gamma, prec + 6) << " "
223  << Utility::str(k, prec + 6) << eol;
224  }
225  }
226  catch (const std::exception& e) {
227  *output << "ERROR: " << e.what() << "\n";
228  retval = 1;
229  }
230  }
231  return retval;
232  }
233  catch (const std::exception& e) {
234  std::cerr << "Caught exception: " << e.what() << "\n";
235  return 1;
236  }
237  catch (...) {
238  std::cerr << "Caught unknown exception\n";
239  return 1;
240  }
241 }
static T AngNormalize(T x)
Definition: Math.hpp:445
An exact implementation of the transverse Mercator projection.
GeographicLib::Math::real real
Definition: GeodSolve.cpp:32
Header for GeographicLib::Utility class.
void Forward(real lon0, real lat, real lon, real &x, real &y, real &gamma, real &k) const
Header for GeographicLib::TransverseMercatorExact class.
Transverse Mercator projection.
Header for GeographicLib::TransverseMercator class.
int main(int argc, char *argv[])
static int extra_digits()
Definition: Math.hpp:185
static void DecodeLatLon(const std::string &dmsa, const std::string &dmsb, real &lat, real &lon, bool swaplatlong=false)
Definition: DMS.cpp:246
static Math::real Decode(const std::string &dms, flag &ind)
Definition: DMS.cpp:28
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
void Reverse(real lon0, real x, real y, real &lat, real &lon, real &gamma, real &k) const
static std::string str(T x, int p=-1)
Definition: Utility.hpp:276
Header for GeographicLib::EllipticFunction class.
void Forward(real lon0, real lat, real lon, real &x, real &y, real &gamma, real &k) const
void Reverse(real lon0, real x, real y, real &lat, real &lon, real &gamma, real &k) const
static int set_digits(int ndigits=0)
Definition: Utility.cpp:48
Exception handling for GeographicLib.
Definition: Constants.hpp:382
Header for GeographicLib::DMS class.