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