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