33 # pragma warning (disable: 4127 4701)
36 #include "CartConvert.usage"
38 int main(
int argc,
char* argv[]) {
40 using namespace GeographicLib;
42 bool localcartesian =
false, reverse =
false;
44 a = Constants::WGS84_a<real>(),
45 f = Constants::WGS84_f<real>();
46 real lat0 = 0, lon0 = 0, h0 = 0;
47 std::string istring, ifile, ofile, cdelim;
50 for (
int m = 1; m < argc; ++m) {
51 std::string arg(argv[m]);
54 else if (arg ==
"-l") {
55 localcartesian =
true;
56 if (m + 3 >= argc)
return usage(1,
true);
60 h0 = Utility::num<real>(std::string(argv[m + 3]));
62 catch (
const std::exception& e) {
63 std::cerr <<
"Error decoding arguments of -l: " << e.what() <<
"\n";
67 }
else if (arg ==
"-e") {
68 if (m + 2 >= argc)
return usage(1,
true);
70 a = Utility::num<real>(std::string(argv[m + 1]));
71 f = Utility::fract<real>(std::string(argv[m + 2]));
73 catch (
const std::exception& e) {
74 std::cerr <<
"Error decoding arguments of -e: " << e.what() <<
"\n";
78 }
else if (arg ==
"--input-string") {
79 if (++m == argc)
return usage(1,
true);
81 }
else if (arg ==
"--input-file") {
82 if (++m == argc)
return usage(1,
true);
84 }
else if (arg ==
"--output-file") {
85 if (++m == argc)
return usage(1,
true);
87 }
else if (arg ==
"--line-separator") {
88 if (++m == argc)
return usage(1,
true);
89 if (std::string(argv[m]).size() != 1) {
90 std::cerr <<
"Line separator must be a single character\n";
94 }
else if (arg ==
"--comment-delimiter") {
95 if (++m == argc)
return usage(1,
true);
97 }
else if (arg ==
"--version") {
99 << argv[0] <<
": GeographicLib version "
100 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
103 return usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
106 if (!ifile.empty() && !istring.empty()) {
107 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
110 if (ifile ==
"-") ifile.clear();
111 std::ifstream infile;
112 std::istringstream instring;
113 if (!ifile.empty()) {
114 infile.open(ifile.c_str());
115 if (!infile.is_open()) {
116 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
119 }
else if (!istring.empty()) {
120 std::string::size_type m = 0;
122 m = istring.find(lsep, m);
123 if (m == std::string::npos)
127 instring.str(istring);
129 std::istream* input = !ifile.empty() ? &infile :
130 (!istring.empty() ? &instring : &std::cin);
132 std::ofstream outfile;
133 if (ofile ==
"-") ofile.clear();
134 if (!ofile.empty()) {
135 outfile.open(ofile.c_str());
136 if (!outfile.is_open()) {
137 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
141 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
148 while (std::getline(*input, s)) {
150 std::string eol(
"\n");
151 if (!cdelim.empty()) {
152 std::string::size_type m = s.find(cdelim);
153 if (m != std::string::npos) {
154 eol =
" " + s.substr(m) +
"\n";
158 std::istringstream str(s);
159 real lat, lon, h, x, y, z;
160 std::string stra, strb, strc;
161 if (!(str >> stra >> strb >> strc))
164 x = Utility::num<real>(stra);
165 y = Utility::num<real>(strb);
166 z = Utility::num<real>(strc);
169 h = Utility::num<real>(strc);
176 lc.
Reverse(x, y, z, lat, lon, h);
178 ec.
Reverse(x, y, z, lat, lon, h);
179 *output << Utility::str<real>(lat, 15) <<
" "
180 << Utility::str<real>(lon, 15) <<
" "
181 << Utility::str<real>(h, 12) << eol;
184 lc.
Forward(lat, lon, h, x, y, z);
186 ec.
Forward(lat, lon, h, x, y, z);
187 *output << Utility::str<real>(x, 10) <<
" "
188 << Utility::str<real>(y, 10) <<
" "
189 << Utility::str<real>(z, 10) << eol;
192 catch (
const std::exception& e) {
193 *output <<
"ERROR: " << e.what() <<
"\n";
199 catch (
const std::exception& e) {
200 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
204 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
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)
Header for GeographicLib::Geocentric class.
int main(int argc, char *argv[])
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.