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