GeoConvert.cpp
Go to the documentation of this file.
1 
12 #include <iostream>
13 #include <string>
14 #include <sstream>
15 #include <fstream>
17 #include <GeographicLib/DMS.hpp>
19 #include <GeographicLib/MGRS.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, const char* const 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, longfirst = false;
38  std::string istring, ifile, ofile, cdelim;
39  char lsep = ';', dmssep = char(0);
40  bool sethemisphere = false, northp = false, abbrev = true, latch = false;
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 == "-z") {
61  if (++m == argc) return usage(1, true);
62  std::string zonestr(argv[m]);
63  try {
64  UTMUPS::DecodeZone(zonestr, zone, northp);
65  sethemisphere = true;
66  }
67  catch (const std::exception&) {
68  std::istringstream str(zonestr);
69  char c;
70  if (!(str >> zone) || (str >> c)) {
71  std::cerr << "Zone " << zonestr
72  << " is not a number or zone+hemisphere\n";
73  return 1;
74  }
75  if (!(zone >= UTMUPS::MINZONE && zone <= UTMUPS::MAXZONE)) {
76  std::cerr << "Zone " << zone << " not in [0, 60]\n";
77  return 1;
78  }
79  sethemisphere = false;
80  }
81  latch = false;
82  } else if (arg == "-s") {
83  zone = UTMUPS::STANDARD;
84  sethemisphere = false;
85  latch = false;
86  } else if (arg == "-S") {
87  zone = UTMUPS::STANDARD;
88  sethemisphere = false;
89  latch = true;
90  } else if (arg == "-t") {
91  zone = UTMUPS::UTM;
92  sethemisphere = false;
93  latch = false;
94  } else if (arg == "-T") {
95  zone = UTMUPS::UTM;
96  sethemisphere = false;
97  latch = true;
98  } else if (arg == "-w")
99  longfirst = !longfirst;
100  else if (arg == "-p") {
101  if (++m == argc) return usage(1, true);
102  try {
103  prec = Utility::val<int>(std::string(argv[m]));
104  }
105  catch (const std::exception&) {
106  std::cerr << "Precision " << argv[m] << " is not a number\n";
107  return 1;
108  }
109  } else if (arg == "-l")
110  abbrev = false;
111  else if (arg == "-a")
112  abbrev = true;
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 << argv[0] << ": GeographicLib version "
134  << GEOGRAPHICLIB_VERSION_STRING << "\n";
135  MGRS::Check();
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, eol;
180  std::string os;
181  int retval = 0;
182 
183  while (std::getline(*input, s)) {
184  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, longfirst);
194  p.SetAltZone(zone);
195  switch (outputmode) {
196  case GEOGRAPHIC:
197  os = p.GeoRepresentation(prec, longfirst);
198  break;
199  case DMS:
200  os = p.DMSRepresentation(prec, longfirst, 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  if (latch &&
221  zone < UTMUPS::MINZONE && p.AltZone() >= UTMUPS::MINZONE) {
222  zone = p.AltZone();
223  northp = p.Northp();
224  sethemisphere = true;
225  latch = false;
226  }
227  }
228  catch (const std::exception& e) {
229  // Write error message to cout so output lines match input lines
230  os = std::string("ERROR: ") + e.what();
231  retval = 1;
232  }
233  *output << os << eol;
234  }
235  return retval;
236  }
237  catch (const std::exception& e) {
238  std::cerr << "Caught exception: " << e.what() << "\n";
239  return 1;
240  }
241  catch (...) {
242  std::cerr << "Caught unknown exception\n";
243  return 1;
244  }
245 }
Matrix3f m
#define max(a, b)
Definition: datatypes.h:20
float real
Definition: datatypes.h:10
Header for GeographicLib::Utility class.
#define min(a, b)
Definition: datatypes.h:19
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
Math::real AltScale() const
Definition: GeoCoords.hpp:375
Conversion between geographic coordinates.
Definition: GeoCoords.hpp:49
Header for GeographicLib::GeoCoords class.
Header for GeographicLib::MGRS class.
static int extra_digits()
Definition: Math.hpp:187
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
Convert between geographic coordinates and UTM/UPS.
Definition: UTMUPS.hpp:75
Convert between degrees and the DMS representation.
Definition: DMS.hpp:34
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const ArgReturnType arg() const
#define GEOGRAPHICLIB_VERSION_STRING
Definition: Config.h:3
static void Check()
Definition: src/MGRS.cpp:403
Namespace for GeographicLib.
Array< double, 1, 3 > e(1./3., 0.5, 2.)
RealScalar s
static void DecodeZone(const std::string &zonestr, int &zone, bool &northp)
Definition: src/UTMUPS.cpp:208
static std::string str(T x, int p=-1)
Definition: Utility.hpp:276
std::string AltMGRSRepresentation(int prec=0) const
std::string AltUTMUPSRepresentation(int prec=0, bool abbrev=true) const
static int set_digits(int ndigits=0)
Definition: Utility.cpp:48
std::string GeoRepresentation(int prec=0, bool longfirst=false) const
ofstream os("timeSchurFactors.csv")
std::string DMSRepresentation(int prec=0, bool longfirst=false, char dmssep=char(0)) const
int main(int argc, const char *const argv[])
Definition: GeoConvert.cpp:28
float * p
void SetAltZone(int zone=UTMUPS::STANDARD) const
Definition: GeoCoords.hpp:337
Math::real AltConvergence() const
Definition: GeoCoords.hpp:370
void Reset(const std::string &s, bool centerp=true, bool longfirst=false)
Convert between UTM/UPS and MGRS.
Definition: MGRS.hpp:74
Header for GeographicLib::DMS class.


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:17