GeographicLib  1.43
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-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="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,
35  cubic = true, gradp = false;
36  real caches, cachew, cachen, cachee;
37  std::string dir;
38  std::string geoid = Geoid::DefaultGeoidName();
39  Geoid::convertflag heightmult = Geoid::NONE;
40  std::string istring, ifile, ofile, cdelim;
41  char lsep = ';';
42  bool northp = false;
43  int zonenum = UTMUPS::INVALID;
44 
45  for (int m = 1; m < argc; ++m) {
46  std::string arg(argv[m]);
47  if (arg == "-a") {
48  cacheall = true;
49  cachearea = false;
50  }
51  else if (arg == "-c") {
52  if (m + 4 >= argc) return usage(1, true);
53  cacheall = false;
54  cachearea = true;
55  try {
56  DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),
57  caches, cachew);
58  DMS::DecodeLatLon(std::string(argv[m + 3]), std::string(argv[m + 4]),
59  cachen, cachee);
60  }
61  catch (const std::exception& e) {
62  std::cerr << "Error decoding argument of -c: " << e.what() << "\n";
63  return 1;
64  }
65  m += 4;
66  } else if (arg == "--msltohae")
67  heightmult = Geoid::GEOIDTOELLIPSOID;
68  else if (arg == "--haetomsl")
69  heightmult = Geoid::ELLIPSOIDTOGEOID;
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 == "-g")
93  gradp = true;
94  else if (arg == "-v")
95  verbose = true;
96  else if (arg == "--input-string") {
97  if (++m == argc) return usage(1, true);
98  istring = argv[m];
99  } else if (arg == "--input-file") {
100  if (++m == argc) return usage(1, true);
101  ifile = argv[m];
102  } else if (arg == "--output-file") {
103  if (++m == argc) return usage(1, true);
104  ofile = argv[m];
105  } else if (arg == "--line-separator") {
106  if (++m == argc) return usage(1, true);
107  if (std::string(argv[m]).size() != 1) {
108  std::cerr << "Line separator must be a single character\n";
109  return 1;
110  }
111  lsep = argv[m][0];
112  } else if (arg == "--comment-delimiter") {
113  if (++m == argc) return usage(1, true);
114  cdelim = argv[m];
115  } else if (arg == "--version") {
116  std::cout << argv[0] << ": GeographicLib version "
117  << GEOGRAPHICLIB_VERSION_STRING << "\n";
118  return 0;
119  } else {
120  int retval = usage(!(arg == "-h" || arg == "--help"), arg != "--help");
121  if (arg == "-h")
122  std::cout<< "\nDefault geoid path = \"" << Geoid::DefaultGeoidPath()
123  << "\"\nDefault geoid name = \"" << Geoid::DefaultGeoidName()
124  << "\"\n";
125  return retval;
126  }
127  }
128 
129  if (!ifile.empty() && !istring.empty()) {
130  std::cerr << "Cannot specify --input-string and --input-file together\n";
131  return 1;
132  }
133  if (ifile == "-") ifile.clear();
134  std::ifstream infile;
135  std::istringstream instring;
136  if (!ifile.empty()) {
137  infile.open(ifile.c_str());
138  if (!infile.is_open()) {
139  std::cerr << "Cannot open " << ifile << " for reading\n";
140  return 1;
141  }
142  } else if (!istring.empty()) {
143  std::string::size_type m = 0;
144  while (true) {
145  m = istring.find(lsep, m);
146  if (m == std::string::npos)
147  break;
148  istring[m] = '\n';
149  }
150  instring.str(istring);
151  }
152  std::istream* input = !ifile.empty() ? &infile :
153  (!istring.empty() ? &instring : &std::cin);
154 
155  std::ofstream outfile;
156  if (ofile == "-") ofile.clear();
157  if (!ofile.empty()) {
158  outfile.open(ofile.c_str());
159  if (!outfile.is_open()) {
160  std::cerr << "Cannot open " << ofile << " for writing\n";
161  return 1;
162  }
163  }
164  std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
165 
166  int retval = 0;
167  try {
168  const Geoid g(geoid, dir, cubic);
169  try {
170  if (cacheall)
171  g.CacheAll();
172  else if (cachearea)
173  g.CacheArea(caches, cachew, cachen, cachee);
174  }
175  catch (const std::exception& e) {
176  std::cerr << "ERROR: " << e.what() << "\nProceeding without a cache\n";
177  }
178  if (verbose) {
179  std::cerr << "Geoid file: " << g.GeoidFile() << "\n"
180  << "Description: " << g.Description() << "\n"
181  << "Interpolation: " << g.Interpolation() << "\n"
182  << "Date & Time: " << g.DateTime() << "\n"
183  << "Offset (m): " << g.Offset() << "\n"
184  << "Scale (m): " << g.Scale() << "\n"
185  << "Max error (m): " << g.MaxError() << "\n"
186  << "RMS error (m): " << g.RMSError() << "\n";
187  if (g.Cache())
188  std::cerr<< "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, suff;
196  const char* spaces = " \t\n\v\f\r,"; // Include comma as space
197  while (std::getline(*input, s)) {
198  try {
199  std::string 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::num<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::num<real>(s.substr(pa + 1, pb - pa));
244  s = s.substr(0, pa + 1);
245  }
246  p.Reset(s);
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  if (gradp) {
255  real gradn, grade;
256  real h = g(p.Latitude(), p.Longitude(), gradn, grade);
257  *output << Utility::str(h, 4) << " "
258  << Utility::str(gradn * 1e6, 2)
259  << (Math::isnan(gradn) ? " " : "e-6 ")
260  << Utility::str(grade * 1e6, 2)
261  << (Math::isnan(grade) ? "" : "e-6")
262  << eol;
263  } else {
264  real h = g(p.Latitude(), p.Longitude());
265  *output << Utility::str(h, 4) << eol;
266  }
267  }
268  }
269  catch (const std::exception& e) {
270  *output << "ERROR: " << e.what() << "\n";
271  retval = 1;
272  }
273  }
274  }
275  catch (const std::exception& e) {
276  std::cerr << "Error reading " << geoid << ": " << e.what() << "\n";
277  retval = 1;
278  }
279  return retval;
280  }
281  catch (const std::exception& e) {
282  std::cerr << "Caught exception: " << e.what() << "\n";
283  return 1;
284  }
285  catch (...) {
286  std::cerr << "Caught unknown exception\n";
287  return 1;
288  }
289 }
Math::real Latitude() const
Definition: GeoCoords.hpp:282
Math::real MaxError() const
Definition: Geoid.hpp:393
GeographicLib::Math::real real
Definition: GeodSolve.cpp:32
Math::real CacheWest() const
Definition: Geoid.hpp:433
Header for GeographicLib::Utility class.
static bool isnan(T x)
Definition: Math.hpp:646
Math::real CacheNorth() const
Definition: Geoid.hpp:452
Conversion between geographic coordinates.
Definition: GeoCoords.hpp:49
const std::string & DateTime() const
Definition: Geoid.hpp:362
static std::string DefaultGeoidPath()
Definition: Geoid.cpp:521
const std::string & GeoidFile() const
Definition: Geoid.hpp:367
Header for GeographicLib::GeoCoords class.
void Reset(const std::string &s, bool centerp=true, bool swaplatlong=false)
Definition: GeoCoords.cpp:19
bool Cache() const
Definition: Geoid.hpp:428
void CacheArea(real south, real west, real north, real east) const
Definition: Geoid.cpp:442
const std::string Interpolation() const
Definition: Geoid.hpp:383
static void DecodeLatLon(const std::string &dmsa, const std::string &dmsb, real &lat, real &lon, bool swaplatlong=false)
Definition: DMS.cpp:246
Math::real Offset() const
Definition: Geoid.hpp:410
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
Math::real RMSError() const
Definition: Geoid.hpp:402
static void DecodeZone(const std::string &zonestr, int &zone, bool &northp)
Definition: UTMUPS.cpp:214
static std::string str(T x, int p=-1)
Definition: Utility.hpp:276
static int set_digits(int ndigits=0)
Definition: Utility.cpp:48
Exception handling for GeographicLib.
Definition: Constants.hpp:382
static std::string DefaultGeoidName()
Definition: Geoid.cpp:534
Math::real CacheSouth() const
Definition: Geoid.hpp:460
Math::real CacheEast() const
Definition: Geoid.hpp:442
const std::string & Description() const
Definition: Geoid.hpp:357
Math::real Scale() const
Definition: Geoid.hpp:418
Header for GeographicLib::Geoid class.
void CacheAll() const
Definition: Geoid.hpp:272
int main(int argc, char *argv[])
Definition: GeoidEval.cpp:29
Math::real Longitude() const
Definition: GeoCoords.hpp:287
Looking up the height of the geoid above the ellipsoid.
Definition: Geoid.hpp:92
Header for GeographicLib::DMS class.