26# pragma warning (disable: 4127 4701)
29#include "GeodesicProj.usage"
31int main(
int argc,
const char*
const argv[]) {
35 Utility::set_digits();
36 bool azimuthal =
false, cassini =
false, gnomonic =
false, reverse =
false,
38 real lat0 = 0, lon0 = 0;
40 a = Constants::WGS84_a(),
41 f = Constants::WGS84_f();
43 std::string istring, ifile, ofile, cdelim;
46 for (
int m = 1; m < argc; ++m) {
47 std::string arg(argv[m]);
50 else if (arg ==
"-c" || arg ==
"-z" || arg ==
"-g") {
51 cassini = azimuthal = gnomonic =
false;
52 cassini = arg ==
"-c";
53 azimuthal = arg ==
"-z";
54 gnomonic = arg ==
"-g";
55 if (m + 2 >= argc)
return usage(1,
true);
57 DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),
58 lat0, lon0, longfirst);
60 catch (
const std::exception& e) {
61 std::cerr <<
"Error decoding arguments of " << arg <<
": "
66 }
else if (arg ==
"-e") {
67 if (m + 2 >= argc)
return usage(1,
true);
69 a = Utility::val<real>(std::string(argv[m + 1]));
70 f = Utility::fract<real>(std::string(argv[m + 2]));
72 catch (
const std::exception& e) {
73 std::cerr <<
"Error decoding arguments of -e: " << e.what() <<
"\n";
77 }
else if (arg ==
"-w")
78 longfirst = !longfirst;
79 else if (arg ==
"-p") {
80 if (++m == argc)
return usage(1,
true);
82 prec = Utility::val<int>(std::string(argv[m]));
84 catch (
const std::exception&) {
85 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
88 }
else if (arg ==
"--input-string") {
89 if (++m == argc)
return usage(1,
true);
91 }
else if (arg ==
"--input-file") {
92 if (++m == argc)
return usage(1,
true);
94 }
else if (arg ==
"--output-file") {
95 if (++m == argc)
return usage(1,
true);
97 }
else if (arg ==
"--line-separator") {
98 if (++m == argc)
return usage(1,
true);
99 if (std::string(argv[m]).size() != 1) {
100 std::cerr <<
"Line separator must be a single character\n";
104 }
else if (arg ==
"--comment-delimiter") {
105 if (++m == argc)
return usage(1,
true);
107 }
else if (arg ==
"--version") {
108 std::cout << argv[0] <<
": GeographicLib version "
109 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
112 return usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
115 if (!ifile.empty() && !istring.empty()) {
116 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
119 if (ifile ==
"-") ifile.clear();
120 std::ifstream infile;
121 std::istringstream instring;
122 if (!ifile.empty()) {
123 infile.open(ifile.c_str());
124 if (!infile.is_open()) {
125 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
128 }
else if (!istring.empty()) {
129 std::string::size_type m = 0;
131 m = istring.find(lsep, m);
132 if (m == std::string::npos)
136 instring.str(istring);
138 std::istream* input = !ifile.empty() ? &infile :
139 (!istring.empty() ? &instring : &std::cin);
141 std::ofstream outfile;
142 if (ofile ==
"-") ofile.clear();
143 if (!ofile.empty()) {
144 outfile.open(ofile.c_str());
145 if (!outfile.is_open()) {
146 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
150 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
152 if (!(azimuthal || cassini || gnomonic)) {
153 std::cerr <<
"Must specify \"-z lat0 lon0\" or "
154 <<
"\"-c lat0 lon0\" or \"-g lat0 lon0\"\n";
166 prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
167 std::string s, eol, stra, strb, strc;
168 std::istringstream str;
170 std::cout << std::fixed;
171 while (std::getline(*input, s)) {
174 if (!cdelim.empty()) {
175 std::string::size_type m = s.find(cdelim);
176 if (m != std::string::npos) {
177 eol =
" " + s.substr(m) +
"\n";
181 str.clear(); str.str(s);
182 real lat, lon, x, y, azi, rk;
183 if (!(str >> stra >> strb))
186 x = Utility::val<real>(stra);
187 y = Utility::val<real>(strb);
189 DMS::DecodeLatLon(stra, strb, lat, lon, longfirst);
194 cs.
Reverse(x, y, lat, lon, azi, rk);
196 az.
Reverse(lat0, lon0, x, y, lat, lon, azi, rk);
198 gn.
Reverse(lat0, lon0, x, y, lat, lon, azi, rk);
199 *output << Utility::str(longfirst ? lon : lat, prec + 5) <<
" "
200 << Utility::str(longfirst ? lat : lon, prec + 5) <<
" "
201 << Utility::str(azi, prec + 5) <<
" "
202 << Utility::str(rk, prec + 6) << eol;
205 cs.
Forward(lat, lon, x, y, azi, rk);
207 az.
Forward(lat0, lon0, lat, lon, x, y, azi, rk);
209 gn.
Forward(lat0, lon0, lat, lon, x, y, azi, rk);
210 *output << Utility::str(x, prec) <<
" "
211 << Utility::str(y, prec) <<
" "
212 << Utility::str(azi, prec + 5) <<
" "
213 << Utility::str(rk, prec + 6) << eol;
216 catch (
const std::exception& e) {
217 *output <<
"ERROR: " << e.what() <<
"\n";
223 catch (
const std::exception& e) {
224 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
228 std::cerr <<
"Caught unknown exception\n";
Header for GeographicLib::AzimuthalEquidistant class.
Header for GeographicLib::CassiniSoldner class.
Header for GeographicLib::DMS class.
GeographicLib::Math::real real
int main(int argc, const char *const argv[])
Header for GeographicLib::Geodesic class.
Header for GeographicLib::Gnomonic class.
Header for GeographicLib::Utility class.
Azimuthal equidistant projection.
void Forward(real lat0, real lon0, real lat, real lon, real &x, real &y, real &azi, real &rk) const
void Reverse(real lat0, real lon0, real x, real y, real &lat, real &lon, real &azi, real &rk) const
Cassini-Soldner projection.
void Reverse(real x, real y, real &lat, real &lon, real &azi, real &rk) const
void Forward(real lat, real lon, real &x, real &y, real &azi, real &rk) const
Exception handling for GeographicLib.
void Forward(real lat0, real lon0, real lat, real lon, real &x, real &y, real &azi, real &rk) const
void Reverse(real lat0, real lon0, real x, real y, real &lat, real &lon, real &azi, real &rk) const
Namespace for GeographicLib.