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