TransverseMercatorTest.cpp

Go to the documentation of this file.
00001 /**
00002  * \file TransverseMercatorTest.cpp
00003  * \brief Command line utility for testing transverse Mercator projections
00004  *
00005  * Copyright (c) Charles Karney (2008, 2009, 2010, 2011) <charles@karney.com>
00006  * and licensed under the LGPL.  For more information, see
00007  * http://geographiclib.sourceforge.net/
00008  *
00009  * Compile with -I../include and link with TransverseMercatorExact.o
00010  * EllipticFunction.o TransverseMercator.o
00011  *
00012  * See the <a href="TransverseMercatorTest.1.html">man page</a> for usage
00013  * information.
00014  **********************************************************************/
00015 
00016 #include "GeographicLib/EllipticFunction.hpp"
00017 #include "GeographicLib/TransverseMercatorExact.hpp"
00018 #include "GeographicLib/TransverseMercator.hpp"
00019 #include "GeographicLib/DMS.hpp"
00020 #include <iostream>
00021 #include <sstream>
00022 
00023 #include "TransverseMercatorTest.usage"
00024 
00025 int main(int argc, char* argv[]) {
00026   using namespace GeographicLib;
00027   typedef Math::real real;
00028   bool reverse = false, testing = false, series = false;
00029   for (int m = 1; m < argc; ++m) {
00030     std::string arg(argv[m]);
00031     if (arg == "-r")
00032       reverse = true;
00033     else if (arg == "-t") {
00034       testing = true;
00035       series = false;
00036     } else if (arg == "-s") {
00037       testing = false;
00038       series = true;
00039     } else if (arg == "--version") {
00040       std::cout
00041         << PROGRAM_NAME
00042         << ": $Id: TransverseMercatorTest.cpp 6978 2011-02-21 22:42:11Z karney $\n"
00043         << "GeographicLib version " << GEOGRAPHICLIB_VERSION << "\n";
00044       return 0;
00045     } else
00046       return usage(!(arg == "-h" || arg == "--help"), arg != "--help");
00047   }
00048 
00049   real e, a;
00050   if (testing) {
00051     e = 1/real(10);
00052     EllipticFunction temp(e * e);
00053     a = 1/temp.E();
00054   }
00055   const TransverseMercatorExact& TME = testing ?
00056     TransverseMercatorExact(a, (std::sqrt(1 - e * e) + 1) / (e * e),
00057                             real(1), true) :
00058     TransverseMercatorExact::UTM;
00059 
00060   const TransverseMercator& TMS = TransverseMercator::UTM;
00061 
00062   std::string s;
00063   int retval = 0;
00064   std::cout << std::fixed;
00065   while (std::getline(std::cin, s)) {
00066     try {
00067       std::istringstream str(s);
00068       real lat, lon, x, y;
00069       std::string stra, strb;
00070       if (!(str >> stra >> strb))
00071           throw GeographicErr("Incomplete input: " + s);
00072       if (reverse) {
00073         x = DMS::Decode(stra);
00074         y = DMS::Decode(strb);
00075       } else
00076         DMS::DecodeLatLon(stra, strb, lat, lon);
00077       std::string strc;
00078       if (str >> strc)
00079         throw GeographicErr("Extraneous input: " + strc);
00080       real gamma, k;
00081       if (reverse) {
00082         if (series)
00083           TMS.Reverse(real(0), x, y, lat, lon, gamma, k);
00084         else
00085           TME.Reverse(real(0), x, y, lat, lon, gamma, k);
00086       } else {
00087         if (series)
00088           TMS.Forward(real(0), lat, lon, x, y, gamma, k);
00089         else
00090           TME.Forward(real(0), lat, lon, x, y, gamma, k);
00091       }
00092       std::cout << DMS::Encode(lat, 15, DMS::NUMBER) << " "
00093                 << DMS::Encode(lon, 15, DMS::NUMBER) << " "
00094                 << DMS::Encode(x, 10, DMS::NUMBER) << " "
00095                 << DMS::Encode(y, 10, DMS::NUMBER) << " "
00096                 << DMS::Encode(gamma, 16, DMS::NUMBER) << " "
00097                 << DMS::Encode(k, 16, DMS::NUMBER) << "\n";
00098     }
00099     catch (const std::exception& e) {
00100       std::cout << "ERROR: " << e.what() << "\n";
00101       retval = 1;
00102     }
00103   }
00104 
00105   return retval;
00106 }