GeographicLib  1.44
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-2015) <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 #include <GeographicLib/MGRS.hpp>
21 
22 #if defined(_MSC_VER)
23 // Squelch warnings about constant conditional expressions
24 # pragma warning (disable: 4127)
25 #endif
26 
27 #include "GeoConvert.usage"
28 
29 int main(int argc, char* argv[]) {
30  try {
31  using namespace GeographicLib;
32  typedef Math::real real;
34  enum { GEOGRAPHIC, DMS, UTMUPS, MGRS, CONVERGENCE };
35  int outputmode = GEOGRAPHIC;
36  int prec = 0;
37  int zone = UTMUPS::MATCH;
38  bool centerp = true, longfirst = false;
39  std::string istring, ifile, ofile, cdelim;
40  char lsep = ';', dmssep = char(0);
41  bool sethemisphere = false, northp = false, abbrev = true;
42 
43  for (int m = 1; m < argc; ++m) {
44  std::string arg(argv[m]);
45  if (arg == "-g")
46  outputmode = GEOGRAPHIC;
47  else if (arg == "-d") {
48  outputmode = DMS;
49  dmssep = '\0';
50  } else if (arg == "-:") {
51  outputmode = DMS;
52  dmssep = ':';
53  } else if (arg == "-u")
54  outputmode = UTMUPS;
55  else if (arg == "-m")
56  outputmode = MGRS;
57  else if (arg == "-c")
58  outputmode = CONVERGENCE;
59  else if (arg == "-n")
60  centerp = false;
61  else if (arg == "-z") {
62  if (++m == argc) return usage(1, true);
63  std::string zonestr(argv[m]);
64  try {
65  UTMUPS::DecodeZone(zonestr, zone, northp);
66  sethemisphere = true;
67  }
68  catch (const std::exception&) {
69  std::istringstream str(zonestr);
70  char c;
71  if (!(str >> zone) || (str >> c)) {
72  std::cerr << "Zone " << zonestr
73  << " is not a number or zone+hemisphere\n";
74  return 1;
75  }
76  if (!(zone >= UTMUPS::MINZONE && zone <= UTMUPS::MAXZONE)) {
77  std::cerr << "Zone " << zone << " not in [0, 60]\n";
78  return 1;
79  }
80  sethemisphere = false;
81  }
82  } else if (arg == "-s") {
83  zone = UTMUPS::STANDARD;
84  sethemisphere = false;
85  } else if (arg == "-t") {
86  zone = UTMUPS::UTM;
87  sethemisphere = false;
88  } else if (arg == "-w")
89  longfirst = true;
90  else if (arg == "-p") {
91  if (++m == argc) return usage(1, true);
92  try {
93  prec = Utility::num<int>(std::string(argv[m]));
94  }
95  catch (const std::exception&) {
96  std::cerr << "Precision " << argv[m] << " is not a number\n";
97  return 1;
98  }
99  } else if (arg == "-l")
100  abbrev = false;
101  else if (arg == "-a")
102  abbrev = true;
103  else if (arg == "--input-string") {
104  if (++m == argc) return usage(1, true);
105  istring = argv[m];
106  } else if (arg == "--input-file") {
107  if (++m == argc) return usage(1, true);
108  ifile = argv[m];
109  } else if (arg == "--output-file") {
110  if (++m == argc) return usage(1, true);
111  ofile = argv[m];
112  } else if (arg == "--line-separator") {
113  if (++m == argc) return usage(1, true);
114  if (std::string(argv[m]).size() != 1) {
115  std::cerr << "Line separator must be a single character\n";
116  return 1;
117  }
118  lsep = argv[m][0];
119  } else if (arg == "--comment-delimiter") {
120  if (++m == argc) return usage(1, true);
121  cdelim = argv[m];
122  } else if (arg == "--version") {
123  std::cout << argv[0] << ": GeographicLib version "
124  << GEOGRAPHICLIB_VERSION_STRING << "\n";
125  MGRS::Check();
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  GeoCoords p;
169  std::string s, eol;
170  std::string os;
171  int retval = 0;
172 
173  while (std::getline(*input, s)) {
174  eol = "\n";
175  try {
176  if (!cdelim.empty()) {
177  std::string::size_type m = s.find(cdelim);
178  if (m != std::string::npos) {
179  eol = " " + s.substr(m) + "\n";
180  s = s.substr(0, m);
181  }
182  }
183  p.Reset(s, centerp, longfirst);
184  p.SetAltZone(zone);
185  switch (outputmode) {
186  case GEOGRAPHIC:
187  os = p.GeoRepresentation(prec, longfirst);
188  break;
189  case DMS:
190  os = p.DMSRepresentation(prec, longfirst, dmssep);
191  break;
192  case UTMUPS:
193  os = (sethemisphere
194  ? p.AltUTMUPSRepresentation(northp, prec, abbrev)
195  : p.AltUTMUPSRepresentation(prec, abbrev));
196  break;
197  case MGRS:
198  os = p.AltMGRSRepresentation(prec);
199  break;
200  case CONVERGENCE:
201  {
202  real
203  gamma = p.AltConvergence(),
204  k = p.AltScale();
205  int prec1 = std::max(-5, std::min(Math::extra_digits() + 8, prec));
206  os = Utility::str(gamma, prec1 + 5) + " "
207  + Utility::str(k, prec1 + 7);
208  }
209  }
210  }
211  catch (const std::exception& e) {
212  // Write error message to cout so output lines match input lines
213  os = std::string("ERROR: ") + e.what();
214  retval = 1;
215  }
216  *output << os << eol;
217  }
218  return retval;
219  }
220  catch (const std::exception& e) {
221  std::cerr << "Caught exception: " << e.what() << "\n";
222  return 1;
223  }
224  catch (...) {
225  std::cerr << "Caught unknown exception\n";
226  return 1;
227  }
228 }
GeographicLib::Math::real real
Definition: GeodSolve.cpp:32
Header for GeographicLib::Utility class.
Conversion between geographic coordinates.
Definition: GeoCoords.hpp:49
std::string AltUTMUPSRepresentation(int prec=0, bool abbrev=true) const
Definition: GeoCoords.cpp:145
Header for GeographicLib::GeoCoords class.
Header for GeographicLib::MGRS class.
static int extra_digits()
Definition: Math.hpp:187
Convert between geographic coordinates and UTM/UPS.
Definition: UTMUPS.hpp:75
Convert between degrees and the DMS representation.
Definition: DMS.hpp:34
std::string GeoRepresentation(int prec=0, bool longfirst=false) const
Definition: GeoCoords.cpp:62
std::string AltMGRSRepresentation(int prec=0) const
Definition: GeoCoords.cpp:97
static void Check()
Definition: MGRS.cpp:402
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
static void DecodeZone(const std::string &zonestr, int &zone, bool &northp)
Definition: UTMUPS.cpp:206
static std::string str(T x, int p=-1)
Definition: Utility.hpp:276
static int set_digits(int ndigits=0)
Definition: Utility.cpp:48
Math::real AltConvergence() const
Definition: GeoCoords.hpp:369
int main(int argc, char *argv[])
Definition: GeoConvert.cpp:29
std::string DMSRepresentation(int prec=0, bool longfirst=false, char dmssep=char(0)) const
Definition: GeoCoords.cpp:80
Math::real AltScale() const
Definition: GeoCoords.hpp:374
void SetAltZone(int zone=UTMUPS::STANDARD) const
Definition: GeoCoords.hpp:336
void Reset(const std::string &s, bool centerp=true, bool longfirst=false)
Definition: GeoCoords.cpp:19
Convert between UTM/UPS and MGRS.
Definition: MGRS.hpp:74
Header for GeographicLib::DMS class.