25 # pragma warning (disable: 4127 4701)
28 #include "CartConvert.usage"
30 int main(
int argc,
char* argv[]) {
35 bool localcartesian =
false, reverse =
false, longfirst =
false;
40 real lat0 = 0, lon0 = 0, h0 = 0;
41 std::string istring, ifile, ofile, cdelim;
44 for (
int m = 1; m < argc; ++m) {
45 std::string arg(argv[m]);
48 else if (arg ==
"-l") {
49 localcartesian =
true;
50 if (m + 3 >= argc)
return usage(1,
true);
53 lat0, lon0, longfirst);
54 h0 = Utility::num<real>(std::string(argv[m + 3]));
56 catch (
const std::exception& e) {
57 std::cerr <<
"Error decoding arguments of -l: " << e.what() <<
"\n";
61 }
else if (arg ==
"-e") {
62 if (m + 2 >= argc)
return usage(1,
true);
64 a = Utility::num<real>(std::string(argv[m + 1]));
65 f = Utility::fract<real>(std::string(argv[m + 2]));
67 catch (
const std::exception& e) {
68 std::cerr <<
"Error decoding arguments of -e: " << e.what() <<
"\n";
72 }
else if (arg ==
"-w")
74 else if (arg ==
"-p") {
75 if (++m == argc)
return usage(1,
true);
77 prec = Utility::num<int>(std::string(argv[m]));
79 catch (
const std::exception&) {
80 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
83 }
else if (arg ==
"--input-string") {
84 if (++m == argc)
return usage(1,
true);
86 }
else if (arg ==
"--input-file") {
87 if (++m == argc)
return usage(1,
true);
89 }
else if (arg ==
"--output-file") {
90 if (++m == argc)
return usage(1,
true);
92 }
else if (arg ==
"--line-separator") {
93 if (++m == argc)
return usage(1,
true);
94 if (std::string(argv[m]).size() != 1) {
95 std::cerr <<
"Line separator must be a single character\n";
99 }
else if (arg ==
"--comment-delimiter") {
100 if (++m == argc)
return usage(1,
true);
102 }
else if (arg ==
"--version") {
103 std::cout << argv[0] <<
": GeographicLib version "
104 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
107 return usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
110 if (!ifile.empty() && !istring.empty()) {
111 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
114 if (ifile ==
"-") ifile.clear();
115 std::ifstream infile;
116 std::istringstream instring;
117 if (!ifile.empty()) {
118 infile.open(ifile.c_str());
119 if (!infile.is_open()) {
120 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
123 }
else if (!istring.empty()) {
124 std::string::size_type m = 0;
126 m = istring.find(lsep, m);
127 if (m == std::string::npos)
131 instring.str(istring);
133 std::istream* input = !ifile.empty() ? &infile :
134 (!istring.empty() ? &instring : &std::cin);
136 std::ofstream outfile;
137 if (ofile ==
"-") ofile.clear();
138 if (!ofile.empty()) {
139 outfile.open(ofile.c_str());
140 if (!outfile.is_open()) {
141 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
145 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
153 std::string s, eol, stra, strb, strc, strd;
154 std::istringstream str;
156 while (std::getline(*input, s)) {
159 if (!cdelim.empty()) {
160 std::string::size_type m = s.find(cdelim);
161 if (m != std::string::npos) {
162 eol =
" " + s.substr(m) +
"\n";
166 str.clear(); str.str(s);
168 real lat, lon, h, x = 0, y = 0, z = 0;
169 if (!(str >> stra >> strb >> strc))
172 x = Utility::num<real>(stra);
173 y = Utility::num<real>(strb);
174 z = Utility::num<real>(strc);
177 h = Utility::num<real>(strc);
183 lc.
Reverse(x, y, z, lat, lon, h);
185 ec.
Reverse(x, y, z, lat, lon, h);
186 *output <<
Utility::str(longfirst ? lon : lat, prec + 5) <<
" "
191 lc.
Forward(lat, lon, h, x, y, z);
193 ec.
Forward(lat, lon, h, x, y, z);
199 catch (
const std::exception& e) {
200 *output <<
"ERROR: " << e.what() <<
"\n";
206 catch (
const std::exception& e) {
207 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
211 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
Namespace for GeographicLib.
static std::string str(T x, int p=-1)
Header for GeographicLib::Geocentric class.
static void DecodeLatLon(const std::string &dmsa, const std::string &dmsb, real &lat, real &lon, bool longfirst=false)
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.