00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <iostream>
00021 #include <sstream>
00022 #include <string>
00023 #include <sstream>
00024 #include <fstream>
00025 #include <GeographicLib/Geocentric.hpp>
00026 #include <GeographicLib/LocalCartesian.hpp>
00027 #include <GeographicLib/DMS.hpp>
00028 #include <GeographicLib/Utility.hpp>
00029
00030 #if defined(_MSC_VER)
00031
00032
00033 # pragma warning (disable: 4127 4701)
00034 #endif
00035
00036 #include "CartConvert.usage"
00037
00038 int main(int argc, char* argv[]) {
00039 try {
00040 using namespace GeographicLib;
00041 typedef Math::real real;
00042 Utility::set_digits();
00043 bool localcartesian = false, reverse = false;
00044 real
00045 a = Constants::WGS84_a(),
00046 f = Constants::WGS84_f();
00047 real lat0 = 0, lon0 = 0, h0 = 0;
00048 std::string istring, ifile, ofile, cdelim;
00049 char lsep = ';';
00050
00051 for (int m = 1; m < argc; ++m) {
00052 std::string arg(argv[m]);
00053 if (arg == "-r")
00054 reverse = true;
00055 else if (arg == "-l") {
00056 localcartesian = true;
00057 if (m + 3 >= argc) return usage(1, true);
00058 try {
00059 DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),
00060 lat0, lon0);
00061 h0 = Utility::num<real>(std::string(argv[m + 3]));
00062 }
00063 catch (const std::exception& e) {
00064 std::cerr << "Error decoding arguments of -l: " << e.what() << "\n";
00065 return 1;
00066 }
00067 m += 3;
00068 } else if (arg == "-e") {
00069 if (m + 2 >= argc) return usage(1, true);
00070 try {
00071 a = Utility::num<real>(std::string(argv[m + 1]));
00072 f = Utility::fract<real>(std::string(argv[m + 2]));
00073 }
00074 catch (const std::exception& e) {
00075 std::cerr << "Error decoding arguments of -e: " << e.what() << "\n";
00076 return 1;
00077 }
00078 m += 2;
00079 } else if (arg == "--input-string") {
00080 if (++m == argc) return usage(1, true);
00081 istring = argv[m];
00082 } else if (arg == "--input-file") {
00083 if (++m == argc) return usage(1, true);
00084 ifile = argv[m];
00085 } else if (arg == "--output-file") {
00086 if (++m == argc) return usage(1, true);
00087 ofile = argv[m];
00088 } else if (arg == "--line-separator") {
00089 if (++m == argc) return usage(1, true);
00090 if (std::string(argv[m]).size() != 1) {
00091 std::cerr << "Line separator must be a single character\n";
00092 return 1;
00093 }
00094 lsep = argv[m][0];
00095 } else if (arg == "--comment-delimiter") {
00096 if (++m == argc) return usage(1, true);
00097 cdelim = argv[m];
00098 } else if (arg == "--version") {
00099 std::cout
00100 << argv[0] << ": GeographicLib version "
00101 << GEOGRAPHICLIB_VERSION_STRING << "\n";
00102 return 0;
00103 } else
00104 return usage(!(arg == "-h" || arg == "--help"), arg != "--help");
00105 }
00106
00107 if (!ifile.empty() && !istring.empty()) {
00108 std::cerr << "Cannot specify --input-string and --input-file together\n";
00109 return 1;
00110 }
00111 if (ifile == "-") ifile.clear();
00112 std::ifstream infile;
00113 std::istringstream instring;
00114 if (!ifile.empty()) {
00115 infile.open(ifile.c_str());
00116 if (!infile.is_open()) {
00117 std::cerr << "Cannot open " << ifile << " for reading\n";
00118 return 1;
00119 }
00120 } else if (!istring.empty()) {
00121 std::string::size_type m = 0;
00122 while (true) {
00123 m = istring.find(lsep, m);
00124 if (m == std::string::npos)
00125 break;
00126 istring[m] = '\n';
00127 }
00128 instring.str(istring);
00129 }
00130 std::istream* input = !ifile.empty() ? &infile :
00131 (!istring.empty() ? &instring : &std::cin);
00132
00133 std::ofstream outfile;
00134 if (ofile == "-") ofile.clear();
00135 if (!ofile.empty()) {
00136 outfile.open(ofile.c_str());
00137 if (!outfile.is_open()) {
00138 std::cerr << "Cannot open " << ofile << " for writing\n";
00139 return 1;
00140 }
00141 }
00142 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
00143
00144 const Geocentric ec(a, f);
00145 const LocalCartesian lc(lat0, lon0, h0, ec);
00146
00147 std::string s;
00148 int retval = 0;
00149 while (std::getline(*input, s)) {
00150 try {
00151 std::string eol("\n");
00152 if (!cdelim.empty()) {
00153 std::string::size_type m = s.find(cdelim);
00154 if (m != std::string::npos) {
00155 eol = " " + s.substr(m) + "\n";
00156 s = s.substr(0, m);
00157 }
00158 }
00159 std::istringstream str(s);
00160
00161 real lat, lon, h, x = 0, y = 0, z = 0;
00162 std::string stra, strb, strc;
00163 if (!(str >> stra >> strb >> strc))
00164 throw GeographicErr("Incomplete input: " + s);
00165 if (reverse) {
00166 x = Utility::num<real>(stra);
00167 y = Utility::num<real>(strb);
00168 z = Utility::num<real>(strc);
00169 } else {
00170 DMS::DecodeLatLon(stra, strb, lat, lon);
00171 h = Utility::num<real>(strc);
00172 }
00173 std::string strd;
00174 if (str >> strd)
00175 throw GeographicErr("Extraneous input: " + strd);
00176 if (reverse) {
00177 if (localcartesian)
00178 lc.Reverse(x, y, z, lat, lon, h);
00179 else
00180 ec.Reverse(x, y, z, lat, lon, h);
00181 *output << Utility::str(lat, 15 + Math::extra_digits()) << " "
00182 << Utility::str(lon, 15 + Math::extra_digits()) << " "
00183 << Utility::str(h, 12 + Math::extra_digits()) << eol;
00184 } else {
00185 if (localcartesian)
00186 lc.Forward(lat, lon, h, x, y, z);
00187 else
00188 ec.Forward(lat, lon, h, x, y, z);
00189 *output << Utility::str(x, 10 + Math::extra_digits()) << " "
00190 << Utility::str(y, 10 + Math::extra_digits()) << " "
00191 << Utility::str(z, 10 + Math::extra_digits()) << eol;
00192 }
00193 }
00194 catch (const std::exception& e) {
00195 *output << "ERROR: " << e.what() << "\n";
00196 retval = 1;
00197 }
00198 }
00199 return retval;
00200 }
00201 catch (const std::exception& e) {
00202 std::cerr << "Caught exception: " << e.what() << "\n";
00203 return 1;
00204 }
00205 catch (...) {
00206 std::cerr << "Caught unknown exception\n";
00207 return 1;
00208 }
00209 }