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