28int main(
int argc,
const char*
const argv[]) {
32 Utility::set_digits();
33 bool exact =
true, extended =
false, reverse =
false, longfirst =
false;
35 a = Constants::WGS84_a(),
36 f = Constants::WGS84_f(),
37 k0 = Constants::UTM_k0(),
40 std::string istring, ifile, ofile, cdelim;
43 for (
int m = 1; m < argc; ++m) {
44 std::string arg(argv[m]);
47 else if (arg ==
"-t") {
50 }
else if (arg ==
"-s") {
53 }
else if (arg ==
"-l") {
54 if (++m >= argc)
return usage(1,
true);
57 lon0 = DMS::Decode(std::string(argv[m]), ind);
58 if (ind == DMS::LATITUDE)
60 lon0 = Math::AngNormalize(lon0);
62 catch (
const std::exception& e) {
63 std::cerr <<
"Error decoding argument of " << arg <<
": "
67 }
else if (arg ==
"-k") {
68 if (++m >= argc)
return usage(1,
true);
70 k0 = Utility::val<real>(std::string(argv[m]));
72 catch (
const std::exception& e) {
73 std::cerr <<
"Error decoding argument of " << arg <<
": "
77 }
else if (arg ==
"-e") {
78 if (m + 2 >= argc)
return usage(1,
true);
80 a = Utility::val<real>(std::string(argv[m + 1]));
81 f = Utility::fract<real>(std::string(argv[m + 2]));
83 catch (
const std::exception& e) {
84 std::cerr <<
"Error decoding arguments of -e: " << e.what() <<
"\n";
88 }
else if (arg ==
"-w")
89 longfirst = !longfirst;
90 else if (arg ==
"-p") {
91 if (++m == argc)
return usage(1,
true);
93 prec = Utility::val<int>(std::string(argv[m]));
95 catch (
const std::exception&) {
96 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
99 }
else if (arg ==
"--input-string") {
100 if (++m == argc)
return usage(1,
true);
102 }
else if (arg ==
"--input-file") {
103 if (++m == argc)
return usage(1,
true);
105 }
else if (arg ==
"--output-file") {
106 if (++m == argc)
return usage(1,
true);
108 }
else if (arg ==
"--line-separator") {
109 if (++m == argc)
return usage(1,
true);
110 if (std::string(argv[m]).size() != 1) {
111 std::cerr <<
"Line separator must be a single character\n";
115 }
else if (arg ==
"--comment-delimiter") {
116 if (++m == argc)
return usage(1,
true);
118 }
else if (arg ==
"--version") {
119 std::cout << argv[0] <<
": GeographicLib version "
120 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
123 return usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
126 if (!ifile.empty() && !istring.empty()) {
127 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
130 if (ifile ==
"-") ifile.clear();
131 std::ifstream infile;
132 std::istringstream instring;
133 if (!ifile.empty()) {
134 infile.open(ifile.c_str());
135 if (!infile.is_open()) {
136 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
139 }
else if (!istring.empty()) {
140 std::string::size_type m = 0;
142 m = istring.find(lsep, m);
143 if (m == std::string::npos)
147 instring.str(istring);
149 std::istream* input = !ifile.empty() ? &infile :
150 (!istring.empty() ? &instring : &std::cin);
152 std::ofstream outfile;
153 if (ofile ==
"-") ofile.clear();
154 if (!ofile.empty()) {
155 outfile.open(ofile.c_str());
156 if (!outfile.is_open()) {
157 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
161 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
167 prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
168 std::string s, eol, stra, strb, strc;
169 std::istringstream str;
171 std::cout << std::fixed;
172 while (std::getline(*input, s)) {
175 if (!cdelim.empty()) {
176 std::string::size_type m = s.find(cdelim);
177 if (m != std::string::npos) {
178 eol =
" " + s.substr(m) +
"\n";
182 str.clear(); str.str(s);
184 if (!(str >> stra >> strb))
187 x = Utility::val<real>(stra);
188 y = Utility::val<real>(strb);
190 DMS::DecodeLatLon(stra, strb, lat, lon, longfirst);
195 TM.
Reverse(lon0, x, y, lat, lon, gamma, k);
196 *output << Utility::str(longfirst ? lon : lat, prec + 5) <<
" "
197 << Utility::str(longfirst ? lat : lon, prec + 5) <<
" "
198 << Utility::str(gamma, prec + 6) <<
" "
199 << Utility::str(k, prec + 6) << eol;
201 TM.
Forward(lon0, lat, lon, x, y, gamma, k);
202 *output << Utility::str(x, prec) <<
" "
203 << Utility::str(y, prec) <<
" "
204 << Utility::str(gamma, prec + 6) <<
" "
205 << Utility::str(k, prec + 6) << eol;
208 catch (
const std::exception& e) {
209 *output <<
"ERROR: " << e.what() <<
"\n";
215 catch (
const std::exception& e) {
216 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
220 std::cerr <<
"Caught unknown exception\n";