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