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") {
92 << argv[0] <<
": GeographicLib version "
93 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
96 return usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
99 if (!ifile.empty() && !istring.empty()) {
100 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
103 if (ifile ==
"-") ifile.clear();
104 std::ifstream infile;
105 std::istringstream instring;
106 if (!ifile.empty()) {
107 infile.open(ifile.c_str());
108 if (!infile.is_open()) {
109 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
112 }
else if (!istring.empty()) {
113 std::string::size_type m = 0;
115 m = istring.find(lsep, m);
116 if (m == std::string::npos)
120 instring.str(istring);
122 std::istream* input = !ifile.empty() ? &infile :
123 (!istring.empty() ? &instring : &std::cin);
125 std::ofstream outfile;
126 if (ofile ==
"-") ofile.clear();
127 if (!ofile.empty()) {
128 outfile.open(ofile.c_str());
129 if (!outfile.is_open()) {
130 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
134 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
141 while (std::getline(*input, s)) {
143 std::string eol(
"\n");
144 if (!cdelim.empty()) {
145 std::string::size_type m = s.find(cdelim);
146 if (m != std::string::npos) {
147 eol =
" " + s.substr(m) +
"\n";
151 std::istringstream str(s);
153 real lat, lon, h, x = 0, y = 0, z = 0;
154 std::string stra, strb, strc;
155 if (!(str >> stra >> strb >> strc))
158 x = Utility::num<real>(stra);
159 y = Utility::num<real>(strb);
160 z = Utility::num<real>(strc);
163 h = Utility::num<real>(strc);
170 lc.
Reverse(x, y, z, lat, lon, h);
172 ec.
Reverse(x, y, z, lat, lon, h);
178 lc.
Forward(lat, lon, h, x, y, z);
180 ec.
Forward(lat, lon, h, x, y, z);
186 catch (
const std::exception& e) {
187 *output <<
"ERROR: " << e.what() <<
"\n";
193 catch (
const std::exception& e) {
194 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
198 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.