GeographicLib  1.39
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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
126  << argv[0] << ": GeographicLib version "
127  << GEOGRAPHICLIB_VERSION_STRING << "\n";
128  return 0;
129  } else
130  return usage(!(arg == "-h" || arg == "--help"), arg != "--help");
131  }
132 
133  if (!ifile.empty() && !istring.empty()) {
134  std::cerr << "Cannot specify --input-string and --input-file together\n";
135  return 1;
136  }
137  if (ifile == "-") ifile.clear();
138  std::ifstream infile;
139  std::istringstream instring;
140  if (!ifile.empty()) {
141  infile.open(ifile.c_str());
142  if (!infile.is_open()) {
143  std::cerr << "Cannot open " << ifile << " for reading\n";
144  return 1;
145  }
146  } else if (!istring.empty()) {
147  std::string::size_type m = 0;
148  while (true) {
149  m = istring.find(lsep, m);
150  if (m == std::string::npos)
151  break;
152  istring[m] = '\n';
153  }
154  instring.str(istring);
155  }
156  std::istream* input = !ifile.empty() ? &infile :
157  (!istring.empty() ? &instring : &std::cin);
158 
159  std::ofstream outfile;
160  if (ofile == "-") ofile.clear();
161  if (!ofile.empty()) {
162  outfile.open(ofile.c_str());
163  if (!outfile.is_open()) {
164  std::cerr << "Cannot open " << ofile << " for writing\n";
165  return 1;
166  }
167  }
168  std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
169 
170  const TransverseMercator& TMS =
171  series ? TransverseMercator(a, f, k0) : TransverseMercator(1, 0, 1);
172 
173  const TransverseMercatorExact& TME =
174  exact ? TransverseMercatorExact(a, f, k0, extended)
175  : TransverseMercatorExact(1, real(0.1), 1, false);
176 
177  // Max precision = 10: 0.1 nm in distance, 10^-15 deg (= 0.11 nm),
178  // 10^-11 sec (= 0.3 nm).
179  prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
180  std::string s;
181  int retval = 0;
182  std::cout << std::fixed;
183  while (std::getline(*input, s)) {
184  try {
185  std::string eol("\n");
186  if (!cdelim.empty()) {
187  std::string::size_type m = s.find(cdelim);
188  if (m != std::string::npos) {
189  eol = " " + s.substr(m) + "\n";
190  s = s.substr(0, m);
191  }
192  }
193  std::istringstream str(s);
194  real lat, lon, x, y;
195  std::string stra, strb;
196  if (!(str >> stra >> strb))
197  throw GeographicErr("Incomplete input: " + s);
198  if (reverse) {
199  x = Utility::num<real>(stra);
200  y = Utility::num<real>(strb);
201  } else
202  DMS::DecodeLatLon(stra, strb, lat, lon);
203  std::string strc;
204  if (str >> strc)
205  throw GeographicErr("Extraneous input: " + strc);
206  real gamma, k;
207  if (reverse) {
208  if (series)
209  TMS.Reverse(lon0, x, y, lat, lon, gamma, k);
210  else
211  TME.Reverse(lon0, x, y, lat, lon, gamma, k);
212  *output << Utility::str(lat, prec + 5) << " "
213  << Utility::str(lon, prec + 5) << " "
214  << Utility::str(gamma, prec + 6) << " "
215  << Utility::str(k, prec + 6) << eol;
216  } else {
217  if (series)
218  TMS.Forward(lon0, lat, lon, x, y, gamma, k);
219  else
220  TME.Forward(lon0, lat, lon, x, y, gamma, k);
221  *output << Utility::str(x, prec) << " "
222  << Utility::str(y, prec) << " "
223  << Utility::str(gamma, prec + 6) << " "
224  << Utility::str(k, prec + 6) << eol;
225  }
226  }
227  catch (const std::exception& e) {
228  *output << "ERROR: " << e.what() << "\n";
229  retval = 1;
230  }
231  }
232  return retval;
233  }
234  catch (const std::exception& e) {
235  std::cerr << "Caught exception: " << e.what() << "\n";
236  return 1;
237  }
238  catch (...) {
239  std::cerr << "Caught unknown exception\n";
240  return 1;
241  }
242 }
static T AngNormalize(T x)
Definition: Math.hpp:399
An exact implementation of the transverse Mercator projection.
GeographicLib::Math::real real
Definition: GeodSolve.cpp:32
Header for GeographicLib::Utility class.
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:184
static void DecodeLatLon(const std::string &dmsa, const std::string &dmsb, real &lat, real &lon, bool swaplatlong=false)
Definition: DMS.cpp:214
static Math::real Decode(const std::string &dms, flag &ind)
Definition: DMS.cpp:28
static std::string str(T x, int p=-1)
Definition: Utility.hpp:276
Header for GeographicLib::EllipticFunction class.
static int set_digits(int ndigits=0)
Definition: Utility.cpp:48
Exception handling for GeographicLib.
Definition: Constants.hpp:361
Header for GeographicLib::DMS class.