GeographicLib  1.35
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;
42  bool localcartesian = false, reverse = false;
43  real
44  a = Constants::WGS84_a<real>(),
45  f = Constants::WGS84_f<real>();
46  real lat0 = 0, lon0 = 0, h0 = 0;
47  std::string istring, ifile, ofile, cdelim;
48  char lsep = ';';
49 
50  for (int m = 1; m < argc; ++m) {
51  std::string arg(argv[m]);
52  if (arg == "-r")
53  reverse = true;
54  else if (arg == "-l") {
55  localcartesian = true;
56  if (m + 3 >= argc) return usage(1, true);
57  try {
58  DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),
59  lat0, lon0);
60  h0 = Utility::num<real>(std::string(argv[m + 3]));
61  }
62  catch (const std::exception& e) {
63  std::cerr << "Error decoding arguments of -l: " << e.what() << "\n";
64  return 1;
65  }
66  m += 3;
67  } else if (arg == "-e") {
68  if (m + 2 >= argc) return usage(1, true);
69  try {
70  a = Utility::num<real>(std::string(argv[m + 1]));
71  f = Utility::fract<real>(std::string(argv[m + 2]));
72  }
73  catch (const std::exception& e) {
74  std::cerr << "Error decoding arguments of -e: " << e.what() << "\n";
75  return 1;
76  }
77  m += 2;
78  } else if (arg == "--input-string") {
79  if (++m == argc) return usage(1, true);
80  istring = argv[m];
81  } else if (arg == "--input-file") {
82  if (++m == argc) return usage(1, true);
83  ifile = argv[m];
84  } else if (arg == "--output-file") {
85  if (++m == argc) return usage(1, true);
86  ofile = argv[m];
87  } else if (arg == "--line-separator") {
88  if (++m == argc) return usage(1, true);
89  if (std::string(argv[m]).size() != 1) {
90  std::cerr << "Line separator must be a single character\n";
91  return 1;
92  }
93  lsep = argv[m][0];
94  } else if (arg == "--comment-delimiter") {
95  if (++m == argc) return usage(1, true);
96  cdelim = argv[m];
97  } else if (arg == "--version") {
98  std::cout
99  << argv[0] << ": GeographicLib version "
100  << GEOGRAPHICLIB_VERSION_STRING << "\n";
101  return 0;
102  } else
103  return usage(!(arg == "-h" || arg == "--help"), arg != "--help");
104  }
105 
106  if (!ifile.empty() && !istring.empty()) {
107  std::cerr << "Cannot specify --input-string and --input-file together\n";
108  return 1;
109  }
110  if (ifile == "-") ifile.clear();
111  std::ifstream infile;
112  std::istringstream instring;
113  if (!ifile.empty()) {
114  infile.open(ifile.c_str());
115  if (!infile.is_open()) {
116  std::cerr << "Cannot open " << ifile << " for reading\n";
117  return 1;
118  }
119  } else if (!istring.empty()) {
120  std::string::size_type m = 0;
121  while (true) {
122  m = istring.find(lsep, m);
123  if (m == std::string::npos)
124  break;
125  istring[m] = '\n';
126  }
127  instring.str(istring);
128  }
129  std::istream* input = !ifile.empty() ? &infile :
130  (!istring.empty() ? &instring : &std::cin);
131 
132  std::ofstream outfile;
133  if (ofile == "-") ofile.clear();
134  if (!ofile.empty()) {
135  outfile.open(ofile.c_str());
136  if (!outfile.is_open()) {
137  std::cerr << "Cannot open " << ofile << " for writing\n";
138  return 1;
139  }
140  }
141  std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
142 
143  const Geocentric ec(a, f);
144  const LocalCartesian lc(lat0, lon0, h0, ec);
145 
146  std::string s;
147  int retval = 0;
148  while (std::getline(*input, s)) {
149  try {
150  std::string eol("\n");
151  if (!cdelim.empty()) {
152  std::string::size_type m = s.find(cdelim);
153  if (m != std::string::npos) {
154  eol = " " + s.substr(m) + "\n";
155  s = s.substr(0, m);
156  }
157  }
158  std::istringstream str(s);
159  real lat, lon, h, x, y, z;
160  std::string stra, strb, strc;
161  if (!(str >> stra >> strb >> strc))
162  throw GeographicErr("Incomplete input: " + s);
163  if (reverse) {
164  x = Utility::num<real>(stra);
165  y = Utility::num<real>(strb);
166  z = Utility::num<real>(strc);
167  } else {
168  DMS::DecodeLatLon(stra, strb, lat, lon);
169  h = Utility::num<real>(strc);
170  }
171  std::string strd;
172  if (str >> strd)
173  throw GeographicErr("Extraneous input: " + strd);
174  if (reverse) {
175  if (localcartesian)
176  lc.Reverse(x, y, z, lat, lon, h);
177  else
178  ec.Reverse(x, y, z, lat, lon, h);
179  *output << Utility::str<real>(lat, 15) << " "
180  << Utility::str<real>(lon, 15) << " "
181  << Utility::str<real>(h, 12) << eol;
182  } else {
183  if (localcartesian)
184  lc.Forward(lat, lon, h, x, y, z);
185  else
186  ec.Forward(lat, lon, h, x, y, z);
187  *output << Utility::str<real>(x, 10) << " "
188  << Utility::str<real>(y, 10) << " "
189  << Utility::str<real>(z, 10) << eol;
190  }
191  }
192  catch (const std::exception& e) {
193  *output << "ERROR: " << e.what() << "\n";
194  retval = 1;
195  }
196  }
197  return retval;
198  }
199  catch (const std::exception& e) {
200  std::cerr << "Caught exception: " << e.what() << "\n";
201  return 1;
202  }
203  catch (...) {
204  std::cerr << "Caught unknown exception\n";
205  return 1;
206  }
207 }
Header for GeographicLib::LocalCartesian class.
GeographicLib::Math::real real
Definition: GeodSolve.cpp:40
Header for GeographicLib::Utility class.
void Forward(real lat, real lon, real h, real &X, real &Y, real &Z) const
Definition: Geocentric.hpp:131
Geocentric coordinates
Definition: Geocentric.hpp:61
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:213
Header for GeographicLib::Geocentric class.
int main(int argc, char *argv[])
Definition: CartConvert.cpp:38
Local cartesian coordinates.
void Reverse(real X, real Y, real Z, real &lat, real &lon, real &h) const
Definition: Geocentric.hpp:193
Exception handling for GeographicLib.
Definition: Constants.hpp:320
void Forward(real lat, real lon, real h, real &x, real &y, real &z) const
Header for GeographicLib::DMS class.