28int main(
int argc,
const char*
const argv[]) {
32 Utility::set_digits();
33 bool lcc =
false, albers =
false, reverse =
false, longfirst =
false;
34 real lat1 = 0, lat2 = 0, lon0 = 0, k1 = 1;
36 a = Constants::WGS84_a(),
37 f = Constants::WGS84_f();
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 ==
"-c" || arg ==
"-a") {
49 if (m + 2 >= argc)
return usage(1,
true);
51 for (
int i = 0; i < 2; ++i) {
53 (i ? lat2 : lat1) = DMS::Decode(std::string(argv[++m]), ind);
54 if (ind == DMS::LONGITUDE)
58 catch (
const std::exception& e) {
59 std::cerr <<
"Error decoding arguments of " << arg <<
": "
63 }
else if (arg ==
"-l") {
64 if (++m == argc)
return usage(1,
true);
67 lon0 = DMS::Decode(std::string(argv[m]), ind);
68 if (ind == DMS::LATITUDE)
70 lon0 = Math::AngNormalize(lon0);
72 catch (
const std::exception& e) {
73 std::cerr <<
"Error decoding argument of " << arg <<
": "
77 }
else if (arg ==
"-k") {
78 if (++m == argc)
return usage(1,
true);
80 k1 = Utility::val<real>(std::string(argv[m]));
82 catch (
const std::exception& e) {
83 std::cerr <<
"Error decoding argument of " << arg <<
": "
87 }
else if (arg ==
"-e") {
88 if (m + 2 >= argc)
return usage(1,
true);
90 a = Utility::val<real>(std::string(argv[m + 1]));
91 f = Utility::fract<real>(std::string(argv[m + 2]));
93 catch (
const std::exception& e) {
94 std::cerr <<
"Error decoding arguments of -e: " << e.what() <<
"\n";
98 }
else if (arg ==
"-w")
99 longfirst = !longfirst;
100 else if (arg ==
"-p") {
101 if (++m == argc)
return usage(1,
true);
103 prec = Utility::val<int>(std::string(argv[m]));
105 catch (
const std::exception&) {
106 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
109 }
else if (arg ==
"--input-string") {
110 if (++m == argc)
return usage(1,
true);
112 }
else if (arg ==
"--input-file") {
113 if (++m == argc)
return usage(1,
true);
115 }
else if (arg ==
"--output-file") {
116 if (++m == argc)
return usage(1,
true);
118 }
else if (arg ==
"--line-separator") {
119 if (++m == argc)
return usage(1,
true);
120 if (std::string(argv[m]).size() != 1) {
121 std::cerr <<
"Line separator must be a single character\n";
125 }
else if (arg ==
"--comment-delimiter") {
126 if (++m == argc)
return usage(1,
true);
128 }
else if (arg ==
"--version") {
129 std::cout << argv[0] <<
": GeographicLib version "
130 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
133 return usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
136 if (!ifile.empty() && !istring.empty()) {
137 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
140 if (ifile ==
"-") ifile.clear();
141 std::ifstream infile;
142 std::istringstream instring;
143 if (!ifile.empty()) {
144 infile.open(ifile.c_str());
145 if (!infile.is_open()) {
146 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
149 }
else if (!istring.empty()) {
150 std::string::size_type m = 0;
152 m = istring.find(lsep, m);
153 if (m == std::string::npos)
157 instring.str(istring);
159 std::istream* input = !ifile.empty() ? &infile :
160 (!istring.empty() ? &instring : &std::cin);
162 std::ofstream outfile;
163 if (ofile ==
"-") ofile.clear();
164 if (!ofile.empty()) {
165 outfile.open(ofile.c_str());
166 if (!outfile.is_open()) {
167 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
171 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
173 if (!(lcc || albers)) {
174 std::cerr <<
"Must specify \"-c lat1 lat2\" or "
175 <<
"\"-a lat1 lat2\"\n";
188 prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
189 std::string s, eol, stra, strb, strc;
190 std::istringstream str;
192 while (std::getline(*input, s)) {
195 if (!cdelim.empty()) {
196 std::string::size_type m = s.find(cdelim);
197 if (m != std::string::npos) {
198 eol =
" " + s.substr(m) +
"\n";
202 str.clear(); str.str(s);
203 real lat, lon, x, y, gamma, k;
204 if (!(str >> stra >> strb))
207 x = Utility::val<real>(stra);
208 y = Utility::val<real>(strb);
210 DMS::DecodeLatLon(stra, strb, lat, lon, longfirst);
215 lproj.
Reverse(lon0, x, y, lat, lon, gamma, k);
217 aproj.
Reverse(lon0, x, y, lat, lon, gamma, k);
218 *output << Utility::str(longfirst ? lon : lat, prec + 5) <<
" "
219 << Utility::str(longfirst ? lat : lon, prec + 5) <<
" "
220 << Utility::str(gamma, prec + 6) <<
" "
221 << Utility::str(k, prec + 6) << eol;
224 lproj.
Forward(lon0, lat, lon, x, y, gamma, k);
226 aproj.
Forward(lon0, lat, lon, x, y, gamma, k);
227 *output << Utility::str(x, prec) <<
" "
228 << Utility::str(y, prec) <<
" "
229 << Utility::str(gamma, prec + 6) <<
" "
230 << Utility::str(k, prec + 6) << eol;
233 catch (
const std::exception& e) {
234 *output <<
"ERROR: " << e.what() <<
"\n";
240 catch (
const std::exception& e) {
241 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
245 std::cerr <<
"Caught unknown exception\n";