GeographicLib 2.1.2
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-2017) <charles@karney.com> and licensed
6 * under the MIT/X11 License. For more information, see
7 * https://geographiclib.sourceforge.io/
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>
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
29int main(int argc, const char* const argv[]) {
30 try {
31 using namespace GeographicLib;
32 typedef Math::real real;
33 Utility::set_digits();
34 bool cacheall = false, cachearea = false, verbose = false, cubic = true;
35 real caches, cachew, cachen, cachee;
36 std::string dir;
37 std::string geoid = Geoid::DefaultGeoidName();
38 Geoid::convertflag heightmult = Geoid::NONE;
39 std::string istring, ifile, ofile, cdelim;
40 char lsep = ';';
41 bool northp = false, longfirst = false;
42 int zonenum = UTMUPS::INVALID;
43
44 for (int m = 1; m < argc; ++m) {
45 std::string arg(argv[m]);
46 if (arg == "-a") {
47 cacheall = true;
48 cachearea = false;
49 }
50 else if (arg == "-c") {
51 if (m + 4 >= argc) return usage(1, true);
52 cacheall = false;
53 cachearea = true;
54 try {
55 DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),
56 caches, cachew, longfirst);
57 DMS::DecodeLatLon(std::string(argv[m + 3]), std::string(argv[m + 4]),
58 cachen, cachee, longfirst);
59 }
60 catch (const std::exception& e) {
61 std::cerr << "Error decoding argument of -c: " << e.what() << "\n";
62 return 1;
63 }
64 m += 4;
65 } else if (arg == "--msltohae")
66 heightmult = Geoid::GEOIDTOELLIPSOID;
67 else if (arg == "--haetomsl")
68 heightmult = Geoid::ELLIPSOIDTOGEOID;
69 else if (arg == "-w")
70 longfirst = !longfirst;
71 else if (arg == "-z") {
72 if (++m == argc) return usage(1, true);
73 std::string zone = argv[m];
74 try {
75 UTMUPS::DecodeZone(zone, zonenum, northp);
76 }
77 catch (const std::exception& e) {
78 std::cerr << "Error decoding zone: " << e.what() << "\n";
79 return 1;
80 }
81 if (!(zonenum >= UTMUPS::MINZONE && zonenum <= UTMUPS::MAXZONE)) {
82 std::cerr << "Illegal zone " << zone << "\n";
83 return 1;
84 }
85 } else if (arg == "-n") {
86 if (++m == argc) return usage(1, true);
87 geoid = argv[m];
88 } else if (arg == "-d") {
89 if (++m == argc) return usage(1, true);
90 dir = argv[m];
91 } else if (arg == "-l")
92 cubic = false;
93 else if (arg == "-v")
94 verbose = true;
95 else if (arg == "--input-string") {
96 if (++m == argc) return usage(1, true);
97 istring = argv[m];
98 } else if (arg == "--input-file") {
99 if (++m == argc) return usage(1, true);
100 ifile = argv[m];
101 } else if (arg == "--output-file") {
102 if (++m == argc) return usage(1, true);
103 ofile = argv[m];
104 } else if (arg == "--line-separator") {
105 if (++m == argc) return usage(1, true);
106 if (std::string(argv[m]).size() != 1) {
107 std::cerr << "Line separator must be a single character\n";
108 return 1;
109 }
110 lsep = argv[m][0];
111 } else if (arg == "--comment-delimiter") {
112 if (++m == argc) return usage(1, true);
113 cdelim = argv[m];
114 } else if (arg == "--version") {
115 std::cout << argv[0] << ": GeographicLib version "
116 << GEOGRAPHICLIB_VERSION_STRING << "\n";
117 return 0;
118 } else {
119 int retval = usage(!(arg == "-h" || arg == "--help"), arg != "--help");
120 if (arg == "-h")
121 std::cout
122 << "\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
189 << "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, eol, suff;
197 const char* spaces = " \t\n\v\f\r,"; // Include comma as space
198 while (std::getline(*input, s)) {
199 try {
200 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::val<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::val<real>(s.substr(pa + 1, pb - pa));
245 s = s.substr(0, pa + 1);
246 }
247 p.Reset(s, true, longfirst);
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 real h = g(p.Latitude(), p.Longitude());
256 *output << Utility::str(h, 4) << eol;
257 }
258 }
259 catch (const std::exception& e) {
260 *output << "ERROR: " << e.what() << "\n";
261 retval = 1;
262 }
263 }
264 }
265 catch (const std::exception& e) {
266 std::cerr << "Error reading " << geoid << ": " << e.what() << "\n";
267 retval = 1;
268 }
269 return retval;
270 }
271 catch (const std::exception& e) {
272 std::cerr << "Caught exception: " << e.what() << "\n";
273 return 1;
274 }
275 catch (...) {
276 std::cerr << "Caught unknown exception\n";
277 return 1;
278 }
279}
Header for GeographicLib::DMS class.
Header for GeographicLib::GeoCoords class.
GeographicLib::Math::real real
Definition: GeodSolve.cpp:31
int main(int argc, const char *const argv[])
Definition: GeoidEval.cpp:29
Header for GeographicLib::Geoid class.
Header for GeographicLib::Utility class.
Conversion between geographic coordinates.
Definition: GeoCoords.hpp:49
void Reset(const std::string &s, bool centerp=true, bool longfirst=false)
Definition: GeoCoords.cpp:19
Math::real Latitude() const
Definition: GeoCoords.hpp:276
Math::real Longitude() const
Definition: GeoCoords.hpp:281
Exception handling for GeographicLib.
Definition: Constants.hpp:316
Looking up the height of the geoid above the ellipsoid.
Definition: Geoid.hpp:82
Math::real Scale() const
Definition: Geoid.hpp:379
Math::real MaxError() const
Definition: Geoid.hpp:354
const std::string & Description() const
Definition: Geoid.hpp:318
Math::real CacheEast() const
Definition: Geoid.hpp:403
bool Cache() const
Definition: Geoid.hpp:389
Math::real RMSError() const
Definition: Geoid.hpp:363
const std::string & GeoidFile() const
Definition: Geoid.hpp:328
Math::real CacheSouth() const
Definition: Geoid.hpp:421
void CacheAll() const
Definition: Geoid.hpp:258
const std::string Interpolation() const
Definition: Geoid.hpp:344
Math::real CacheNorth() const
Definition: Geoid.hpp:413
Math::real Offset() const
Definition: Geoid.hpp:371
void CacheArea(real south, real west, real north, real east) const
Definition: Geoid.cpp:408
const std::string & DateTime() const
Definition: Geoid.hpp:323
Math::real CacheWest() const
Definition: Geoid.hpp:394
Namespace for GeographicLib.
Definition: Accumulator.cpp:12