25 # pragma warning (disable: 4127 4701) 28 #include "CartConvert.usage" 30 int main(
int argc,
char* argv[]) {
35 bool localcartesian =
false, reverse =
false;
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);
53 h0 = Utility::num<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::num<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 ==
"--input-string") {
72 if (++m == argc)
return usage(1,
true);
74 }
else if (arg ==
"--input-file") {
75 if (++m == argc)
return usage(1,
true);
77 }
else if (arg ==
"--output-file") {
78 if (++m == argc)
return usage(1,
true);
80 }
else if (arg ==
"--line-separator") {
81 if (++m == argc)
return usage(1,
true);
82 if (std::string(argv[m]).size() != 1) {
83 std::cerr <<
"Line separator must be a single character\n";
87 }
else if (arg ==
"--comment-delimiter") {
88 if (++m == argc)
return usage(1,
true);
90 }
else if (arg ==
"--version") {
91 std::cout << argv[0] <<
": GeographicLib version " 92 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
95 return usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
98 if (!ifile.empty() && !istring.empty()) {
99 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
102 if (ifile ==
"-") ifile.clear();
103 std::ifstream infile;
104 std::istringstream instring;
105 if (!ifile.empty()) {
106 infile.open(ifile.c_str());
107 if (!infile.is_open()) {
108 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
111 }
else if (!istring.empty()) {
112 std::string::size_type m = 0;
114 m = istring.find(lsep, m);
115 if (m == std::string::npos)
119 instring.str(istring);
121 std::istream* input = !ifile.empty() ? &infile :
122 (!istring.empty() ? &instring : &std::cin);
124 std::ofstream outfile;
125 if (ofile ==
"-") ofile.clear();
126 if (!ofile.empty()) {
127 outfile.open(ofile.c_str());
128 if (!outfile.is_open()) {
129 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
133 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
140 while (std::getline(*input, s)) {
142 std::string eol(
"\n");
143 if (!cdelim.empty()) {
144 std::string::size_type m = s.find(cdelim);
145 if (m != std::string::npos) {
146 eol =
" " + s.substr(m) +
"\n";
150 std::istringstream str(s);
152 real lat, lon, h, x = 0, y = 0, z = 0;
153 std::string stra, strb, strc;
154 if (!(str >> stra >> strb >> strc))
157 x = Utility::num<real>(stra);
158 y = Utility::num<real>(strb);
159 z = Utility::num<real>(strc);
162 h = Utility::num<real>(strc);
169 lc.
Reverse(x, y, z, lat, lon, h);
171 ec.
Reverse(x, y, z, lat, lon, h);
177 lc.
Forward(lat, lon, h, x, y, z);
179 ec.
Forward(lat, lon, h, x, y, z);
185 catch (
const std::exception& e) {
186 *output <<
"ERROR: " << e.what() <<
"\n";
192 catch (
const std::exception& e) {
193 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
197 std::cerr <<
"Caught unknown exception\n";
Header for GeographicLib::LocalCartesian class.
GeographicLib::Math::real real
Header for GeographicLib::Utility class.
void Forward(real lat, real lon, real h, real &X, real &Y, real &Z) const
static int extra_digits()
void Reverse(real x, real y, real z, real &lat, real &lon, real &h) const
static void DecodeLatLon(const std::string &dmsa, const std::string &dmsb, real &lat, real &lon, bool swaplatlong=false)
Namespace for GeographicLib.
static std::string str(T x, int p=-1)
Header for GeographicLib::Geocentric class.
int main(int argc, char *argv[])
static int set_digits(int ndigits=0)
Local cartesian coordinates.
void Reverse(real X, real Y, real Z, real &lat, real &lon, real &h) const
Exception handling for GeographicLib.
void Forward(real lat, real lon, real h, real &x, real &y, real &z) const
Header for GeographicLib::DMS class.