GeographicLib  2.6
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-2017) <karney@alum.mit.edu> and licensed
6  * under the MIT/X11 License. For more information, see
7  * https://geographiclib.sourceforge.io/
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 potentially uninitialized local variables
23 # pragma warning (disable: 4701)
24 #endif
25 
26 #include "GeoidEval.usage"
27 
28 int main(int argc, const char* const argv[]) {
29  try {
30  using namespace GeographicLib;
31  typedef Math::real real;
33  bool cacheall = false, cachearea = false, verbose = false, cubic = true;
34  real caches, cachew, cachen, cachee;
35  std::string dir;
36  std::string geoid = Geoid::DefaultGeoidName();
37  Geoid::convertflag heightmult = Geoid::NONE;
38  std::string istring, ifile, ofile, cdelim;
39  char lsep = ';';
40  bool northp = false, longfirst = false;
41  int zonenum = UTMUPS::INVALID;
42 
43  for (int m = 1; m < argc; ++m) {
44  std::string arg(argv[m]);
45  if (arg == "-a") {
46  cacheall = true;
47  cachearea = false;
48  }
49  else if (arg == "-c") {
50  if (m + 4 >= argc) return usage(1, true);
51  cacheall = false;
52  cachearea = true;
53  try {
54  DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),
55  caches, cachew, longfirst);
56  DMS::DecodeLatLon(std::string(argv[m + 3]), std::string(argv[m + 4]),
57  cachen, cachee, longfirst);
58  }
59  catch (const std::exception& e) {
60  std::cerr << "Error decoding argument of -c: " << e.what() << "\n";
61  return 1;
62  }
63  m += 4;
64  } else if (arg == "--msltohae")
65  heightmult = Geoid::GEOIDTOELLIPSOID;
66  else if (arg == "--haetomsl")
67  heightmult = Geoid::ELLIPSOIDTOGEOID;
68  else if (arg == "-w")
69  longfirst = !longfirst;
70  else if (arg == "-z") {
71  if (++m == argc) return usage(1, true);
72  std::string zone = argv[m];
73  try {
74  UTMUPS::DecodeZone(zone, zonenum, northp);
75  }
76  catch (const std::exception& e) {
77  std::cerr << "Error decoding zone: " << e.what() << "\n";
78  return 1;
79  }
80  if (!(zonenum >= UTMUPS::MINZONE && zonenum <= UTMUPS::MAXZONE)) {
81  std::cerr << "Illegal zone " << zone << "\n";
82  return 1;
83  }
84  } else if (arg == "-n") {
85  if (++m == argc) return usage(1, true);
86  geoid = argv[m];
87  } else if (arg == "-d") {
88  if (++m == argc) return usage(1, true);
89  dir = argv[m];
90  } else if (arg == "-l")
91  cubic = false;
92  else if (arg == "-v")
93  verbose = true;
94  else if (arg == "--input-string") {
95  if (++m == argc) return usage(1, true);
96  istring = argv[m];
97  } else if (arg == "--input-file") {
98  if (++m == argc) return usage(1, true);
99  ifile = argv[m];
100  } else if (arg == "--output-file") {
101  if (++m == argc) return usage(1, true);
102  ofile = argv[m];
103  } else if (arg == "--line-separator") {
104  if (++m == argc) return usage(1, true);
105  if (std::string(argv[m]).size() != 1) {
106  std::cerr << "Line separator must be a single character\n";
107  return 1;
108  }
109  lsep = argv[m][0];
110  } else if (arg == "--comment-delimiter") {
111  if (++m == argc) return usage(1, true);
112  cdelim = argv[m];
113  } else if (arg == "--version") {
114  std::cout << argv[0] << ": GeographicLib version "
115  << GEOGRAPHICLIB_VERSION_STRING << "\n";
116  return 0;
117  } else {
118  int retval = usage(!(arg == "-h" || arg == "--help"), arg != "--help");
119  if (arg == "-h")
120  std::cout
121  << "\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
188  << "Caching:"
189  << "\n SW Corner: " << g.CacheSouth() << " " << g.CacheWest()
190  << "\n NE Corner: " << g.CacheNorth() << " " << g.CacheEast()
191  << "\n";
192  }
193 
194  GeoCoords p;
195  std::string s, eol, suff;
196  const char* spaces = " \t\n\v\f\r,"; // Include comma as space
197  while (std::getline(*input, s)) {
198  try {
199  eol = "\n";
200  if (!cdelim.empty()) {
201  std::string::size_type m = s.find(cdelim);
202  if (m != std::string::npos) {
203  eol = " " + s.substr(m) + "\n";
204  std::string::size_type m1 =
205  m > 0 ? s.find_last_not_of(spaces, m - 1) : std::string::npos;
206  s = s.substr(0, m1 != std::string::npos ? m1 + 1 : m);
207  }
208  }
209  real height = 0;
210  if (zonenum != UTMUPS::INVALID) {
211  // Expect "easting northing" if heightmult == 0, or
212  // "easting northing height" if heightmult != 0.
213  std::string::size_type pa = 0, pb = 0;
214  real easting = 0, northing = 0;
215  for (int i = 0; i < (heightmult ? 3 : 2); ++i) {
216  if (pb == std::string::npos)
217  throw GeographicErr("Incomplete input: " + s);
218  // Start of i'th token
219  pa = s.find_first_not_of(spaces, pb);
220  if (pa == std::string::npos)
221  throw GeographicErr("Incomplete input: " + s);
222  // End of i'th token
223  pb = s.find_first_of(spaces, pa);
224  (i == 2 ? height : (i == 0 ? easting : northing)) =
225  Utility::val<real>(s.substr(pa, (pb == std::string::npos ?
226  pb : pb - pa)));
227  }
228  p.Reset(zonenum, northp, easting, northing);
229  if (heightmult) {
230  suff = pb == std::string::npos ? "" : s.substr(pb);
231  s = s.substr(0, pa);
232  }
233  } else {
234  if (heightmult) {
235  // Treat last token as height
236  // pb = last char of last token
237  // pa = last char preceding white space
238  // px = last char of 2nd last token
239  std::string::size_type pb = s.find_last_not_of(spaces);
240  std::string::size_type pa = s.find_last_of(spaces, pb);
241  if (pa == std::string::npos || pb == std::string::npos)
242  throw GeographicErr("Incomplete input: " + s);
243  height = Utility::val<real>(s.substr(pa + 1, pb - pa));
244  s = s.substr(0, pa + 1);
245  }
246  p.Reset(s, true, longfirst);
247  }
248  if (heightmult) {
249  real h = g(p.Latitude(), p.Longitude());
250  *output << s
251  << Utility::str(height + real(heightmult) * h, 4)
252  << suff << eol;
253  } else {
254  real h = g(p.Latitude(), p.Longitude());
255  *output << Utility::str(h, 4) << eol;
256  }
257  }
258  catch (const std::exception& e) {
259  *output << "ERROR: " << e.what() << "\n";
260  retval = 1;
261  }
262  }
263  }
264  catch (const std::exception& e) {
265  std::cerr << "Error reading " << geoid << ": " << e.what() << "\n";
266  retval = 1;
267  }
268  return retval;
269  }
270  catch (const std::exception& e) {
271  std::cerr << "Caught exception: " << e.what() << "\n";
272  return 1;
273  }
274  catch (...) {
275  std::cerr << "Caught unknown exception\n";
276  return 1;
277  }
278 }
const std::string & GeoidFile() const
Definition: Geoid.hpp:328
Math::real Scale() const
Definition: Geoid.hpp:379
Math::real RMSError() const
Definition: Geoid.hpp:363
const std::string Interpolation() const
Definition: Geoid.hpp:344
Math::real Offset() const
Definition: Geoid.hpp:371
Header for GeographicLib::Utility class.
void CacheArea(real south, real west, real north, real east) const
Definition: Geoid.cpp:407
Conversion between geographic coordinates.
Definition: GeoCoords.hpp:49
bool Cache() const
Definition: Geoid.hpp:389
Math::real CacheNorth() const
Definition: Geoid.hpp:413
Math::real MaxError() const
Definition: Geoid.hpp:354
Header for GeographicLib::GeoCoords class.
int usage(int retval, bool)
Definition: Geod3ODE.cpp:41
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
static std::string DefaultGeoidPath()
Definition: Geoid.cpp:488
const std::string & Description() const
Definition: Geoid.hpp:318
static void DecodeZone(const std::string &zonestr, int &zone, bool &northp)
Definition: UTMUPS.cpp:205
static std::string str(T x, int p=-1)
Definition: Utility.hpp:161
Math::real Longitude() const
Definition: GeoCoords.hpp:281
int main(int argc, const char *const argv[])
Definition: GeoidEval.cpp:28
static void DecodeLatLon(const std::string &dmsa, const std::string &dmsb, real &lat, real &lon, bool longfirst=false)
Definition: DMS.cpp:374
GeographicLib::Math::real real
Definition: Geod3Solve.cpp:25
static int set_digits(int ndigits=0)
Definition: Utility.cpp:184
static std::string DefaultGeoidName()
Definition: Geoid.cpp:501
Exception handling for GeographicLib.
Definition: Constants.hpp:344
Math::real CacheSouth() const
Definition: Geoid.hpp:421
Math::real CacheWest() const
Definition: Geoid.hpp:394
Math::real Latitude() const
Definition: GeoCoords.hpp:276
Header for GeographicLib::Geoid class.
const std::string & DateTime() const
Definition: Geoid.hpp:323
void Reset(const std::string &s, bool centerp=true, bool longfirst=false)
Definition: GeoCoords.cpp:19
void CacheAll() const
Definition: Geoid.hpp:258
Math::real CacheEast() const
Definition: Geoid.hpp:403
Looking up the height of the geoid above the ellipsoid.
Definition: Geoid.hpp:82
Header for GeographicLib::DMS class.