CartConvert.cpp
Go to the documentation of this file.
1 
12 #include <iostream>
13 #include <string>
14 #include <sstream>
15 #include <fstream>
18 #include <GeographicLib/DMS.hpp>
20 
21 #if defined(_MSC_VER)
22 // Squelch warnings about constant conditional expressions and potentially
23 // uninitialized local variables
24 # pragma warning (disable: 4127 4701)
25 #endif
26 
27 #include "CartConvert.usage"
28 
29 int main(int argc, const char* const argv[]) {
30  try {
31  using namespace GeographicLib;
32  typedef Math::real real;
34  bool localcartesian = false, reverse = false, longfirst = false;
35  real
38  int prec = 6;
39  real lat0 = 0, lon0 = 0, h0 = 0;
40  std::string istring, ifile, ofile, cdelim;
41  char lsep = ';';
42 
43  for (int m = 1; m < argc; ++m) {
44  std::string arg(argv[m]);
45  if (arg == "-r")
46  reverse = true;
47  else if (arg == "-l") {
48  localcartesian = true;
49  if (m + 3 >= argc) return usage(1, true);
50  try {
51  DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),
52  lat0, lon0, longfirst);
53  h0 = Utility::val<real>(std::string(argv[m + 3]));
54  }
55  catch (const std::exception& e) {
56  std::cerr << "Error decoding arguments of -l: " << e.what() << "\n";
57  return 1;
58  }
59  m += 3;
60  } else if (arg == "-e") {
61  if (m + 2 >= argc) return usage(1, true);
62  try {
63  a = Utility::val<real>(std::string(argv[m + 1]));
64  f = Utility::fract<real>(std::string(argv[m + 2]));
65  }
66  catch (const std::exception& e) {
67  std::cerr << "Error decoding arguments of -e: " << e.what() << "\n";
68  return 1;
69  }
70  m += 2;
71  } else if (arg == "-w")
72  longfirst = !longfirst;
73  else if (arg == "-p") {
74  if (++m == argc) return usage(1, true);
75  try {
76  prec = Utility::val<int>(std::string(argv[m]));
77  }
78  catch (const std::exception&) {
79  std::cerr << "Precision " << argv[m] << " is not a number\n";
80  return 1;
81  }
82  } else if (arg == "--input-string") {
83  if (++m == argc) return usage(1, true);
84  istring = argv[m];
85  } else if (arg == "--input-file") {
86  if (++m == argc) return usage(1, true);
87  ifile = argv[m];
88  } else if (arg == "--output-file") {
89  if (++m == argc) return usage(1, true);
90  ofile = argv[m];
91  } else if (arg == "--line-separator") {
92  if (++m == argc) return usage(1, true);
93  if (std::string(argv[m]).size() != 1) {
94  std::cerr << "Line separator must be a single character\n";
95  return 1;
96  }
97  lsep = argv[m][0];
98  } else if (arg == "--comment-delimiter") {
99  if (++m == argc) return usage(1, true);
100  cdelim = argv[m];
101  } else if (arg == "--version") {
102  std::cout << argv[0] << ": GeographicLib version "
103  << GEOGRAPHICLIB_VERSION_STRING << "\n";
104  return 0;
105  } else
106  return usage(!(arg == "-h" || arg == "--help"), arg != "--help");
107  }
108 
109  if (!ifile.empty() && !istring.empty()) {
110  std::cerr << "Cannot specify --input-string and --input-file together\n";
111  return 1;
112  }
113  if (ifile == "-") ifile.clear();
114  std::ifstream infile;
115  std::istringstream instring;
116  if (!ifile.empty()) {
117  infile.open(ifile.c_str());
118  if (!infile.is_open()) {
119  std::cerr << "Cannot open " << ifile << " for reading\n";
120  return 1;
121  }
122  } else if (!istring.empty()) {
123  std::string::size_type m = 0;
124  while (true) {
125  m = istring.find(lsep, m);
126  if (m == std::string::npos)
127  break;
128  istring[m] = '\n';
129  }
130  instring.str(istring);
131  }
132  std::istream* input = !ifile.empty() ? &infile :
133  (!istring.empty() ? &instring : &std::cin);
134 
135  std::ofstream outfile;
136  if (ofile == "-") ofile.clear();
137  if (!ofile.empty()) {
138  outfile.open(ofile.c_str());
139  if (!outfile.is_open()) {
140  std::cerr << "Cannot open " << ofile << " for writing\n";
141  return 1;
142  }
143  }
144  std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
145 
146  const Geocentric ec(a, f);
147  const LocalCartesian lc(lat0, lon0, h0, ec);
148 
149  // Max precision = 10: 0.1 nm in distance, 10^-15 deg (= 0.11 nm),
150  // 10^-11 sec (= 0.3 nm).
151  prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
152  std::string s, eol, stra, strb, strc, strd;
153  std::istringstream str;
154  int retval = 0;
155  while (std::getline(*input, s)) {
156  try {
157  eol = "\n";
158  if (!cdelim.empty()) {
159  std::string::size_type m = s.find(cdelim);
160  if (m != std::string::npos) {
161  eol = " " + s.substr(m) + "\n";
162  s = s.substr(0, m);
163  }
164  }
165  str.clear(); str.str(s);
166  // initial values to suppress warnings
167  real lat, lon, h, x = 0, y = 0, z = 0;
168  if (!(str >> stra >> strb >> strc))
169  throw GeographicErr("Incomplete input: " + s);
170  if (reverse) {
171  x = Utility::val<real>(stra);
172  y = Utility::val<real>(strb);
173  z = Utility::val<real>(strc);
174  } else {
175  DMS::DecodeLatLon(stra, strb, lat, lon, longfirst);
176  h = Utility::val<real>(strc);
177  }
178  if (str >> strd)
179  throw GeographicErr("Extraneous input: " + strd);
180  if (reverse) {
181  if (localcartesian)
182  lc.Reverse(x, y, z, lat, lon, h);
183  else
184  ec.Reverse(x, y, z, lat, lon, h);
185  *output << Utility::str(longfirst ? lon : lat, prec + 5) << " "
186  << Utility::str(longfirst ? lat : lon, prec + 5) << " "
187  << Utility::str(h, prec) << eol;
188  } else {
189  if (localcartesian)
190  lc.Forward(lat, lon, h, x, y, z);
191  else
192  ec.Forward(lat, lon, h, x, y, z);
193  *output << Utility::str(x, prec) << " "
194  << Utility::str(y, prec) << " "
195  << Utility::str(z, prec) << eol;
196  }
197  }
198  catch (const std::exception& e) {
199  *output << "ERROR: " << e.what() << "\n";
200  retval = 1;
201  }
202  }
203  return retval;
204  }
205  catch (const std::exception& e) {
206  std::cerr << "Caught exception: " << e.what() << "\n";
207  return 1;
208  }
209  catch (...) {
210  std::cerr << "Caught unknown exception\n";
211  return 1;
212  }
213 }
Matrix3f m
#define max(a, b)
Definition: datatypes.h:20
float real
Definition: datatypes.h:10
Scalar * y
Header for GeographicLib::LocalCartesian class.
static const double lat
Header for GeographicLib::Utility class.
#define min(a, b)
Definition: datatypes.h:19
const double h0
void Forward(real lat, real lon, real h, real &X, real &Y, real &Z) const
Definition: Geocentric.hpp:132
Array33i a
static int extra_digits()
Definition: Math.hpp:187
Geocentric coordinates
Definition: Geocentric.hpp:67
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
void Reverse(real x, real y, real z, real &lat, real &lon, real &h) const
const double lat0
int main(int argc, const char *const argv[])
Definition: CartConvert.cpp:29
#define GEOGRAPHICLIB_VERSION_STRING
Definition: Config.h:3
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Namespace for GeographicLib.
Array< double, 1, 3 > e(1./3., 0.5, 2.)
RealScalar s
const double lon0
static std::string str(T x, int p=-1)
Definition: Utility.hpp:276
Header for GeographicLib::Geocentric class.
static void DecodeLatLon(const std::string &dmsa, const std::string &dmsb, real &lat, real &lon, bool longfirst=false)
Definition: src/DMS.cpp:252
static int set_digits(int ndigits=0)
Definition: Utility.cpp:48
Local cartesian coordinates.
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const ArgReturnType arg() const
const double h
void Reverse(real X, real Y, real Z, real &lat, real &lon, real &h) const
Definition: Geocentric.hpp:194
Exception handling for GeographicLib.
Definition: Constants.hpp:389
void reverse(const MatrixType &m)
void Forward(real lat, real lon, real h, real &x, real &y, real &z) const
static const double lon
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Header for GeographicLib::DMS class.


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:41:46