30int main(
int argc,
const char*
const argv[]) {
34 Utility::set_digits();
35 bool azimuthal =
false, cassini =
false, gnomonic =
false, reverse =
false,
37 real lat0 = 0, lon0 = 0;
39 a = Constants::WGS84_a(),
40 f = Constants::WGS84_f();
42 std::string istring, ifile, ofile, cdelim;
45 for (
int m = 1; m < argc; ++m) {
46 std::string arg(argv[m]);
49 else if (arg ==
"-c" || arg ==
"-z" || arg ==
"-g") {
50 cassini = azimuthal = gnomonic =
false;
51 cassini = arg ==
"-c";
52 azimuthal = arg ==
"-z";
53 gnomonic = arg ==
"-g";
54 if (m + 2 >= argc)
return usage(1,
true);
56 DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),
57 lat0, lon0, longfirst);
59 catch (
const std::exception& e) {
60 std::cerr <<
"Error decoding arguments of " << arg <<
": "
65 }
else if (arg ==
"-e") {
66 if (m + 2 >= argc)
return usage(1,
true);
68 a = Utility::val<real>(std::string(argv[m + 1]));
69 f = Utility::fract<real>(std::string(argv[m + 2]));
71 catch (
const std::exception& e) {
72 std::cerr <<
"Error decoding arguments of -e: " << e.what() <<
"\n";
76 }
else if (arg ==
"-w")
77 longfirst = !longfirst;
78 else if (arg ==
"-p") {
79 if (++m == argc)
return usage(1,
true);
81 prec = Utility::val<int>(std::string(argv[m]));
83 catch (
const std::exception&) {
84 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
87 }
else if (arg ==
"--input-string") {
88 if (++m == argc)
return usage(1,
true);
90 }
else if (arg ==
"--input-file") {
91 if (++m == argc)
return usage(1,
true);
93 }
else if (arg ==
"--output-file") {
94 if (++m == argc)
return usage(1,
true);
96 }
else if (arg ==
"--line-separator") {
97 if (++m == argc)
return usage(1,
true);
98 if (std::string(argv[m]).size() != 1) {
99 std::cerr <<
"Line separator must be a single character\n";
103 }
else if (arg ==
"--comment-delimiter") {
104 if (++m == argc)
return usage(1,
true);
106 }
else if (arg ==
"--version") {
107 std::cout << argv[0] <<
": GeographicLib version "
108 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
111 return usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
114 if (!ifile.empty() && !istring.empty()) {
115 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
118 if (ifile ==
"-") ifile.clear();
119 std::ifstream infile;
120 std::istringstream instring;
121 if (!ifile.empty()) {
122 infile.open(ifile.c_str());
123 if (!infile.is_open()) {
124 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
127 }
else if (!istring.empty()) {
128 std::string::size_type m = 0;
130 m = istring.find(lsep, m);
131 if (m == std::string::npos)
135 instring.str(istring);
137 std::istream* input = !ifile.empty() ? &infile :
138 (!istring.empty() ? &instring : &std::cin);
140 std::ofstream outfile;
141 if (ofile ==
"-") ofile.clear();
142 if (!ofile.empty()) {
143 outfile.open(ofile.c_str());
144 if (!outfile.is_open()) {
145 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
149 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
151 if (!(azimuthal || cassini || gnomonic)) {
152 std::cerr <<
"Must specify \"-z lat0 lon0\" or "
153 <<
"\"-c lat0 lon0\" or \"-g lat0 lon0\"\n";
165 prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
166 std::string s, eol, stra, strb, strc;
167 std::istringstream str;
169 std::cout << std::fixed;
170 while (std::getline(*input, s)) {
173 if (!cdelim.empty()) {
174 std::string::size_type m = s.find(cdelim);
175 if (m != std::string::npos) {
176 eol =
" " + s.substr(m) +
"\n";
180 str.clear(); str.str(s);
181 real lat, lon, x, y, azi, rk;
182 if (!(str >> stra >> strb))
185 x = Utility::val<real>(stra);
186 y = Utility::val<real>(strb);
188 DMS::DecodeLatLon(stra, strb, lat, lon, longfirst);
193 cs.
Reverse(x, y, lat, lon, azi, rk);
195 az.
Reverse(lat0, lon0, x, y, lat, lon, azi, rk);
197 gn.
Reverse(lat0, lon0, x, y, lat, lon, azi, rk);
198 *output << Utility::str(longfirst ? lon : lat, prec + 5) <<
" "
199 << Utility::str(longfirst ? lat : lon, prec + 5) <<
" "
200 << Utility::str(azi, prec + 5) <<
" "
201 << Utility::str(rk, prec + 6) << eol;
204 cs.
Forward(lat, lon, x, y, azi, rk);
206 az.
Forward(lat0, lon0, lat, lon, x, y, azi, rk);
208 gn.
Forward(lat0, lon0, lat, lon, x, y, azi, rk);
209 *output << Utility::str(x, prec) <<
" "
210 << Utility::str(y, prec) <<
" "
211 << Utility::str(azi, prec + 5) <<
" "
212 << Utility::str(rk, prec + 6) << eol;
215 catch (
const std::exception& e) {
216 *output <<
"ERROR: " << e.what() <<
"\n";
222 catch (
const std::exception& e) {
223 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
227 std::cerr <<
"Caught unknown exception\n";