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