GeographicLib  1.42
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-2012) <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;
36  real
37  a = Constants::WGS84_a(),
38  f = Constants::WGS84_f();
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);
53  h0 = Utility::num<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::num<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 == "--input-string") {
72  if (++m == argc) return usage(1, true);
73  istring = argv[m];
74  } else if (arg == "--input-file") {
75  if (++m == argc) return usage(1, true);
76  ifile = argv[m];
77  } else if (arg == "--output-file") {
78  if (++m == argc) return usage(1, true);
79  ofile = argv[m];
80  } else if (arg == "--line-separator") {
81  if (++m == argc) return usage(1, true);
82  if (std::string(argv[m]).size() != 1) {
83  std::cerr << "Line separator must be a single character\n";
84  return 1;
85  }
86  lsep = argv[m][0];
87  } else if (arg == "--comment-delimiter") {
88  if (++m == argc) return usage(1, true);
89  cdelim = argv[m];
90  } else if (arg == "--version") {
91  std::cout
92  << argv[0] << ": GeographicLib version "
93  << GEOGRAPHICLIB_VERSION_STRING << "\n";
94  return 0;
95  } else
96  return usage(!(arg == "-h" || arg == "--help"), arg != "--help");
97  }
98 
99  if (!ifile.empty() && !istring.empty()) {
100  std::cerr << "Cannot specify --input-string and --input-file together\n";
101  return 1;
102  }
103  if (ifile == "-") ifile.clear();
104  std::ifstream infile;
105  std::istringstream instring;
106  if (!ifile.empty()) {
107  infile.open(ifile.c_str());
108  if (!infile.is_open()) {
109  std::cerr << "Cannot open " << ifile << " for reading\n";
110  return 1;
111  }
112  } else if (!istring.empty()) {
113  std::string::size_type m = 0;
114  while (true) {
115  m = istring.find(lsep, m);
116  if (m == std::string::npos)
117  break;
118  istring[m] = '\n';
119  }
120  instring.str(istring);
121  }
122  std::istream* input = !ifile.empty() ? &infile :
123  (!istring.empty() ? &instring : &std::cin);
124 
125  std::ofstream outfile;
126  if (ofile == "-") ofile.clear();
127  if (!ofile.empty()) {
128  outfile.open(ofile.c_str());
129  if (!outfile.is_open()) {
130  std::cerr << "Cannot open " << ofile << " for writing\n";
131  return 1;
132  }
133  }
134  std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
135 
136  const Geocentric ec(a, f);
137  const LocalCartesian lc(lat0, lon0, h0, ec);
138 
139  std::string s;
140  int retval = 0;
141  while (std::getline(*input, s)) {
142  try {
143  std::string eol("\n");
144  if (!cdelim.empty()) {
145  std::string::size_type m = s.find(cdelim);
146  if (m != std::string::npos) {
147  eol = " " + s.substr(m) + "\n";
148  s = s.substr(0, m);
149  }
150  }
151  std::istringstream str(s);
152  // initial values to suppress warnings
153  real lat, lon, h, x = 0, y = 0, z = 0;
154  std::string stra, strb, strc;
155  if (!(str >> stra >> strb >> strc))
156  throw GeographicErr("Incomplete input: " + s);
157  if (reverse) {
158  x = Utility::num<real>(stra);
159  y = Utility::num<real>(strb);
160  z = Utility::num<real>(strc);
161  } else {
162  DMS::DecodeLatLon(stra, strb, lat, lon);
163  h = Utility::num<real>(strc);
164  }
165  std::string strd;
166  if (str >> strd)
167  throw GeographicErr("Extraneous input: " + strd);
168  if (reverse) {
169  if (localcartesian)
170  lc.Reverse(x, y, z, lat, lon, h);
171  else
172  ec.Reverse(x, y, z, lat, lon, h);
173  *output << Utility::str(lat, 15 + Math::extra_digits()) << " "
174  << Utility::str(lon, 15 + Math::extra_digits()) << " "
175  << Utility::str(h, 12 + Math::extra_digits()) << eol;
176  } else {
177  if (localcartesian)
178  lc.Forward(lat, lon, h, x, y, z);
179  else
180  ec.Forward(lat, lon, h, x, y, z);
181  *output << Utility::str(x, 10 + Math::extra_digits()) << " "
182  << Utility::str(y, 10 + Math::extra_digits()) << " "
183  << Utility::str(z, 10 + Math::extra_digits()) << eol;
184  }
185  }
186  catch (const std::exception& e) {
187  *output << "ERROR: " << e.what() << "\n";
188  retval = 1;
189  }
190  }
191  return retval;
192  }
193  catch (const std::exception& e) {
194  std::cerr << "Caught exception: " << e.what() << "\n";
195  return 1;
196  }
197  catch (...) {
198  std::cerr << "Caught unknown exception\n";
199  return 1;
200  }
201 }
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:137
static int extra_digits()
Definition: Math.hpp:185
Geocentric coordinates
Definition: Geocentric.hpp:67
void Reverse(real x, real y, real z, real &lat, real &lon, real &h) const
static void DecodeLatLon(const std::string &dmsa, const std::string &dmsb, real &lat, real &lon, bool swaplatlong=false)
Definition: DMS.cpp:246
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.
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:199
Exception handling for GeographicLib.
Definition: Constants.hpp:382
void Forward(real lat, real lon, real h, real &x, real &y, real &z) const
Header for GeographicLib::DMS class.