28int main(
int argc,
const char*
const argv[]) {
32 Utility::set_digits();
33 bool cacheall =
false, cachearea =
false, verbose =
false, cubic =
true;
34 real caches, cachew, cachen, cachee;
36 std::string geoid = Geoid::DefaultGeoidName();
38 std::string istring, ifile, ofile, cdelim;
40 bool northp =
false, longfirst =
false;
41 int zonenum = UTMUPS::INVALID;
43 for (
int m = 1; m < argc; ++m) {
44 std::string arg(argv[m]);
49 else if (arg ==
"-c") {
50 if (m + 4 >= argc)
return usage(1,
true);
54 DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),
55 caches, cachew, longfirst);
56 DMS::DecodeLatLon(std::string(argv[m + 3]), std::string(argv[m + 4]),
57 cachen, cachee, longfirst);
59 catch (
const std::exception& e) {
60 std::cerr <<
"Error decoding argument of -c: " << e.what() <<
"\n";
64 }
else if (arg ==
"--msltohae")
65 heightmult = Geoid::GEOIDTOELLIPSOID;
66 else if (arg ==
"--haetomsl")
67 heightmult = Geoid::ELLIPSOIDTOGEOID;
69 longfirst = !longfirst;
70 else if (arg ==
"-z") {
71 if (++m == argc)
return usage(1,
true);
72 std::string zone = argv[m];
74 UTMUPS::DecodeZone(zone, zonenum, northp);
76 catch (
const std::exception& e) {
77 std::cerr <<
"Error decoding zone: " << e.what() <<
"\n";
80 if (!(zonenum >= UTMUPS::MINZONE && zonenum <= UTMUPS::MAXZONE)) {
81 std::cerr <<
"Illegal zone " << zone <<
"\n";
84 }
else if (arg ==
"-n") {
85 if (++m == argc)
return usage(1,
true);
87 }
else if (arg ==
"-d") {
88 if (++m == argc)
return usage(1,
true);
90 }
else if (arg ==
"-l")
94 else if (arg ==
"--input-string") {
95 if (++m == argc)
return usage(1,
true);
97 }
else if (arg ==
"--input-file") {
98 if (++m == argc)
return usage(1,
true);
100 }
else if (arg ==
"--output-file") {
101 if (++m == argc)
return usage(1,
true);
103 }
else if (arg ==
"--line-separator") {
104 if (++m == argc)
return usage(1,
true);
105 if (std::string(argv[m]).size() != 1) {
106 std::cerr <<
"Line separator must be a single character\n";
110 }
else if (arg ==
"--comment-delimiter") {
111 if (++m == argc)
return usage(1,
true);
113 }
else if (arg ==
"--version") {
114 std::cout << argv[0] <<
": GeographicLib version "
115 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
118 int retval = usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
121 <<
"\nDefault geoid path = \"" << Geoid::DefaultGeoidPath()
122 <<
"\"\nDefault geoid name = \"" << Geoid::DefaultGeoidName()
128 if (!ifile.empty() && !istring.empty()) {
129 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
132 if (ifile ==
"-") ifile.clear();
133 std::ifstream infile;
134 std::istringstream instring;
135 if (!ifile.empty()) {
136 infile.open(ifile.c_str());
137 if (!infile.is_open()) {
138 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
141 }
else if (!istring.empty()) {
142 std::string::size_type m = 0;
144 m = istring.find(lsep, m);
145 if (m == std::string::npos)
149 instring.str(istring);
151 std::istream* input = !ifile.empty() ? &infile :
152 (!istring.empty() ? &instring : &std::cin);
154 std::ofstream outfile;
155 if (ofile ==
"-") ofile.clear();
156 if (!ofile.empty()) {
157 outfile.open(ofile.c_str());
158 if (!outfile.is_open()) {
159 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
163 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
167 const Geoid g(geoid, dir, cubic);
172 g.
CacheArea(caches, cachew, cachen, cachee);
174 catch (
const std::exception& e) {
175 std::cerr <<
"ERROR: " << e.what() <<
"\nProceeding without a cache\n";
178 std::cerr <<
"Geoid file: " << g.
GeoidFile() <<
"\n"
181 <<
"Date & Time: " << g.
DateTime() <<
"\n"
182 <<
"Offset (m): " << g.
Offset() <<
"\n"
183 <<
"Scale (m): " << g.
Scale() <<
"\n"
184 <<
"Max error (m): " << g.
MaxError() <<
"\n"
185 <<
"RMS error (m): " << g.
RMSError() <<
"\n";
195 std::string s, eol, suff;
196 const char* spaces =
" \t\n\v\f\r,";
197 while (std::getline(*input, s)) {
200 if (!cdelim.empty()) {
201 std::string::size_type m = s.find(cdelim);
202 if (m != std::string::npos) {
203 eol =
" " + s.substr(m) +
"\n";
204 std::string::size_type m1 =
205 m > 0 ? s.find_last_not_of(spaces, m - 1) : std::string::npos;
206 s = s.substr(0, m1 != std::string::npos ? m1 + 1 : m);
210 if (zonenum != UTMUPS::INVALID) {
213 std::string::size_type pa = 0, pb = 0;
214 real easting = 0, northing = 0;
215 for (
int i = 0; i < (heightmult ? 3 : 2); ++i) {
216 if (pb == std::string::npos)
219 pa = s.find_first_not_of(spaces, pb);
220 if (pa == std::string::npos)
223 pb = s.find_first_of(spaces, pa);
224 (i == 2 ? height : (i == 0 ? easting : northing)) =
225 Utility::val<real>(s.substr(pa, (pb == std::string::npos ?
228 p.
Reset(zonenum, northp, easting, northing);
230 suff = pb == std::string::npos ?
"" : s.substr(pb);
239 std::string::size_type pb = s.find_last_not_of(spaces);
240 std::string::size_type pa = s.find_last_of(spaces, pb);
241 if (pa == std::string::npos || pb == std::string::npos)
243 height = Utility::val<real>(s.substr(pa + 1, pb - pa));
244 s = s.substr(0, pa + 1);
246 p.
Reset(s,
true, longfirst);
251 << Utility::str(height +
real(heightmult) * h, 4)
255 *output << Utility::str(h, 4) << eol;
258 catch (
const std::exception& e) {
259 *output <<
"ERROR: " << e.what() <<
"\n";
264 catch (
const std::exception& e) {
265 std::cerr <<
"Error reading " << geoid <<
": " << e.what() <<
"\n";
270 catch (
const std::exception& e) {
271 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
275 std::cerr <<
"Caught unknown exception\n";