CartConvert.cpp
Go to the documentation of this file.
00001 /**
00002  * \file CartConvert.cpp
00003  * \brief Command line utility for geodetic to cartesian coordinate conversions
00004  *
00005  * Copyright (c) Charles Karney (2008) <charles@karney.com>
00006  * http://charles.karney.info/geographic
00007  * and licensed under the LGPL.
00008  *
00009  * Compile with
00010  *
00011  *   g++ -g -O3 -I.. -o CartConvert CartConvert.cpp Geocentric.cpp LocalCartesian.cpp Constants.cpp
00012  *
00013  * See \ref cartconvert for usage information.
00014  **********************************************************************/
00015 
00016 #include <string>
00017 #include <iostream>
00018 #include <iomanip>
00019 #include <sstream>
00020 #include "GeographicLib/Geocentric.hpp"
00021 #include "GeographicLib/LocalCartesian.hpp"
00022 
00023 int usage(int retval) {
00024   ( retval ? std::cerr : std::cout ) <<
00025 "Usage: CartConvert [-r] [-l lat0 lon0 h0] [-h]\n\
00026 $Id: CartConvert.cpp 6553 2009-02-24 03:10:01Z ckarney $\n\
00027 \n\
00028 Convert geodetic coordinates to either geocentric or local cartesian\n\
00029 coordinates.  Geocentric coordinates have the origin at the center of the\n\
00030 earth, with the z axis going thru the north pole, and the x axis thru lat =\n\
00031 0, lon = 0.  By default, the conversion is to geocentric coordinates.\n\
00032 Specifying -l lat0 lon0 h0 causes a local coordinate system to be used with\n\
00033 the origin at latitude = lat0, longitude = lon0, height = h0, z normal to\n\
00034 the ellipsoid and y due north.  The WGS84 model of the earth is used.\n\
00035 \n\
00036 Geodetic coordinates are provided on standard input as a set of lines\n\
00037 containing (blank separated) latitude, longitude (decimal degrees), and\n\
00038 height (meters).  For each set of geodetic coordinates, the corresponding\n\
00039 cartesian coordinates x, y, z (meters) are printed on standard output.\n\
00040 \n\
00041 If -r is given the reverse transformation is performed.\n\
00042 \n\
00043 -h prints this help\n";
00044   return retval;
00045 }
00046 
00047 int main(int argc, char* argv[]) {
00048   bool localcartesian = false, reverse = false;
00049   double latlonh0[3] = {0, 0, 0};
00050   for (int m = 1; m < argc; ++m) {
00051     std::string arg = std::string(argv[m]);
00052     if (arg == "-r")
00053       reverse = true;
00054     else if (arg == "-l") {
00055       localcartesian = true;
00056       for (unsigned i = 0; i < 3; ++i) {
00057         if (++m == argc) return usage(1);
00058         std::string a = std::string(argv[m]);
00059         std::istringstream str(a);
00060         if (!(str >> latlonh0[i])) return usage(1);
00061       }
00062     } else
00063       return usage(arg != "-h");
00064   }
00065   const GeographicLib::LocalCartesian lc(latlonh0[0], latlonh0[1], latlonh0[2]);
00066   const GeographicLib::Geocentric& ec = GeographicLib::Geocentric::WGS84;
00067 
00068   std::cout << std::setprecision(16);
00069   while (true) {
00070     double lat, lon, h, x, y, z;
00071     if (reverse)
00072       std::cin >> x >> y >> z;
00073     else
00074       std::cin >> lat >> lon >> h;
00075     if (!std::cin.good())
00076       break;
00077     if (reverse) {
00078       if (localcartesian)
00079         lc.Reverse(x, y, z, lat, lon, h);
00080       else
00081         ec.Reverse(x, y, z, lat, lon, h);
00082       std::cout << lat << " " << lon << " " << h << "\n";
00083     } else {
00084       if (localcartesian)
00085         lc.Forward(lat, lon, h, x, y, z);
00086       else
00087         ec.Forward(lat, lon, h, x, y, z);
00088       std::cout << x << " " << y << " " << z << "\n";
00089     }
00090   }
00091   return 0;
00092 }