GeographicLib  1.35
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;
44  bool exact = true, extended = false, series = false, reverse = false;
45  real
46  a = Constants::WGS84_a<real>(),
47  f = Constants::WGS84_f<real>(),
48  k0 = Constants::UTM_k0<real>(),
49  lon0 = 0;
50  std::string istring, ifile, ofile, cdelim;
51  char lsep = ';';
52 
53  for (int m = 1; m < argc; ++m) {
54  std::string arg(argv[m]);
55  if (arg == "-r")
56  reverse = true;
57  else if (arg == "-t") {
58  exact = true;
59  extended = true;
60  series = false;
61  } else if (arg == "-s") {
62  exact = false;
63  extended = false;
64  series = true;
65  } else if (arg == "-l") {
66  if (++m >= argc) return usage(1, true);
67  try {
68  DMS::flag ind;
69  lon0 = DMS::Decode(std::string(argv[m]), ind);
70  if (ind == DMS::LATITUDE)
71  throw GeographicErr("Bad hemisphere");
72  if (!(lon0 >= -540 && lon0 < 540))
73  throw GeographicErr("Bad longitude");
74  lon0 = Math::AngNormalize(lon0);
75  }
76  catch (const std::exception& e) {
77  std::cerr << "Error decoding argument of " << arg << ": "
78  << e.what() << "\n";
79  return 1;
80  }
81  } else if (arg == "-k") {
82  if (++m >= argc) return usage(1, true);
83  try {
84  k0 = Utility::num<real>(std::string(argv[m]));
85  }
86  catch (const std::exception& e) {
87  std::cerr << "Error decoding argument of " << arg << ": "
88  << e.what() << "\n";
89  return 1;
90  }
91  } else if (arg == "-e") {
92  if (m + 2 >= argc) return usage(1, true);
93  try {
94  a = Utility::num<real>(std::string(argv[m + 1]));
95  f = Utility::fract<real>(std::string(argv[m + 2]));
96  }
97  catch (const std::exception& e) {
98  std::cerr << "Error decoding arguments of -e: " << e.what() << "\n";
99  return 1;
100  }
101  m += 2;
102  } else if (arg == "--input-string") {
103  if (++m == argc) return usage(1, true);
104  istring = argv[m];
105  } else if (arg == "--input-file") {
106  if (++m == argc) return usage(1, true);
107  ifile = argv[m];
108  } else if (arg == "--output-file") {
109  if (++m == argc) return usage(1, true);
110  ofile = argv[m];
111  } else if (arg == "--line-separator") {
112  if (++m == argc) return usage(1, true);
113  if (std::string(argv[m]).size() != 1) {
114  std::cerr << "Line separator must be a single character\n";
115  return 1;
116  }
117  lsep = argv[m][0];
118  } else if (arg == "--comment-delimiter") {
119  if (++m == argc) return usage(1, true);
120  cdelim = argv[m];
121  } else if (arg == "--version") {
122  std::cout
123  << argv[0] << ": GeographicLib version "
124  << GEOGRAPHICLIB_VERSION_STRING << "\n";
125  return 0;
126  } else
127  return usage(!(arg == "-h" || arg == "--help"), arg != "--help");
128  }
129 
130  if (!ifile.empty() && !istring.empty()) {
131  std::cerr << "Cannot specify --input-string and --input-file together\n";
132  return 1;
133  }
134  if (ifile == "-") ifile.clear();
135  std::ifstream infile;
136  std::istringstream instring;
137  if (!ifile.empty()) {
138  infile.open(ifile.c_str());
139  if (!infile.is_open()) {
140  std::cerr << "Cannot open " << ifile << " for reading\n";
141  return 1;
142  }
143  } else if (!istring.empty()) {
144  std::string::size_type m = 0;
145  while (true) {
146  m = istring.find(lsep, m);
147  if (m == std::string::npos)
148  break;
149  istring[m] = '\n';
150  }
151  instring.str(istring);
152  }
153  std::istream* input = !ifile.empty() ? &infile :
154  (!istring.empty() ? &instring : &std::cin);
155 
156  std::ofstream outfile;
157  if (ofile == "-") ofile.clear();
158  if (!ofile.empty()) {
159  outfile.open(ofile.c_str());
160  if (!outfile.is_open()) {
161  std::cerr << "Cannot open " << ofile << " for writing\n";
162  return 1;
163  }
164  }
165  std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
166 
167  const TransverseMercator& TMS =
168  series ? TransverseMercator(a, f, k0) : TransverseMercator(1, 0, 1);
169 
170  const TransverseMercatorExact& TME =
171  exact ? TransverseMercatorExact(a, f, k0, extended)
172  : TransverseMercatorExact(1, real(0.1), 1, false);
173 
174  std::string s;
175  int retval = 0;
176  std::cout << std::fixed;
177  while (std::getline(*input, s)) {
178  try {
179  std::string eol("\n");
180  if (!cdelim.empty()) {
181  std::string::size_type m = s.find(cdelim);
182  if (m != std::string::npos) {
183  eol = " " + s.substr(m) + "\n";
184  s = s.substr(0, m);
185  }
186  }
187  std::istringstream str(s);
188  real lat, lon, x, y;
189  std::string stra, strb;
190  if (!(str >> stra >> strb))
191  throw GeographicErr("Incomplete input: " + s);
192  if (reverse) {
193  x = Utility::num<real>(stra);
194  y = Utility::num<real>(strb);
195  } else
196  DMS::DecodeLatLon(stra, strb, lat, lon);
197  std::string strc;
198  if (str >> strc)
199  throw GeographicErr("Extraneous input: " + strc);
200  real gamma, k;
201  if (reverse) {
202  if (series)
203  TMS.Reverse(lon0, x, y, lat, lon, gamma, k);
204  else
205  TME.Reverse(lon0, x, y, lat, lon, gamma, k);
206  *output << Utility::str<real>(lat, 15) << " "
207  << Utility::str<real>(lon, 15) << " "
208  << Utility::str<real>(gamma, 16) << " "
209  << Utility::str<real>(k, 16) << eol;
210  } else {
211  if (series)
212  TMS.Forward(lon0, lat, lon, x, y, gamma, k);
213  else
214  TME.Forward(lon0, lat, lon, x, y, gamma, k);
215  *output << Utility::str<real>(x, 10) << " "
216  << Utility::str<real>(y, 10) << " "
217  << Utility::str<real>(gamma, 16) << " "
218  << Utility::str<real>(k, 16) << eol;
219  }
220  }
221  catch (const std::exception& e) {
222  *output << "ERROR: " << e.what() << "\n";
223  retval = 1;
224  }
225  }
226  return retval;
227  }
228  catch (const std::exception& e) {
229  std::cerr << "Caught exception: " << e.what() << "\n";
230  return 1;
231  }
232  catch (...) {
233  std::cerr << "Caught unknown exception\n";
234  return 1;
235  }
236 }