GeographicLib  1.39
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GeoConvert.cpp
Go to the documentation of this file.
1 /**
2  * \file GeoConvert.cpp
3  * \brief Command line utility for geographic coordinate conversions
4  *
5  * Copyright (c) Charles Karney (2008-2014) <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="GeoConvert.1.html">man page</a> for usage information.
10  **********************************************************************/
11 
12 #include <iostream>
13 #include <sstream>
14 #include <string>
15 #include <sstream>
16 #include <fstream>
18 #include <GeographicLib/DMS.hpp>
20 
21 #if defined(_MSC_VER)
22 // Squelch warnings about constant conditional expressions
23 # pragma warning (disable: 4127)
24 #endif
25 
26 #include "GeoConvert.usage"
27 
28 int main(int argc, char* argv[]) {
29  try {
30  using namespace GeographicLib;
31  typedef Math::real real;
33  enum { GEOGRAPHIC, DMS, UTMUPS, MGRS, CONVERGENCE };
34  int outputmode = GEOGRAPHIC;
35  int prec = 0;
36  int zone = UTMUPS::MATCH;
37  bool centerp = true, swaplatlong = false;
38  std::string istring, ifile, ofile, cdelim;
39  char lsep = ';', dmssep = char(0);
40  bool sethemisphere = false, northp = false, abbrev = true;
41 
42  for (int m = 1; m < argc; ++m) {
43  std::string arg(argv[m]);
44  if (arg == "-g")
45  outputmode = GEOGRAPHIC;
46  else if (arg == "-d") {
47  outputmode = DMS;
48  dmssep = '\0';
49  } else if (arg == "-:") {
50  outputmode = DMS;
51  dmssep = ':';
52  } else if (arg == "-u")
53  outputmode = UTMUPS;
54  else if (arg == "-m")
55  outputmode = MGRS;
56  else if (arg == "-c")
57  outputmode = CONVERGENCE;
58  else if (arg == "-n")
59  centerp = false;
60  else if (arg == "-w")
61  swaplatlong = true;
62  else if (arg == "-l")
63  abbrev = false;
64  else if (arg == "-a")
65  abbrev = true;
66  else if (arg == "-p") {
67  if (++m == argc) return usage(1, true);
68  try {
69  prec = Utility::num<int>(std::string(argv[m]));
70  }
71  catch (const std::exception&) {
72  std::cerr << "Precision " << argv[m] << " is not a number\n";
73  return 1;
74  }
75  } else if (arg == "-z") {
76  if (++m == argc) return usage(1, true);
77  std::string zonestr(argv[m]);
78  try {
79  UTMUPS::DecodeZone(zonestr, zone, northp);
80  sethemisphere = true;
81  }
82  catch (const std::exception&) {
83  std::istringstream str(zonestr);
84  char c;
85  if (!(str >> zone) || (str >> c)) {
86  std::cerr << "Zone " << zonestr
87  << " is not a number or zone+hemisphere\n";
88  return 1;
89  }
90  if (!(zone >= UTMUPS::MINZONE && zone <= UTMUPS::MAXZONE)) {
91  std::cerr << "Zone " << zone << " not in [0, 60]\n";
92  return 1;
93  }
94  sethemisphere = false;
95  }
96  } else if (arg == "-s") {
97  zone = UTMUPS::STANDARD;
98  sethemisphere = false;
99  } else if (arg == "-t") {
100  zone = UTMUPS::UTM;
101  sethemisphere = false;
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  GeoCoords p;
168  std::string s;
169  std::string os;
170  int retval = 0;
171 
172  while (std::getline(*input, s)) {
173  std::string eol("\n");
174  try {
175  if (!cdelim.empty()) {
176  std::string::size_type m = s.find(cdelim);
177  if (m != std::string::npos) {
178  eol = " " + s.substr(m) + "\n";
179  s = s.substr(0, m);
180  }
181  }
182  p.Reset(s, centerp, swaplatlong);
183  p.SetAltZone(zone);
184  switch (outputmode) {
185  case GEOGRAPHIC:
186  os = p.GeoRepresentation(prec, swaplatlong);
187  break;
188  case DMS:
189  os = p.DMSRepresentation(prec, swaplatlong, dmssep);
190  break;
191  case UTMUPS:
192  os = (sethemisphere
193  ? p.AltUTMUPSRepresentation(northp, prec, abbrev)
194  : p.AltUTMUPSRepresentation(prec, abbrev));
195  break;
196  case MGRS:
197  os = p.AltMGRSRepresentation(prec);
198  break;
199  case CONVERGENCE:
200  {
201  real
202  gamma = p.AltConvergence(),
203  k = p.AltScale();
204  int prec1 = std::max(-5, std::min(Math::extra_digits() + 8, prec));
205  os = Utility::str(gamma, prec1 + 5) + " "
206  + Utility::str(k, prec1 + 7);
207  }
208  }
209  }
210  catch (const std::exception& e) {
211  // Write error message to cout so output lines match input lines
212  os = std::string("ERROR: ") + e.what();
213  retval = 1;
214  }
215  *output << os << eol;
216  }
217  return retval;
218  }
219  catch (const std::exception& e) {
220  std::cerr << "Caught exception: " << e.what() << "\n";
221  return 1;
222  }
223  catch (...) {
224  std::cerr << "Caught unknown exception\n";
225  return 1;
226  }
227 }
GeographicLib::Math::real real
Definition: GeodSolve.cpp:32
Header for GeographicLib::Utility class.
Conversion between geographic coordinates.
Definition: GeoCoords.hpp:49
Header for GeographicLib::GeoCoords class.
static int extra_digits()
Definition: Math.hpp:184
Convert between geographic coordinates and UTM/UPS.
Definition: UTMUPS.hpp:75
Convert between degrees and the DMS representation.
Definition: DMS.hpp:34
static void DecodeZone(const std::string &zonestr, int &zone, bool &northp)
Definition: UTMUPS.cpp:214
static std::string str(T x, int p=-1)
Definition: Utility.hpp:276
static int set_digits(int ndigits=0)
Definition: Utility.cpp:48
int main(int argc, char *argv[])
Definition: GeoConvert.cpp:28
Convert between UTM/UPS and MGRS.
Definition: MGRS.hpp:69
Header for GeographicLib::DMS class.