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