GeographicLib  1.38
 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  * Compile and link with
10  * g++ -g -O3 -I../include -I../man -o GeoConvert \
11  * GeoConvert.cpp \
12  * ../src/DMS.cpp \
13  * ../src/GeoCoords.cpp \
14  * ../src/MGRS.cpp \
15  * ../src/PolarStereographic.cpp \
16  * ../src/TransverseMercator.cpp \
17  * ../src/UTMUPS.cpp
18  *
19  * See the <a href="GeoConvert.1.html">man page</a> for usage
20  * information.
21  **********************************************************************/
22 
23 #include <iostream>
24 #include <sstream>
25 #include <string>
26 #include <sstream>
27 #include <fstream>
29 #include <GeographicLib/DMS.hpp>
31 
32 #if defined(_MSC_VER)
33 // Squelch warnings about constant conditional expressions
34 # pragma warning (disable: 4127)
35 #endif
36 
37 #include "GeoConvert.usage"
38 
39 int main(int argc, char* argv[]) {
40  try {
41  using namespace GeographicLib;
42  typedef Math::real real;
44  enum { GEOGRAPHIC, DMS, UTMUPS, MGRS, CONVERGENCE };
45  int outputmode = GEOGRAPHIC;
46  int prec = 0;
47  int zone = UTMUPS::MATCH;
48  bool centerp = true, swaplatlong = false;
49  std::string istring, ifile, ofile, cdelim;
50  char lsep = ';', dmssep = char(0);
51  bool sethemisphere = false, northp = false, abbrev = true;
52 
53  for (int m = 1; m < argc; ++m) {
54  std::string arg(argv[m]);
55  if (arg == "-g")
56  outputmode = GEOGRAPHIC;
57  else if (arg == "-d") {
58  outputmode = DMS;
59  dmssep = '\0';
60  } else if (arg == "-:") {
61  outputmode = DMS;
62  dmssep = ':';
63  } else if (arg == "-u")
64  outputmode = UTMUPS;
65  else if (arg == "-m")
66  outputmode = MGRS;
67  else if (arg == "-c")
68  outputmode = CONVERGENCE;
69  else if (arg == "-n")
70  centerp = false;
71  else if (arg == "-w")
72  swaplatlong = true;
73  else if (arg == "-l")
74  abbrev = false;
75  else if (arg == "-a")
76  abbrev = true;
77  else if (arg == "-p") {
78  if (++m == argc) return usage(1, true);
79  try {
80  prec = Utility::num<int>(std::string(argv[m]));
81  }
82  catch (const std::exception&) {
83  std::cerr << "Precision " << argv[m] << " is not a number\n";
84  return 1;
85  }
86  } else if (arg == "-z") {
87  if (++m == argc) return usage(1, true);
88  std::string zonestr(argv[m]);
89  try {
90  UTMUPS::DecodeZone(zonestr, zone, northp);
91  sethemisphere = true;
92  }
93  catch (const std::exception&) {
94  std::istringstream str(zonestr);
95  char c;
96  if (!(str >> zone) || (str >> c)) {
97  std::cerr << "Zone " << zonestr
98  << " is not a number or zone+hemisphere\n";
99  return 1;
100  }
101  if (!(zone >= UTMUPS::MINZONE && zone <= UTMUPS::MAXZONE)) {
102  std::cerr << "Zone " << zone << " not in [0, 60]\n";
103  return 1;
104  }
105  sethemisphere = false;
106  }
107  } else if (arg == "-s") {
108  zone = UTMUPS::STANDARD;
109  sethemisphere = false;
110  } else if (arg == "-t") {
111  zone = UTMUPS::UTM;
112  sethemisphere = false;
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  GeoCoords p;
179  std::string s;
180  std::string os;
181  int retval = 0;
182 
183  while (std::getline(*input, s)) {
184  std::string eol("\n");
185  try {
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  p.Reset(s, centerp, swaplatlong);
194  p.SetAltZone(zone);
195  switch (outputmode) {
196  case GEOGRAPHIC:
197  os = p.GeoRepresentation(prec, swaplatlong);
198  break;
199  case DMS:
200  os = p.DMSRepresentation(prec, swaplatlong, dmssep);
201  break;
202  case UTMUPS:
203  os = (sethemisphere
204  ? p.AltUTMUPSRepresentation(northp, prec, abbrev)
205  : p.AltUTMUPSRepresentation(prec, abbrev));
206  break;
207  case MGRS:
208  os = p.AltMGRSRepresentation(prec);
209  break;
210  case CONVERGENCE:
211  {
212  real
213  gamma = p.AltConvergence(),
214  k = p.AltScale();
215  int prec1 = std::max(-5, std::min(Math::extra_digits() + 8, prec));
216  os = Utility::str(gamma, prec1 + 5) + " "
217  + Utility::str(k, prec1 + 7);
218  }
219  }
220  }
221  catch (const std::exception& e) {
222  // Write error message to cout so output lines match input lines
223  os = std::string("ERROR: ") + e.what();
224  retval = 1;
225  }
226  *output << os << eol;
227  }
228  return retval;
229  }
230  catch (const std::exception& e) {
231  std::cerr << "Caught exception: " << e.what() << "\n";
232  return 1;
233  }
234  catch (...) {
235  std::cerr << "Caught unknown exception\n";
236  return 1;
237  }
238 }
GeographicLib::Math::real real
Definition: GeodSolve.cpp:40
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:266
static int set_digits(int ndigits=0)
Definition: Utility.cpp:48
int main(int argc, char *argv[])
Definition: GeoConvert.cpp:39
Convert between UTM/UPS and MGRS.
Definition: MGRS.hpp:69
Header for GeographicLib::DMS class.