24# pragma warning (disable: 4127 4701)
27#include "CartConvert.usage"
29int main(
int argc,
const char*
const argv[]) {
33 Utility::set_digits();
34 bool localcartesian =
false, reverse =
false, longfirst =
false;
36 a = Constants::WGS84_a(),
37 f = Constants::WGS84_f();
39 real lat0 = 0, lon0 = 0, h0 = 0;
40 std::string istring, ifile, ofile, cdelim;
43 for (
int m = 1; m < argc; ++m) {
44 std::string arg(argv[m]);
47 else if (arg ==
"-l") {
48 localcartesian =
true;
49 if (m + 3 >= argc)
return usage(1,
true);
51 DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),
52 lat0, lon0, longfirst);
53 h0 = Utility::val<real>(std::string(argv[m + 3]));
55 catch (
const std::exception& e) {
56 std::cerr <<
"Error decoding arguments of -l: " << e.what() <<
"\n";
60 }
else if (arg ==
"-e") {
61 if (m + 2 >= argc)
return usage(1,
true);
63 a = Utility::val<real>(std::string(argv[m + 1]));
64 f = Utility::fract<real>(std::string(argv[m + 2]));
66 catch (
const std::exception& e) {
67 std::cerr <<
"Error decoding arguments of -e: " << e.what() <<
"\n";
71 }
else if (arg ==
"-w")
72 longfirst = !longfirst;
73 else if (arg ==
"-p") {
74 if (++m == argc)
return usage(1,
true);
76 prec = Utility::val<int>(std::string(argv[m]));
78 catch (
const std::exception&) {
79 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
82 }
else if (arg ==
"--input-string") {
83 if (++m == argc)
return usage(1,
true);
85 }
else if (arg ==
"--input-file") {
86 if (++m == argc)
return usage(1,
true);
88 }
else if (arg ==
"--output-file") {
89 if (++m == argc)
return usage(1,
true);
91 }
else if (arg ==
"--line-separator") {
92 if (++m == argc)
return usage(1,
true);
93 if (std::string(argv[m]).size() != 1) {
94 std::cerr <<
"Line separator must be a single character\n";
98 }
else if (arg ==
"--comment-delimiter") {
99 if (++m == argc)
return usage(1,
true);
101 }
else if (arg ==
"--version") {
102 std::cout << argv[0] <<
": GeographicLib version "
103 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
106 return usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
109 if (!ifile.empty() && !istring.empty()) {
110 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
113 if (ifile ==
"-") ifile.clear();
114 std::ifstream infile;
115 std::istringstream instring;
116 if (!ifile.empty()) {
117 infile.open(ifile.c_str());
118 if (!infile.is_open()) {
119 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
122 }
else if (!istring.empty()) {
123 std::string::size_type m = 0;
125 m = istring.find(lsep, m);
126 if (m == std::string::npos)
130 instring.str(istring);
132 std::istream* input = !ifile.empty() ? &infile :
133 (!istring.empty() ? &instring : &std::cin);
135 std::ofstream outfile;
136 if (ofile ==
"-") ofile.clear();
137 if (!ofile.empty()) {
138 outfile.open(ofile.c_str());
139 if (!outfile.is_open()) {
140 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
144 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
151 prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
152 std::string s, eol, stra, strb, strc, strd;
153 std::istringstream str;
155 while (std::getline(*input, s)) {
158 if (!cdelim.empty()) {
159 std::string::size_type m = s.find(cdelim);
160 if (m != std::string::npos) {
161 eol =
" " + s.substr(m) +
"\n";
165 str.clear(); str.str(s);
167 real lat, lon, h, x = 0, y = 0, z = 0;
168 if (!(str >> stra >> strb >> strc))
171 x = Utility::val<real>(stra);
172 y = Utility::val<real>(strb);
173 z = Utility::val<real>(strc);
175 DMS::DecodeLatLon(stra, strb, lat, lon, longfirst);
176 h = Utility::val<real>(strc);
182 lc.
Reverse(x, y, z, lat, lon, h);
184 ec.
Reverse(x, y, z, lat, lon, h);
185 *output << Utility::str(longfirst ? lon : lat, prec + 5) <<
" "
186 << Utility::str(longfirst ? lat : lon, prec + 5) <<
" "
187 << Utility::str(h, prec) << eol;
190 lc.
Forward(lat, lon, h, x, y, z);
192 ec.
Forward(lat, lon, h, x, y, z);
193 *output << Utility::str(x, prec) <<
" "
194 << Utility::str(y, prec) <<
" "
195 << Utility::str(z, prec) << eol;
198 catch (
const std::exception& e) {
199 *output <<
"ERROR: " << e.what() <<
"\n";
205 catch (
const std::exception& e) {
206 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
210 std::cerr <<
"Caught unknown exception\n";
int main(int argc, const char *const argv[])
Header for GeographicLib::DMS class.
Header for GeographicLib::Geocentric class.
GeographicLib::Math::real real
Header for GeographicLib::LocalCartesian class.
Header for GeographicLib::Utility class.
void Reverse(real X, real Y, real Z, real &lat, real &lon, real &h) const
void Forward(real lat, real lon, real h, real &X, real &Y, real &Z) const
Exception handling for GeographicLib.
Local cartesian coordinates.
void Reverse(real x, real y, real z, real &lat, real &lon, real &h) const
void Forward(real lat, real lon, real h, real &x, real &y, real &z) const
Namespace for GeographicLib.