24# pragma warning (disable: 4127 4701)
27#include "GeoidEval.usage"
29int main(
int argc,
const char*
const argv[]) {
33 Utility::set_digits();
34 bool cacheall =
false, cachearea =
false, verbose =
false, cubic =
true;
35 real caches, cachew, cachen, cachee;
37 std::string geoid = Geoid::DefaultGeoidName();
39 std::string istring, ifile, ofile, cdelim;
41 bool northp =
false, longfirst =
false;
42 int zonenum = UTMUPS::INVALID;
44 for (
int m = 1; m < argc; ++m) {
45 std::string arg(argv[m]);
50 else if (arg ==
"-c") {
51 if (m + 4 >= argc)
return usage(1,
true);
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);
60 catch (
const std::exception& e) {
61 std::cerr <<
"Error decoding argument of -c: " << e.what() <<
"\n";
65 }
else if (arg ==
"--msltohae")
66 heightmult = Geoid::GEOIDTOELLIPSOID;
67 else if (arg ==
"--haetomsl")
68 heightmult = Geoid::ELLIPSOIDTOGEOID;
70 longfirst = !longfirst;
71 else if (arg ==
"-z") {
72 if (++m == argc)
return usage(1,
true);
73 std::string zone = argv[m];
75 UTMUPS::DecodeZone(zone, zonenum, northp);
77 catch (
const std::exception& e) {
78 std::cerr <<
"Error decoding zone: " << e.what() <<
"\n";
81 if (!(zonenum >= UTMUPS::MINZONE && zonenum <= UTMUPS::MAXZONE)) {
82 std::cerr <<
"Illegal zone " << zone <<
"\n";
85 }
else if (arg ==
"-n") {
86 if (++m == argc)
return usage(1,
true);
88 }
else if (arg ==
"-d") {
89 if (++m == argc)
return usage(1,
true);
91 }
else if (arg ==
"-l")
95 else if (arg ==
"--input-string") {
96 if (++m == argc)
return usage(1,
true);
98 }
else if (arg ==
"--input-file") {
99 if (++m == argc)
return usage(1,
true);
101 }
else if (arg ==
"--output-file") {
102 if (++m == argc)
return usage(1,
true);
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";
111 }
else if (arg ==
"--comment-delimiter") {
112 if (++m == argc)
return usage(1,
true);
114 }
else if (arg ==
"--version") {
115 std::cout << argv[0] <<
": GeographicLib version "
116 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
119 int retval = usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
122 <<
"\nDefault geoid path = \"" << Geoid::DefaultGeoidPath()
123 <<
"\"\nDefault geoid name = \"" << Geoid::DefaultGeoidName()
129 if (!ifile.empty() && !istring.empty()) {
130 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
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";
142 }
else if (!istring.empty()) {
143 std::string::size_type m = 0;
145 m = istring.find(lsep, m);
146 if (m == std::string::npos)
150 instring.str(istring);
152 std::istream* input = !ifile.empty() ? &infile :
153 (!istring.empty() ? &instring : &std::cin);
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";
164 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
168 const Geoid g(geoid, dir, cubic);
173 g.
CacheArea(caches, cachew, cachen, cachee);
175 catch (
const std::exception& e) {
176 std::cerr <<
"ERROR: " << e.what() <<
"\nProceeding without a cache\n";
179 std::cerr <<
"Geoid file: " << g.
GeoidFile() <<
"\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";
196 std::string s, eol, suff;
197 const char* spaces =
" \t\n\v\f\r,";
198 while (std::getline(*input, s)) {
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);
211 if (zonenum != UTMUPS::INVALID) {
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)
220 pa = s.find_first_not_of(spaces, pb);
221 if (pa == std::string::npos)
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 ?
229 p.
Reset(zonenum, northp, easting, northing);
231 suff = pb == std::string::npos ?
"" : s.substr(pb);
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)
244 height = Utility::val<real>(s.substr(pa + 1, pb - pa));
245 s = s.substr(0, pa + 1);
247 p.
Reset(s,
true, longfirst);
252 << Utility::str(height +
real(heightmult) * h, 4)
256 *output << Utility::str(h, 4) << eol;
259 catch (
const std::exception& e) {
260 *output <<
"ERROR: " << e.what() <<
"\n";
265 catch (
const std::exception& e) {
266 std::cerr <<
"Error reading " << geoid <<
": " << e.what() <<
"\n";
271 catch (
const std::exception& e) {
272 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
276 std::cerr <<
"Caught unknown exception\n";
Header for GeographicLib::DMS class.
Header for GeographicLib::GeoCoords class.
GeographicLib::Math::real real
int main(int argc, const char *const argv[])
Header for GeographicLib::Geoid class.
Header for GeographicLib::Utility class.
Conversion between geographic coordinates.
void Reset(const std::string &s, bool centerp=true, bool longfirst=false)
Math::real Latitude() const
Math::real Longitude() const
Exception handling for GeographicLib.
Looking up the height of the geoid above the ellipsoid.
Math::real MaxError() const
const std::string & Description() const
Math::real CacheEast() const
Math::real RMSError() const
const std::string & GeoidFile() const
Math::real CacheSouth() const
const std::string Interpolation() const
Math::real CacheNorth() const
Math::real Offset() const
void CacheArea(real south, real west, real north, real east) const
const std::string & DateTime() const
Math::real CacheWest() const
Namespace for GeographicLib.