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/Geodesic.hpp>
00026 #include <GeographicLib/GeodesicLine.hpp>
00027 #include <GeographicLib/GeodesicExact.hpp>
00028 #include <GeographicLib/GeodesicLineExact.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 "GeodSolve.usage"
00039
00040 typedef GeographicLib::Math::real real;
00041
00042 std::string LatLonString(real lat, real lon, int prec, bool dms, char dmssep) {
00043 using namespace GeographicLib;
00044 return dms ?
00045 DMS::Encode(lat, prec + 5, DMS::LATITUDE, dmssep) + " " +
00046 DMS::Encode(lon, prec + 5, DMS::LONGITUDE, dmssep) :
00047 DMS::Encode(lat, prec + 5, DMS::NUMBER) + " " +
00048 DMS::Encode(lon, prec + 5, DMS::NUMBER);
00049 }
00050
00051 std::string AzimuthString(real azi, int prec, bool dms, char dmssep) {
00052 using namespace GeographicLib;
00053 return dms ? DMS::Encode(azi, prec + 5, DMS::AZIMUTH, dmssep) :
00054 DMS::Encode(azi >= 180 ? azi - 360 : azi, prec + 5, DMS::NUMBER);
00055 }
00056
00057 std::string DistanceStrings(real s12, real a12,
00058 bool full, bool arcmode, int prec, bool dms) {
00059 using namespace GeographicLib;
00060 std::string s;
00061 if (full || !arcmode)
00062 s += Utility::str(s12, prec);
00063 if (full)
00064 s += " ";
00065 if (full || arcmode)
00066 s += DMS::Encode(a12, prec + 5, dms ? DMS::NONE : DMS::NUMBER);
00067 return s;
00068 }
00069
00070 real ReadDistance(const std::string& s, bool arcmode) {
00071 using namespace GeographicLib;
00072 return arcmode ? DMS::DecodeAngle(s) : Utility::num<real>(s);
00073 }
00074
00075 int main(int argc, char* argv[]) {
00076 try {
00077 using namespace GeographicLib;
00078 Utility::set_digits();
00079 bool linecalc = false, inverse = false, arcmode = false,
00080 dms = false, full = false, exact = false;
00081 real
00082 a = Constants::WGS84_a(),
00083 f = Constants::WGS84_f();
00084 real lat1, lon1, azi1, lat2, lon2, azi2, s12, m12, a12, M12, M21, S12;
00085 real azi2sense = 0;
00086 int prec = 3;
00087 std::string istring, ifile, ofile, cdelim;
00088 char lsep = ';', dmssep = char(0);
00089
00090 for (int m = 1; m < argc; ++m) {
00091 std::string arg(argv[m]);
00092 if (arg == "-i") {
00093 inverse = true;
00094 linecalc = false;
00095 } else if (arg == "-a")
00096 arcmode = true;
00097 else if (arg == "-l") {
00098 inverse = false;
00099 linecalc = true;
00100 if (m + 3 >= argc) return usage(1, true);
00101 try {
00102 DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),
00103 lat1, lon1);
00104 azi1 = DMS::DecodeAzimuth(std::string(argv[m + 3]));
00105 }
00106 catch (const std::exception& e) {
00107 std::cerr << "Error decoding arguments of -l: " << e.what() << "\n";
00108 return 1;
00109 }
00110 m += 3;
00111 } else if (arg == "-e") {
00112 if (m + 2 >= argc) return usage(1, true);
00113 try {
00114 a = Utility::num<real>(std::string(argv[m + 1]));
00115 f = Utility::fract<real>(std::string(argv[m + 2]));
00116 }
00117 catch (const std::exception& e) {
00118 std::cerr << "Error decoding arguments of -e: " << e.what() << "\n";
00119 return 1;
00120 }
00121 m += 2;
00122 }
00123 else if (arg == "-d") {
00124 dms = true;
00125 dmssep = '\0';
00126 } else if (arg == "-:") {
00127 dms = true;
00128 dmssep = ':';
00129 } else if (arg == "-b")
00130 azi2sense = 180;
00131 else if (arg == "-f")
00132 full = true;
00133 else if (arg == "-p") {
00134 if (++m == argc) return usage(1, true);
00135 try {
00136 prec = Utility::num<int>(std::string(argv[m]));
00137 }
00138 catch (const std::exception&) {
00139 std::cerr << "Precision " << argv[m] << " is not a number\n";
00140 return 1;
00141 }
00142 } else if (arg == "-E")
00143 exact = true;
00144 else if (arg == "--input-string") {
00145 if (++m == argc) return usage(1, true);
00146 istring = argv[m];
00147 } else if (arg == "--input-file") {
00148 if (++m == argc) return usage(1, true);
00149 ifile = argv[m];
00150 } else if (arg == "--output-file") {
00151 if (++m == argc) return usage(1, true);
00152 ofile = argv[m];
00153 } else if (arg == "--line-separator") {
00154 if (++m == argc) return usage(1, true);
00155 if (std::string(argv[m]).size() != 1) {
00156 std::cerr << "Line separator must be a single character\n";
00157 return 1;
00158 }
00159 lsep = argv[m][0];
00160 } else if (arg == "--comment-delimiter") {
00161 if (++m == argc) return usage(1, true);
00162 cdelim = argv[m];
00163 } else if (arg == "--version") {
00164 std::cout
00165 << argv[0] << ": GeographicLib version "
00166 << GEOGRAPHICLIB_VERSION_STRING << "\n";
00167 return 0;
00168 } else
00169 return usage(!(arg == "-h" || arg == "--help"), arg != "--help");
00170 }
00171
00172 if (!ifile.empty() && !istring.empty()) {
00173 std::cerr << "Cannot specify --input-string and --input-file together\n";
00174 return 1;
00175 }
00176 if (ifile == "-") ifile.clear();
00177 std::ifstream infile;
00178 std::istringstream instring;
00179 if (!ifile.empty()) {
00180 infile.open(ifile.c_str());
00181 if (!infile.is_open()) {
00182 std::cerr << "Cannot open " << ifile << " for reading\n";
00183 return 1;
00184 }
00185 } else if (!istring.empty()) {
00186 std::string::size_type m = 0;
00187 while (true) {
00188 m = istring.find(lsep, m);
00189 if (m == std::string::npos)
00190 break;
00191 istring[m] = '\n';
00192 }
00193 instring.str(istring);
00194 }
00195 std::istream* input = !ifile.empty() ? &infile :
00196 (!istring.empty() ? &instring : &std::cin);
00197
00198 std::ofstream outfile;
00199 if (ofile == "-") ofile.clear();
00200 if (!ofile.empty()) {
00201 outfile.open(ofile.c_str());
00202 if (!outfile.is_open()) {
00203 std::cerr << "Cannot open " << ofile << " for writing\n";
00204 return 1;
00205 }
00206 }
00207 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
00208
00209 const Geodesic geod(a, f);
00210 const GeodesicExact geode(a, f);
00211 GeodesicLine l;
00212 GeodesicLineExact le;
00213 if (linecalc) {
00214 if (exact)
00215 le = geode.Line(lat1, lon1, azi1);
00216 else
00217 l = geod.Line(lat1, lon1, azi1);
00218 }
00219
00220
00221
00222 prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
00223 std::string s;
00224 int retval = 0;
00225 while (std::getline(*input, s)) {
00226 try {
00227 std::string eol("\n");
00228 if (!cdelim.empty()) {
00229 std::string::size_type m = s.find(cdelim);
00230 if (m != std::string::npos) {
00231 eol = " " + s.substr(m) + "\n";
00232 s = s.substr(0, m);
00233 }
00234 }
00235 std::istringstream str(s);
00236 if (inverse) {
00237 std::string slat1, slon1, slat2, slon2;
00238 if (!(str >> slat1 >> slon1 >> slat2 >> slon2))
00239 throw GeographicErr("Incomplete input: " + s);
00240 std::string strc;
00241 if (str >> strc)
00242 throw GeographicErr("Extraneous input: " + strc);
00243 DMS::DecodeLatLon(slat1, slon1, lat1, lon1);
00244 DMS::DecodeLatLon(slat2, slon2, lat2, lon2);
00245 a12 = exact ?
00246 geode.Inverse(lat1, lon1, lat2, lon2, s12, azi1, azi2,
00247 m12, M12, M21, S12) :
00248 geod.Inverse(lat1, lon1, lat2, lon2, s12, azi1, azi2,
00249 m12, M12, M21, S12);
00250 if (full)
00251 *output << LatLonString(lat1, lon1, prec, dms, dmssep) << " ";
00252 *output << AzimuthString(azi1, prec, dms, dmssep) << " ";
00253 if (full)
00254 *output << LatLonString(lat2, lon2, prec, dms, dmssep) << " ";
00255 *output << AzimuthString(azi2 + azi2sense, prec, dms, dmssep) << " "
00256 << DistanceStrings(s12, a12, full, arcmode, prec, dms);
00257 if (full)
00258 *output << " " << Utility::str(m12, prec)
00259 << " " << Utility::str(M12, prec+7)
00260 << " " << Utility::str(M21, prec+7)
00261 << " " << Utility::str(S12, std::max(prec-7, 0));
00262 *output << eol;
00263 } else {
00264 if (linecalc) {
00265 std::string ss12;
00266 if (!(str >> ss12))
00267 throw GeographicErr("Incomplete input: " + s);
00268 std::string strc;
00269 if (str >> strc)
00270 throw GeographicErr("Extraneous input: " + strc);
00271 s12 = ReadDistance(ss12, arcmode);
00272 if (arcmode)
00273 exact ?
00274 le.ArcPosition(s12, lat2, lon2, azi2, a12, m12, M12, M21, S12) :
00275 l.ArcPosition(s12, lat2, lon2, azi2, a12, m12, M12, M21, S12);
00276 else
00277 a12 = exact ?
00278 le.Position(s12, lat2, lon2, azi2, m12, M12, M21, S12) :
00279 l.Position(s12, lat2, lon2, azi2, m12, M12, M21, S12);
00280 } else {
00281 std::string slat1, slon1, sazi1, ss12;
00282 if (!(str >> slat1 >> slon1 >> sazi1 >> ss12))
00283 throw GeographicErr("Incomplete input: " + s);
00284 std::string strc;
00285 if (str >> strc)
00286 throw GeographicErr("Extraneous input: " + strc);
00287 DMS::DecodeLatLon(slat1, slon1, lat1, lon1);
00288 azi1 = DMS::DecodeAzimuth(sazi1);
00289 s12 = ReadDistance(ss12, arcmode);
00290 if (arcmode)
00291 exact ?
00292 geode.ArcDirect(lat1, lon1, azi1, s12, lat2, lon2, azi2, a12,
00293 m12, M12, M21, S12) :
00294 geod.ArcDirect(lat1, lon1, azi1, s12, lat2, lon2, azi2, a12,
00295 m12, M12, M21, S12);
00296 else
00297 a12 = exact ?
00298 geode.Direct(lat1, lon1, azi1, s12, lat2, lon2, azi2,
00299 m12, M12, M21, S12) :
00300 geod.Direct(lat1, lon1, azi1, s12, lat2, lon2, azi2,
00301 m12, M12, M21, S12);
00302 }
00303 if (arcmode)
00304 std::swap(s12, a12);
00305 if (full)
00306 *output << LatLonString(lat1, lon1, prec, dms, dmssep) << " "
00307 << AzimuthString(azi1, prec, dms, dmssep) << " ";
00308 *output << LatLonString(lat2, lon2, prec, dms, dmssep) << " "
00309 << AzimuthString(azi2 + azi2sense, prec, dms, dmssep);
00310 if (full)
00311 *output << " "
00312 << DistanceStrings(s12, a12, full, arcmode, prec, dms)
00313 << " " << Utility::str(m12, prec)
00314 << " " << Utility::str(M12, prec+7)
00315 << " " << Utility::str(M21, prec+7)
00316 << " " << Utility::str(S12, std::max(prec-7, 0));
00317 *output << eol;
00318 }
00319 }
00320 catch (const std::exception& e) {
00321
00322 *output << "ERROR: " << e.what() << "\n";
00323 retval = 1;
00324 }
00325 }
00326 return retval;
00327 }
00328 catch (const std::exception& e) {
00329 std::cerr << "Caught exception: " << e.what() << "\n";
00330 return 1;
00331 }
00332 catch (...) {
00333 std::cerr << "Caught unknown exception\n";
00334 return 1;
00335 }
00336 }