28int main(
int argc,
const char*
const argv[]) {
32 Utility::set_digits();
33 bool localcartesian =
false, reverse =
false, longfirst =
false;
35 a = Constants::WGS84_a(),
36 f = Constants::WGS84_f();
38 real lat0 = 0, lon0 = 0, h0 = 0;
39 std::string istring, ifile, ofile, cdelim;
42 for (
int m = 1; m < argc; ++m) {
43 std::string arg(argv[m]);
46 else if (arg ==
"-l") {
47 localcartesian =
true;
48 if (m + 3 >= argc)
return usage(1,
true);
50 DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),
51 lat0, lon0, longfirst);
52 h0 = Utility::val<real>(std::string(argv[m + 3]));
54 catch (
const std::exception& e) {
55 std::cerr <<
"Error decoding arguments of -l: " << e.what() <<
"\n";
59 }
else if (arg ==
"-e") {
60 if (m + 2 >= argc)
return usage(1,
true);
62 a = Utility::val<real>(std::string(argv[m + 1]));
63 f = Utility::fract<real>(std::string(argv[m + 2]));
65 catch (
const std::exception& e) {
66 std::cerr <<
"Error decoding arguments of -e: " << e.what() <<
"\n";
70 }
else if (arg ==
"-w")
71 longfirst = !longfirst;
72 else if (arg ==
"-p") {
73 if (++m == argc)
return usage(1,
true);
75 prec = Utility::val<int>(std::string(argv[m]));
77 catch (
const std::exception&) {
78 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
81 }
else if (arg ==
"--input-string") {
82 if (++m == argc)
return usage(1,
true);
84 }
else if (arg ==
"--input-file") {
85 if (++m == argc)
return usage(1,
true);
87 }
else if (arg ==
"--output-file") {
88 if (++m == argc)
return usage(1,
true);
90 }
else if (arg ==
"--line-separator") {
91 if (++m == argc)
return usage(1,
true);
92 if (std::string(argv[m]).size() != 1) {
93 std::cerr <<
"Line separator must be a single character\n";
97 }
else if (arg ==
"--comment-delimiter") {
98 if (++m == argc)
return usage(1,
true);
100 }
else if (arg ==
"--version") {
101 std::cout << argv[0] <<
": GeographicLib version "
102 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
105 return usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
108 if (!ifile.empty() && !istring.empty()) {
109 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
112 if (ifile ==
"-") ifile.clear();
113 std::ifstream infile;
114 std::istringstream instring;
115 if (!ifile.empty()) {
116 infile.open(ifile.c_str());
117 if (!infile.is_open()) {
118 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
121 }
else if (!istring.empty()) {
122 std::string::size_type m = 0;
124 m = istring.find(lsep, m);
125 if (m == std::string::npos)
129 instring.str(istring);
131 std::istream* input = !ifile.empty() ? &infile :
132 (!istring.empty() ? &instring : &std::cin);
134 std::ofstream outfile;
135 if (ofile ==
"-") ofile.clear();
136 if (!ofile.empty()) {
137 outfile.open(ofile.c_str());
138 if (!outfile.is_open()) {
139 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
143 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
150 prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
151 std::string s, eol, stra, strb, strc, strd;
152 std::istringstream str;
154 while (std::getline(*input, s)) {
157 if (!cdelim.empty()) {
158 std::string::size_type m = s.find(cdelim);
159 if (m != std::string::npos) {
160 eol =
" " + s.substr(m) +
"\n";
164 str.clear(); str.str(s);
166 real lat, lon, h, x = 0, y = 0, z = 0;
167 if (!(str >> stra >> strb >> strc))
170 x = Utility::val<real>(stra);
171 y = Utility::val<real>(strb);
172 z = Utility::val<real>(strc);
174 DMS::DecodeLatLon(stra, strb, lat, lon, longfirst);
175 h = Utility::val<real>(strc);
181 lc.
Reverse(x, y, z, lat, lon, h);
183 ec.
Reverse(x, y, z, lat, lon, h);
184 *output << Utility::str(longfirst ? lon : lat, prec + 5) <<
" "
185 << Utility::str(longfirst ? lat : lon, prec + 5) <<
" "
186 << Utility::str(h, prec) << eol;
189 lc.
Forward(lat, lon, h, x, y, z);
191 ec.
Forward(lat, lon, h, x, y, z);
192 *output << Utility::str(x, prec) <<
" "
193 << Utility::str(y, prec) <<
" "
194 << Utility::str(z, prec) << eol;
197 catch (
const std::exception& e) {
198 *output <<
"ERROR: " << e.what() <<
"\n";
204 catch (
const std::exception& e) {
205 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
209 std::cerr <<
"Caught unknown exception\n";