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/LambertConformalConic.hpp>
00026 #include <GeographicLib/AlbersEqualArea.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 "ConicProj.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 lcc = false, albers = false, reverse = false;
00044 real lat1 = 0, lat2 = 0, lon0 = 0, k1 = 1;
00045 real
00046 a = Constants::WGS84_a(),
00047 f = Constants::WGS84_f();
00048 int prec = 6;
00049 std::string istring, ifile, ofile, cdelim;
00050 char lsep = ';';
00051
00052 for (int m = 1; m < argc; ++m) {
00053 std::string arg(argv[m]);
00054 if (arg == "-r")
00055 reverse = true;
00056 else if (arg == "-c" || arg == "-a") {
00057 lcc = arg == "-c";
00058 albers = arg == "-a";
00059 if (m + 2 >= argc) return usage(1, true);
00060 try {
00061 for (int i = 0; i < 2; ++i) {
00062 DMS::flag ind;
00063 (i ? lat2 : lat1) = DMS::Decode(std::string(argv[++m]), ind);
00064 if (ind == DMS::LONGITUDE)
00065 throw GeographicErr("Bad hemisphere");
00066 }
00067 }
00068 catch (const std::exception& e) {
00069 std::cerr << "Error decoding arguments of " << arg << ": "
00070 << e.what() << "\n";
00071 return 1;
00072 }
00073 } else if (arg == "-l") {
00074 if (++m == argc) return usage(1, true);
00075 try {
00076 DMS::flag ind;
00077 lon0 = DMS::Decode(std::string(argv[m]), ind);
00078 if (ind == DMS::LATITUDE)
00079 throw GeographicErr("Bad hemisphere");
00080 if (!(lon0 >= -540 && lon0 < 540))
00081 throw GeographicErr("Bad longitude");
00082 lon0 = Math::AngNormalize(lon0);
00083 }
00084 catch (const std::exception& e) {
00085 std::cerr << "Error decoding argument of " << arg << ": "
00086 << e.what() << "\n";
00087 return 1;
00088 }
00089 } else if (arg == "-k") {
00090 if (++m == argc) return usage(1, true);
00091 try {
00092 k1 = Utility::num<real>(std::string(argv[m]));
00093 }
00094 catch (const std::exception& e) {
00095 std::cerr << "Error decoding argument of " << arg << ": "
00096 << e.what() << "\n";
00097 return 1;
00098 }
00099 } else if (arg == "-e") {
00100 if (m + 2 >= argc) return usage(1, true);
00101 try {
00102 a = Utility::num<real>(std::string(argv[m + 1]));
00103 f = Utility::fract<real>(std::string(argv[m + 2]));
00104 }
00105 catch (const std::exception& e) {
00106 std::cerr << "Error decoding arguments of -e: " << e.what() << "\n";
00107 return 1;
00108 }
00109 m += 2;
00110 } else if (arg == "-p") {
00111 if (++m == argc) return usage(1, true);
00112 try {
00113 prec = Utility::num<int>(std::string(argv[m]));
00114 }
00115 catch (const std::exception&) {
00116 std::cerr << "Precision " << argv[m] << " is not a number\n";
00117 return 1;
00118 }
00119 } else if (arg == "--input-string") {
00120 if (++m == argc) return usage(1, true);
00121 istring = argv[m];
00122 } else if (arg == "--input-file") {
00123 if (++m == argc) return usage(1, true);
00124 ifile = argv[m];
00125 } else if (arg == "--output-file") {
00126 if (++m == argc) return usage(1, true);
00127 ofile = argv[m];
00128 } else if (arg == "--line-separator") {
00129 if (++m == argc) return usage(1, true);
00130 if (std::string(argv[m]).size() != 1) {
00131 std::cerr << "Line separator must be a single character\n";
00132 return 1;
00133 }
00134 lsep = argv[m][0];
00135 } else if (arg == "--comment-delimiter") {
00136 if (++m == argc) return usage(1, true);
00137 cdelim = argv[m];
00138 } else if (arg == "--version") {
00139 std::cout
00140 << argv[0] << ": GeographicLib version "
00141 << GEOGRAPHICLIB_VERSION_STRING << "\n";
00142 return 0;
00143 } else
00144 return usage(!(arg == "-h" || arg == "--help"), arg != "--help");
00145 }
00146
00147 if (!ifile.empty() && !istring.empty()) {
00148 std::cerr << "Cannot specify --input-string and --input-file together\n";
00149 return 1;
00150 }
00151 if (ifile == "-") ifile.clear();
00152 std::ifstream infile;
00153 std::istringstream instring;
00154 if (!ifile.empty()) {
00155 infile.open(ifile.c_str());
00156 if (!infile.is_open()) {
00157 std::cerr << "Cannot open " << ifile << " for reading\n";
00158 return 1;
00159 }
00160 } else if (!istring.empty()) {
00161 std::string::size_type m = 0;
00162 while (true) {
00163 m = istring.find(lsep, m);
00164 if (m == std::string::npos)
00165 break;
00166 istring[m] = '\n';
00167 }
00168 instring.str(istring);
00169 }
00170 std::istream* input = !ifile.empty() ? &infile :
00171 (!istring.empty() ? &instring : &std::cin);
00172
00173 std::ofstream outfile;
00174 if (ofile == "-") ofile.clear();
00175 if (!ofile.empty()) {
00176 outfile.open(ofile.c_str());
00177 if (!outfile.is_open()) {
00178 std::cerr << "Cannot open " << ofile << " for writing\n";
00179 return 1;
00180 }
00181 }
00182 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
00183
00184 if (!(lcc || albers)) {
00185 std::cerr << "Must specify \"-c lat1 lat2\" or "
00186 << "\"-a lat1 lat2\"\n";
00187 return 1;
00188 }
00189
00190 const LambertConformalConic lproj =
00191 lcc ? LambertConformalConic(a, f, lat1, lat2, k1)
00192 : LambertConformalConic(1, 0, 0, 0, 1);
00193 const AlbersEqualArea aproj =
00194 albers ? AlbersEqualArea(a, f, lat1, lat2, k1)
00195 : AlbersEqualArea(1, 0, 0, 0, 1);
00196
00197
00198
00199 prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
00200 std::string s;
00201 int retval = 0;
00202 std::cout << std::fixed;
00203 while (std::getline(*input, s)) {
00204 try {
00205 std::string eol("\n");
00206 if (!cdelim.empty()) {
00207 std::string::size_type m = s.find(cdelim);
00208 if (m != std::string::npos) {
00209 eol = " " + s.substr(m) + "\n";
00210 s = s.substr(0, m);
00211 }
00212 }
00213 std::istringstream str(s);
00214 real lat, lon, x, y, gamma, k;
00215 std::string stra, strb;
00216 if (!(str >> stra >> strb))
00217 throw GeographicErr("Incomplete input: " + s);
00218 if (reverse) {
00219 x = Utility::num<real>(stra);
00220 y = Utility::num<real>(strb);
00221 } else
00222 DMS::DecodeLatLon(stra, strb, lat, lon);
00223 std::string strc;
00224 if (str >> strc)
00225 throw GeographicErr("Extraneous input: " + strc);
00226 if (reverse) {
00227 if (lcc)
00228 lproj.Reverse(lon0, x, y, lat, lon, gamma, k);
00229 else
00230 aproj.Reverse(lon0, x, y, lat, lon, gamma, k);
00231 *output << Utility::str(lat, prec + 5) << " "
00232 << Utility::str(lon, prec + 5) << " "
00233 << Utility::str(gamma, prec + 6) << " "
00234 << Utility::str(k, prec + 6) << eol;
00235 } else {
00236 if (lcc)
00237 lproj.Forward(lon0, lat, lon, x, y, gamma, k);
00238 else
00239 aproj.Forward(lon0, lat, lon, x, y, gamma, k);
00240 *output << Utility::str(x, prec) << " "
00241 << Utility::str(y, prec) << " "
00242 << Utility::str(gamma, prec + 6) << " "
00243 << Utility::str(k, prec + 6) << eol;
00244 }
00245 }
00246 catch (const std::exception& e) {
00247 *output << "ERROR: " << e.what() << "\n";
00248 retval = 1;
00249 }
00250 }
00251 return retval;
00252 }
00253 catch (const std::exception& e) {
00254 std::cerr << "Caught exception: " << e.what() << "\n";
00255 return 1;
00256 }
00257 catch (...) {
00258 std::cerr << "Caught unknown exception\n";
00259 return 1;
00260 }
00261 }