38 # pragma warning (disable: 4127 4701)
41 #include "GeodesicProj.usage"
43 int main(
int argc,
char* argv[]) {
45 using namespace GeographicLib;
48 bool azimuthal =
false, cassini =
false, gnomonic =
false, reverse =
false;
49 real lat0 = 0, lon0 = 0;
54 std::string istring, ifile, ofile, cdelim;
57 for (
int m = 1; m < argc; ++m) {
58 std::string arg(argv[m]);
61 else if (arg ==
"-c" || arg ==
"-z" || arg ==
"-g") {
62 cassini = arg ==
"-c";
63 azimuthal = arg ==
"-z";
64 gnomonic = arg ==
"-g";
65 if (m + 2 >= argc)
return usage(1,
true);
70 catch (
const std::exception& e) {
71 std::cerr <<
"Error decoding arguments of " << arg <<
": "
76 }
else if (arg ==
"-e") {
77 if (m + 2 >= argc)
return usage(1,
true);
79 a = Utility::num<real>(std::string(argv[m + 1]));
80 f = Utility::fract<real>(std::string(argv[m + 2]));
82 catch (
const std::exception& e) {
83 std::cerr <<
"Error decoding arguments of -e: " << e.what() <<
"\n";
87 }
else if (arg ==
"-p") {
88 if (++m == argc)
return usage(1,
true);
90 prec = Utility::num<int>(std::string(argv[m]));
92 catch (
const std::exception&) {
93 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
96 }
else if (arg ==
"--input-string") {
97 if (++m == argc)
return usage(1,
true);
99 }
else if (arg ==
"--input-file") {
100 if (++m == argc)
return usage(1,
true);
102 }
else if (arg ==
"--output-file") {
103 if (++m == argc)
return usage(1,
true);
105 }
else if (arg ==
"--line-separator") {
106 if (++m == argc)
return usage(1,
true);
107 if (std::string(argv[m]).size() != 1) {
108 std::cerr <<
"Line separator must be a single character\n";
112 }
else if (arg ==
"--comment-delimiter") {
113 if (++m == argc)
return usage(1,
true);
115 }
else if (arg ==
"--version") {
117 << argv[0] <<
": GeographicLib version "
118 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
121 return usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
124 if (!ifile.empty() && !istring.empty()) {
125 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
128 if (ifile ==
"-") ifile.clear();
129 std::ifstream infile;
130 std::istringstream instring;
131 if (!ifile.empty()) {
132 infile.open(ifile.c_str());
133 if (!infile.is_open()) {
134 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
137 }
else if (!istring.empty()) {
138 std::string::size_type m = 0;
140 m = istring.find(lsep, m);
141 if (m == std::string::npos)
145 instring.str(istring);
147 std::istream* input = !ifile.empty() ? &infile :
148 (!istring.empty() ? &instring : &std::cin);
150 std::ofstream outfile;
151 if (ofile ==
"-") ofile.clear();
152 if (!ofile.empty()) {
153 outfile.open(ofile.c_str());
154 if (!outfile.is_open()) {
155 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
159 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
161 if (!(azimuthal || cassini || gnomonic)) {
162 std::cerr <<
"Must specify \"-z lat0 lon0\" or "
163 <<
"\"-c lat0 lon0\" or \"-g lat0 lon0\"\n";
178 std::cout << std::fixed;
179 while (std::getline(*input, s)) {
181 std::string eol(
"\n");
182 if (!cdelim.empty()) {
183 std::string::size_type m = s.find(cdelim);
184 if (m != std::string::npos) {
185 eol =
" " + s.substr(m) +
"\n";
189 std::istringstream str(s);
190 real lat, lon, x, y, azi, rk;
191 std::string stra, strb;
192 if (!(str >> stra >> strb))
195 x = Utility::num<real>(stra);
196 y = Utility::num<real>(strb);
204 cs.Reverse(x, y, lat, lon, azi, rk);
206 az.
Reverse(lat0, lon0, x, y, lat, lon, azi, rk);
208 gn.
Reverse(lat0, lon0, x, y, lat, lon, azi, rk);
215 cs.Forward(lat, lon, x, y, azi, rk);
217 az.
Forward(lat0, lon0, lat, lon, x, y, azi, rk);
219 gn.
Forward(lat0, lon0, lat, lon, x, y, azi, rk);
226 catch (
const std::exception& e) {
227 *output <<
"ERROR: " << e.what() <<
"\n";
233 catch (
const std::exception& e) {
234 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
238 std::cerr <<
"Caught unknown exception\n";