GeographicLib  1.42
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
117  << argv[0] << ": GeographicLib version "
118  << GEOGRAPHICLIB_VERSION_STRING << "\n";
119  return 0;
120  } else {
121  int retval = usage(!(arg == "-h" || arg == "--help"), arg != "--help");
122  if (arg == "-h")
123  std::cout<< "\nDefault geoid path = \"" << Geoid::DefaultGeoidPath()
124  << "\"\nDefault geoid name = \"" << Geoid::DefaultGeoidName()
125  << "\"\n";
126  return retval;
127  }
128  }
129 
130  if (!ifile.empty() && !istring.empty()) {
131  std::cerr << "Cannot specify --input-string and --input-file together\n";
132  return 1;
133  }
134  if (ifile == "-") ifile.clear();
135  std::ifstream infile;
136  std::istringstream instring;
137  if (!ifile.empty()) {
138  infile.open(ifile.c_str());
139  if (!infile.is_open()) {
140  std::cerr << "Cannot open " << ifile << " for reading\n";
141  return 1;
142  }
143  } else if (!istring.empty()) {
144  std::string::size_type m = 0;
145  while (true) {
146  m = istring.find(lsep, m);
147  if (m == std::string::npos)
148  break;
149  istring[m] = '\n';
150  }
151  instring.str(istring);
152  }
153  std::istream* input = !ifile.empty() ? &infile :
154  (!istring.empty() ? &instring : &std::cin);
155 
156  std::ofstream outfile;
157  if (ofile == "-") ofile.clear();
158  if (!ofile.empty()) {
159  outfile.open(ofile.c_str());
160  if (!outfile.is_open()) {
161  std::cerr << "Cannot open " << ofile << " for writing\n";
162  return 1;
163  }
164  }
165  std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
166 
167  int retval = 0;
168  try {
169  const Geoid g(geoid, dir, cubic);
170  try {
171  if (cacheall)
172  g.CacheAll();
173  else if (cachearea)
174  g.CacheArea(caches, cachew, cachen, cachee);
175  }
176  catch (const std::exception& e) {
177  std::cerr << "ERROR: " << e.what() << "\nProceeding without a cache\n";
178  }
179  if (verbose) {
180  std::cerr << "Geoid file: " << g.GeoidFile() << "\n"
181  << "Description: " << g.Description() << "\n"
182  << "Interpolation: " << g.Interpolation() << "\n"
183  << "Date & Time: " << g.DateTime() << "\n"
184  << "Offset (m): " << g.Offset() << "\n"
185  << "Scale (m): " << g.Scale() << "\n"
186  << "Max error (m): " << g.MaxError() << "\n"
187  << "RMS error (m): " << g.RMSError() << "\n";
188  if (g.Cache())
189  std::cerr<< "Caching:"
190  << "\n SW Corner: " << g.CacheSouth() << " " << g.CacheWest()
191  << "\n NE Corner: " << g.CacheNorth() << " " << g.CacheEast()
192  << "\n";
193  }
194 
195  GeoCoords p;
196  std::string s, suff;
197  const char* spaces = " \t\n\v\f\r,"; // Include comma as space
198  while (std::getline(*input, s)) {
199  try {
200  std::string eol("\n");
201  if (!cdelim.empty()) {
202  std::string::size_type m = s.find(cdelim);
203  if (m != std::string::npos) {
204  eol = " " + s.substr(m) + "\n";
205  std::string::size_type m1 =
206  m > 0 ? s.find_last_not_of(spaces, m - 1) : std::string::npos;
207  s = s.substr(0, m1 != std::string::npos ? m1 + 1 : m);
208  }
209  }
210  real height = 0;
211  if (zonenum != UTMUPS::INVALID) {
212  // Expect "easting northing" if heightmult == 0, or
213  // "easting northing height" if heightmult != 0.
214  std::string::size_type pa = 0, pb = 0;
215  real easting = 0, northing = 0;
216  for (int i = 0; i < (heightmult ? 3 : 2); ++i) {
217  if (pb == std::string::npos)
218  throw GeographicErr("Incomplete input: " + s);
219  // Start of i'th token
220  pa = s.find_first_not_of(spaces, pb);
221  if (pa == std::string::npos)
222  throw GeographicErr("Incomplete input: " + s);
223  // End of i'th token
224  pb = s.find_first_of(spaces, pa);
225  (i == 2 ? height : (i == 0 ? easting : northing)) =
226  Utility::num<real>(s.substr(pa, (pb == std::string::npos ?
227  pb : pb - pa)));
228  }
229  p.Reset(zonenum, northp, easting, northing);
230  if (heightmult) {
231  suff = pb == std::string::npos ? "" : s.substr(pb);
232  s = s.substr(0, pa);
233  }
234  } else {
235  if (heightmult) {
236  // Treat last token as height
237  // pb = last char of last token
238  // pa = last char preceding white space
239  // px = last char of 2nd last token
240  std::string::size_type pb = s.find_last_not_of(spaces);
241  std::string::size_type pa = s.find_last_of(spaces, pb);
242  if (pa == std::string::npos || pb == std::string::npos)
243  throw GeographicErr("Incomplete input: " + s);
244  height = Utility::num<real>(s.substr(pa + 1, pb - pa));
245  s = s.substr(0, pa + 1);
246  }
247  p.Reset(s);
248  }
249  if (heightmult) {
250  real h = g(p.Latitude(), p.Longitude());
251  *output << s
252  << Utility::str(height + real(heightmult) * h, 4)
253  << suff << eol;
254  } else {
255  if (gradp) {
256  real gradn, grade;
257  real h = g(p.Latitude(), p.Longitude(), gradn, grade);
258  *output << Utility::str(h, 4) << " "
259  << Utility::str(gradn * 1e6, 2)
260  << (Math::isnan(gradn) ? " " : "e-6 ")
261  << Utility::str(grade * 1e6, 2)
262  << (Math::isnan(grade) ? "" : "e-6")
263  << eol;
264  } else {
265  real h = g(p.Latitude(), p.Longitude());
266  *output << Utility::str(h, 4) << eol;
267  }
268  }
269  }
270  catch (const std::exception& e) {
271  *output << "ERROR: " << e.what() << "\n";
272  retval = 1;
273  }
274  }
275  }
276  catch (const std::exception& e) {
277  std::cerr << "Error reading " << geoid << ": " << e.what() << "\n";
278  retval = 1;
279  }
280  return retval;
281  }
282  catch (const std::exception& e) {
283  std::cerr << "Caught exception: " << e.what() << "\n";
284  return 1;
285  }
286  catch (...) {
287  std::cerr << "Caught unknown exception\n";
288  return 1;
289  }
290 }
Math::real Latitude() const
Definition: GeoCoords.hpp:282
Math::real MaxError() const
Definition: Geoid.hpp:392
GeographicLib::Math::real real
Definition: GeodSolve.cpp:32
Math::real CacheWest() const
Definition: Geoid.hpp:432
Header for GeographicLib::Utility class.
static bool isnan(T x)
Definition: Math.hpp:628
Math::real CacheNorth() const
Definition: Geoid.hpp:451
Conversion between geographic coordinates.
Definition: GeoCoords.hpp:49
const std::string & DateTime() const
Definition: Geoid.hpp:361
static std::string DefaultGeoidPath()
Definition: Geoid.cpp:521
const std::string & GeoidFile() const
Definition: Geoid.hpp:366
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:427
void CacheArea(real south, real west, real north, real east) const
Definition: Geoid.cpp:442
const std::string Interpolation() const
Definition: Geoid.hpp:382
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:409
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
Math::real RMSError() const
Definition: Geoid.hpp:401
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:459
Math::real CacheEast() const
Definition: Geoid.hpp:441
const std::string & Description() const
Definition: Geoid.hpp:356
Math::real Scale() const
Definition: Geoid.hpp:417
Header for GeographicLib::Geoid class.
void CacheAll() const
Definition: Geoid.hpp:271
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.
Definition: Geoid.hpp:91
Header for GeographicLib::DMS class.