GeographicLib  1.38
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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  * Compile and link with
10  * g++ -g -O3 -I../include -I../man -o CartConvert \
11  * CartConvert.cpp \
12  * ../src/DMS.cpp \
13  * ../src/Geocentric.cpp \
14  * ../src/LocalCartesian.cpp
15  *
16  * See the <a href="CartConvert.1.html">man page</a> for usage
17  * information.
18  **********************************************************************/
19 
20 #include <iostream>
21 #include <sstream>
22 #include <string>
23 #include <sstream>
24 #include <fstream>
27 #include <GeographicLib/DMS.hpp>
29 
30 #if defined(_MSC_VER)
31 // Squelch warnings about constant conditional expressions and potentially
32 // uninitialized local variables
33 # pragma warning (disable: 4127 4701)
34 #endif
35 
36 #include "CartConvert.usage"
37 
38 int main(int argc, char* argv[]) {
39  try {
40  using namespace GeographicLib;
41  typedef Math::real real;
43  bool localcartesian = false, reverse = false;
44  real
45  a = Constants::WGS84_a(),
46  f = Constants::WGS84_f();
47  real lat0 = 0, lon0 = 0, h0 = 0;
48  std::string istring, ifile, ofile, cdelim;
49  char lsep = ';';
50 
51  for (int m = 1; m < argc; ++m) {
52  std::string arg(argv[m]);
53  if (arg == "-r")
54  reverse = true;
55  else if (arg == "-l") {
56  localcartesian = true;
57  if (m + 3 >= argc) return usage(1, true);
58  try {
59  DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),
60  lat0, lon0);
61  h0 = Utility::num<real>(std::string(argv[m + 3]));
62  }
63  catch (const std::exception& e) {
64  std::cerr << "Error decoding arguments of -l: " << e.what() << "\n";
65  return 1;
66  }
67  m += 3;
68  } else if (arg == "-e") {
69  if (m + 2 >= argc) return usage(1, true);
70  try {
71  a = Utility::num<real>(std::string(argv[m + 1]));
72  f = Utility::fract<real>(std::string(argv[m + 2]));
73  }
74  catch (const std::exception& e) {
75  std::cerr << "Error decoding arguments of -e: " << e.what() << "\n";
76  return 1;
77  }
78  m += 2;
79  } else if (arg == "--input-string") {
80  if (++m == argc) return usage(1, true);
81  istring = argv[m];
82  } else if (arg == "--input-file") {
83  if (++m == argc) return usage(1, true);
84  ifile = argv[m];
85  } else if (arg == "--output-file") {
86  if (++m == argc) return usage(1, true);
87  ofile = argv[m];
88  } else if (arg == "--line-separator") {
89  if (++m == argc) return usage(1, true);
90  if (std::string(argv[m]).size() != 1) {
91  std::cerr << "Line separator must be a single character\n";
92  return 1;
93  }
94  lsep = argv[m][0];
95  } else if (arg == "--comment-delimiter") {
96  if (++m == argc) return usage(1, true);
97  cdelim = argv[m];
98  } else if (arg == "--version") {
99  std::cout
100  << argv[0] << ": GeographicLib version "
101  << GEOGRAPHICLIB_VERSION_STRING << "\n";
102  return 0;
103  } else
104  return usage(!(arg == "-h" || arg == "--help"), arg != "--help");
105  }
106 
107  if (!ifile.empty() && !istring.empty()) {
108  std::cerr << "Cannot specify --input-string and --input-file together\n";
109  return 1;
110  }
111  if (ifile == "-") ifile.clear();
112  std::ifstream infile;
113  std::istringstream instring;
114  if (!ifile.empty()) {
115  infile.open(ifile.c_str());
116  if (!infile.is_open()) {
117  std::cerr << "Cannot open " << ifile << " for reading\n";
118  return 1;
119  }
120  } else if (!istring.empty()) {
121  std::string::size_type m = 0;
122  while (true) {
123  m = istring.find(lsep, m);
124  if (m == std::string::npos)
125  break;
126  istring[m] = '\n';
127  }
128  instring.str(istring);
129  }
130  std::istream* input = !ifile.empty() ? &infile :
131  (!istring.empty() ? &instring : &std::cin);
132 
133  std::ofstream outfile;
134  if (ofile == "-") ofile.clear();
135  if (!ofile.empty()) {
136  outfile.open(ofile.c_str());
137  if (!outfile.is_open()) {
138  std::cerr << "Cannot open " << ofile << " for writing\n";
139  return 1;
140  }
141  }
142  std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
143 
144  const Geocentric ec(a, f);
145  const LocalCartesian lc(lat0, lon0, h0, ec);
146 
147  std::string s;
148  int retval = 0;
149  while (std::getline(*input, s)) {
150  try {
151  std::string eol("\n");
152  if (!cdelim.empty()) {
153  std::string::size_type m = s.find(cdelim);
154  if (m != std::string::npos) {
155  eol = " " + s.substr(m) + "\n";
156  s = s.substr(0, m);
157  }
158  }
159  std::istringstream str(s);
160  // initial values to suppress warnings
161  real lat, lon, h, x = 0, y = 0, z = 0;
162  std::string stra, strb, strc;
163  if (!(str >> stra >> strb >> strc))
164  throw GeographicErr("Incomplete input: " + s);
165  if (reverse) {
166  x = Utility::num<real>(stra);
167  y = Utility::num<real>(strb);
168  z = Utility::num<real>(strc);
169  } else {
170  DMS::DecodeLatLon(stra, strb, lat, lon);
171  h = Utility::num<real>(strc);
172  }
173  std::string strd;
174  if (str >> strd)
175  throw GeographicErr("Extraneous input: " + strd);
176  if (reverse) {
177  if (localcartesian)
178  lc.Reverse(x, y, z, lat, lon, h);
179  else
180  ec.Reverse(x, y, z, lat, lon, h);
181  *output << Utility::str(lat, 15 + Math::extra_digits()) << " "
182  << Utility::str(lon, 15 + Math::extra_digits()) << " "
183  << Utility::str(h, 12 + Math::extra_digits()) << eol;
184  } else {
185  if (localcartesian)
186  lc.Forward(lat, lon, h, x, y, z);
187  else
188  ec.Forward(lat, lon, h, x, y, z);
189  *output << Utility::str(x, 10 + Math::extra_digits()) << " "
190  << Utility::str(y, 10 + Math::extra_digits()) << " "
191  << Utility::str(z, 10 + Math::extra_digits()) << eol;
192  }
193  }
194  catch (const std::exception& e) {
195  *output << "ERROR: " << e.what() << "\n";
196  retval = 1;
197  }
198  }
199  return retval;
200  }
201  catch (const std::exception& e) {
202  std::cerr << "Caught exception: " << e.what() << "\n";
203  return 1;
204  }
205  catch (...) {
206  std::cerr << "Caught unknown exception\n";
207  return 1;
208  }
209 }