GeographicLib  1.46
GeoidEval.cpp
Go to the documentation of this file.
1 /**
2  * \file GeoidEval.cpp
3  * \brief Command line utility for evaluating geoid heights
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="GeoidEval.1.html">man page</a> for usage information.
10  **********************************************************************/
11 
12 #include <iostream>
13 #include <string>
14 #include <sstream>
15 #include <fstream>
16 #include <GeographicLib/Geoid.hpp>
17 #include <GeographicLib/DMS.hpp>
20 
21 #if defined(_MSC_VER)
22 // Squelch warnings about constant conditional expressions and potentially
23 // uninitialized local variables
24 # pragma warning (disable: 4127 4701)
25 #endif
26 
27 #include "GeoidEval.usage"
28 
29 int main(int argc, char* argv[]) {
30  try {
31  using namespace GeographicLib;
32  typedef Math::real real;
34  bool cacheall = false, cachearea = false, verbose = false, cubic = true;
35  real caches, cachew, cachen, cachee;
36  std::string dir;
37  std::string geoid = Geoid::DefaultGeoidName();
38  Geoid::convertflag heightmult = Geoid::NONE;
39  std::string istring, ifile, ofile, cdelim;
40  char lsep = ';';
41  bool northp = false, longfirst = false;
42  int zonenum = UTMUPS::INVALID;
43 
44  for (int m = 1; m < argc; ++m) {
45  std::string arg(argv[m]);
46  if (arg == "-a") {
47  cacheall = true;
48  cachearea = false;
49  }
50  else if (arg == "-c") {
51  if (m + 4 >= argc) return usage(1, true);
52  cacheall = false;
53  cachearea = true;
54  try {
55  DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),
56  caches, cachew, longfirst);
57  DMS::DecodeLatLon(std::string(argv[m + 3]), std::string(argv[m + 4]),
58  cachen, cachee, longfirst);
59  }
60  catch (const std::exception& e) {
61  std::cerr << "Error decoding argument of -c: " << e.what() << "\n";
62  return 1;
63  }
64  m += 4;
65  } else if (arg == "--msltohae")
66  heightmult = Geoid::GEOIDTOELLIPSOID;
67  else if (arg == "--haetomsl")
68  heightmult = Geoid::ELLIPSOIDTOGEOID;
69  else if (arg == "-w")
70  longfirst = !longfirst;
71  else if (arg == "-z") {
72  if (++m == argc) return usage(1, true);
73  std::string zone = argv[m];
74  try {
75  UTMUPS::DecodeZone(zone, zonenum, northp);
76  }
77  catch (const std::exception& e) {
78  std::cerr << "Error decoding zone: " << e.what() << "\n";
79  return 1;
80  }
81  if (!(zonenum >= UTMUPS::MINZONE && zonenum <= UTMUPS::MAXZONE)) {
82  std::cerr << "Illegal zone " << zone << "\n";
83  return 1;
84  }
85  } else if (arg == "-n") {
86  if (++m == argc) return usage(1, true);
87  geoid = argv[m];
88  } else if (arg == "-d") {
89  if (++m == argc) return usage(1, true);
90  dir = argv[m];
91  } else if (arg == "-l")
92  cubic = false;
93  else if (arg == "-v")
94  verbose = true;
95  else if (arg == "--input-string") {
96  if (++m == argc) return usage(1, true);
97  istring = argv[m];
98  } else if (arg == "--input-file") {
99  if (++m == argc) return usage(1, true);
100  ifile = argv[m];
101  } else if (arg == "--output-file") {
102  if (++m == argc) return usage(1, true);
103  ofile = argv[m];
104  } else if (arg == "--line-separator") {
105  if (++m == argc) return usage(1, true);
106  if (std::string(argv[m]).size() != 1) {
107  std::cerr << "Line separator must be a single character\n";
108  return 1;
109  }
110  lsep = argv[m][0];
111  } else if (arg == "--comment-delimiter") {
112  if (++m == argc) return usage(1, true);
113  cdelim = argv[m];
114  } else if (arg == "--version") {
115  std::cout << argv[0] << ": GeographicLib version "
116  << GEOGRAPHICLIB_VERSION_STRING << "\n";
117  return 0;
118  } else {
119  int retval = usage(!(arg == "-h" || arg == "--help"), arg != "--help");
120  if (arg == "-h")
121  std::cout<< "\nDefault geoid path = \"" << Geoid::DefaultGeoidPath()
122  << "\"\nDefault geoid name = \"" << Geoid::DefaultGeoidName()
123  << "\"\n";
124  return retval;
125  }
126  }
127 
128  if (!ifile.empty() && !istring.empty()) {
129  std::cerr << "Cannot specify --input-string and --input-file together\n";
130  return 1;
131  }
132  if (ifile == "-") ifile.clear();
133  std::ifstream infile;
134  std::istringstream instring;
135  if (!ifile.empty()) {
136  infile.open(ifile.c_str());
137  if (!infile.is_open()) {
138  std::cerr << "Cannot open " << ifile << " for reading\n";
139  return 1;
140  }
141  } else if (!istring.empty()) {
142  std::string::size_type m = 0;
143  while (true) {
144  m = istring.find(lsep, m);
145  if (m == std::string::npos)
146  break;
147  istring[m] = '\n';
148  }
149  instring.str(istring);
150  }
151  std::istream* input = !ifile.empty() ? &infile :
152  (!istring.empty() ? &instring : &std::cin);
153 
154  std::ofstream outfile;
155  if (ofile == "-") ofile.clear();
156  if (!ofile.empty()) {
157  outfile.open(ofile.c_str());
158  if (!outfile.is_open()) {
159  std::cerr << "Cannot open " << ofile << " for writing\n";
160  return 1;
161  }
162  }
163  std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
164 
165  int retval = 0;
166  try {
167  const Geoid g(geoid, dir, cubic);
168  try {
169  if (cacheall)
170  g.CacheAll();
171  else if (cachearea)
172  g.CacheArea(caches, cachew, cachen, cachee);
173  }
174  catch (const std::exception& e) {
175  std::cerr << "ERROR: " << e.what() << "\nProceeding without a cache\n";
176  }
177  if (verbose) {
178  std::cerr << "Geoid file: " << g.GeoidFile() << "\n"
179  << "Description: " << g.Description() << "\n"
180  << "Interpolation: " << g.Interpolation() << "\n"
181  << "Date & Time: " << g.DateTime() << "\n"
182  << "Offset (m): " << g.Offset() << "\n"
183  << "Scale (m): " << g.Scale() << "\n"
184  << "Max error (m): " << g.MaxError() << "\n"
185  << "RMS error (m): " << g.RMSError() << "\n";
186  if (g.Cache())
187  std::cerr<< "Caching:"
188  << "\n SW Corner: " << g.CacheSouth() << " " << g.CacheWest()
189  << "\n NE Corner: " << g.CacheNorth() << " " << g.CacheEast()
190  << "\n";
191  }
192 
193  GeoCoords p;
194  std::string s, eol, suff;
195  const char* spaces = " \t\n\v\f\r,"; // Include comma as space
196  while (std::getline(*input, s)) {
197  try {
198  eol = "\n";
199  if (!cdelim.empty()) {
200  std::string::size_type m = s.find(cdelim);
201  if (m != std::string::npos) {
202  eol = " " + s.substr(m) + "\n";
203  std::string::size_type m1 =
204  m > 0 ? s.find_last_not_of(spaces, m - 1) : std::string::npos;
205  s = s.substr(0, m1 != std::string::npos ? m1 + 1 : m);
206  }
207  }
208  real height = 0;
209  if (zonenum != UTMUPS::INVALID) {
210  // Expect "easting northing" if heightmult == 0, or
211  // "easting northing height" if heightmult != 0.
212  std::string::size_type pa = 0, pb = 0;
213  real easting = 0, northing = 0;
214  for (int i = 0; i < (heightmult ? 3 : 2); ++i) {
215  if (pb == std::string::npos)
216  throw GeographicErr("Incomplete input: " + s);
217  // Start of i'th token
218  pa = s.find_first_not_of(spaces, pb);
219  if (pa == std::string::npos)
220  throw GeographicErr("Incomplete input: " + s);
221  // End of i'th token
222  pb = s.find_first_of(spaces, pa);
223  (i == 2 ? height : (i == 0 ? easting : northing)) =
224  Utility::num<real>(s.substr(pa, (pb == std::string::npos ?
225  pb : pb - pa)));
226  }
227  p.Reset(zonenum, northp, easting, northing);
228  if (heightmult) {
229  suff = pb == std::string::npos ? "" : s.substr(pb);
230  s = s.substr(0, pa);
231  }
232  } else {
233  if (heightmult) {
234  // Treat last token as height
235  // pb = last char of last token
236  // pa = last char preceding white space
237  // px = last char of 2nd last token
238  std::string::size_type pb = s.find_last_not_of(spaces);
239  std::string::size_type pa = s.find_last_of(spaces, pb);
240  if (pa == std::string::npos || pb == std::string::npos)
241  throw GeographicErr("Incomplete input: " + s);
242  height = Utility::num<real>(s.substr(pa + 1, pb - pa));
243  s = s.substr(0, pa + 1);
244  }
245  p.Reset(s, true, longfirst);
246  }
247  if (heightmult) {
248  real h = g(p.Latitude(), p.Longitude());
249  *output << s
250  << Utility::str(height + real(heightmult) * h, 4)
251  << suff << eol;
252  } else {
253  real h = g(p.Latitude(), p.Longitude());
254  *output << Utility::str(h, 4) << eol;
255  }
256  }
257  catch (const std::exception& e) {
258  *output << "ERROR: " << e.what() << "\n";
259  retval = 1;
260  }
261  }
262  }
263  catch (const std::exception& e) {
264  std::cerr << "Error reading " << geoid << ": " << e.what() << "\n";
265  retval = 1;
266  }
267  return retval;
268  }
269  catch (const std::exception& e) {
270  std::cerr << "Caught exception: " << e.what() << "\n";
271  return 1;
272  }
273  catch (...) {
274  std::cerr << "Caught unknown exception\n";
275  return 1;
276  }
277 }
Math::real Latitude() const
Definition: GeoCoords.hpp:277
Math::real MaxError() const
Definition: Geoid.hpp:354
GeographicLib::Math::real real
Definition: GeodSolve.cpp:31
Math::real CacheWest() const
Definition: Geoid.hpp:394
Header for GeographicLib::Utility class.
Math::real CacheNorth() const
Definition: Geoid.hpp:413
Conversion between geographic coordinates.
Definition: GeoCoords.hpp:49
const std::string & DateTime() const
Definition: Geoid.hpp:323
static std::string DefaultGeoidPath()
Definition: Geoid.cpp:488
const std::string & GeoidFile() const
Definition: Geoid.hpp:328
Header for GeographicLib::GeoCoords class.
bool Cache() const
Definition: Geoid.hpp:389
void CacheArea(real south, real west, real north, real east) const
Definition: Geoid.cpp:407
const std::string Interpolation() const
Definition: Geoid.hpp:344
Math::real Offset() const
Definition: Geoid.hpp:371
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
Math::real RMSError() const
Definition: Geoid.hpp:363
static void DecodeZone(const std::string &zonestr, int &zone, bool &northp)
Definition: UTMUPS.cpp:206
static std::string str(T x, int p=-1)
Definition: Utility.hpp:276
static void DecodeLatLon(const std::string &dmsa, const std::string &dmsb, real &lat, real &lon, bool longfirst=false)
Definition: DMS.cpp:250
static int set_digits(int ndigits=0)
Definition: Utility.cpp:48
Exception handling for GeographicLib.
Definition: Constants.hpp:373
static std::string DefaultGeoidName()
Definition: Geoid.cpp:501
Math::real CacheSouth() const
Definition: Geoid.hpp:421
Math::real CacheEast() const
Definition: Geoid.hpp:403
const std::string & Description() const
Definition: Geoid.hpp:318
Math::real Scale() const
Definition: Geoid.hpp:379
Header for GeographicLib::Geoid class.
void Reset(const std::string &s, bool centerp=true, bool longfirst=false)
Definition: GeoCoords.cpp:19
void CacheAll() const
Definition: Geoid.hpp:260
int main(int argc, char *argv[])
Definition: GeoidEval.cpp:29
Math::real Longitude() const
Definition: GeoCoords.hpp:282
Looking up the height of the geoid above the ellipsoid.
Definition: Geoid.hpp:82
Header for GeographicLib::DMS class.