00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <iostream>
00025 #include <string>
00026 #include <sstream>
00027 #include <fstream>
00028 #include <GeographicLib/Geoid.hpp>
00029 #include <GeographicLib/DMS.hpp>
00030 #include <GeographicLib/Utility.hpp>
00031 #include <GeographicLib/GeoCoords.hpp>
00032
00033 #if defined(_MSC_VER)
00034
00035
00036 # pragma warning (disable: 4127 4701)
00037 #endif
00038
00039 #include "GeoidEval.usage"
00040
00041 int main(int argc, char* argv[]) {
00042 try {
00043 using namespace GeographicLib;
00044 typedef Math::real real;
00045 Utility::set_digits();
00046 bool cacheall = false, cachearea = false, verbose = false,
00047 cubic = true, gradp = false;
00048 real caches, cachew, cachen, cachee;
00049 std::string dir;
00050 std::string geoid = Geoid::DefaultGeoidName();
00051 Geoid::convertflag heightmult = Geoid::NONE;
00052 std::string istring, ifile, ofile, cdelim;
00053 char lsep = ';';
00054 bool northp = false;
00055 int zonenum = UTMUPS::INVALID;
00056
00057 for (int m = 1; m < argc; ++m) {
00058 std::string arg(argv[m]);
00059 if (arg == "-a") {
00060 cacheall = true;
00061 cachearea = false;
00062 }
00063 else if (arg == "-c") {
00064 if (m + 4 >= argc) return usage(1, true);
00065 cacheall = false;
00066 cachearea = true;
00067 try {
00068 DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),
00069 caches, cachew);
00070 DMS::DecodeLatLon(std::string(argv[m + 3]), std::string(argv[m + 4]),
00071 cachen, cachee);
00072 }
00073 catch (const std::exception& e) {
00074 std::cerr << "Error decoding argument of -c: " << e.what() << "\n";
00075 return 1;
00076 }
00077 m += 4;
00078 } else if (arg == "--msltohae")
00079 heightmult = Geoid::GEOIDTOELLIPSOID;
00080 else if (arg == "--haetomsl")
00081 heightmult = Geoid::ELLIPSOIDTOGEOID;
00082 else if (arg == "-z") {
00083 if (++m == argc) return usage(1, true);
00084 std::string zone = argv[m];
00085 try {
00086 UTMUPS::DecodeZone(zone, zonenum, northp);
00087 }
00088 catch (const std::exception& e) {
00089 std::cerr << "Error decoding zone: " << e.what() << "\n";
00090 return 1;
00091 }
00092 if (!(zonenum >= UTMUPS::MINZONE && zonenum <= UTMUPS::MAXZONE)) {
00093 std::cerr << "Illegal zone " << zone << "\n";
00094 return 1;
00095 }
00096 } else if (arg == "-n") {
00097 if (++m == argc) return usage(1, true);
00098 geoid = argv[m];
00099 } else if (arg == "-d") {
00100 if (++m == argc) return usage(1, true);
00101 dir = argv[m];
00102 } else if (arg == "-l")
00103 cubic = false;
00104 else if (arg == "-g")
00105 gradp = true;
00106 else if (arg == "-v")
00107 verbose = true;
00108 else if (arg == "--input-string") {
00109 if (++m == argc) return usage(1, true);
00110 istring = argv[m];
00111 } else if (arg == "--input-file") {
00112 if (++m == argc) return usage(1, true);
00113 ifile = argv[m];
00114 } else if (arg == "--output-file") {
00115 if (++m == argc) return usage(1, true);
00116 ofile = argv[m];
00117 } else if (arg == "--line-separator") {
00118 if (++m == argc) return usage(1, true);
00119 if (std::string(argv[m]).size() != 1) {
00120 std::cerr << "Line separator must be a single character\n";
00121 return 1;
00122 }
00123 lsep = argv[m][0];
00124 } else if (arg == "--comment-delimiter") {
00125 if (++m == argc) return usage(1, true);
00126 cdelim = argv[m];
00127 } else if (arg == "--version") {
00128 std::cout
00129 << argv[0] << ": GeographicLib version "
00130 << GEOGRAPHICLIB_VERSION_STRING << "\n";
00131 return 0;
00132 } else {
00133 int retval = usage(!(arg == "-h" || arg == "--help"), arg != "--help");
00134 if (arg == "-h")
00135 std::cout<< "\nDefault geoid path = \"" << Geoid::DefaultGeoidPath()
00136 << "\"\nDefault geoid name = \"" << Geoid::DefaultGeoidName()
00137 << "\"\n";
00138 return retval;
00139 }
00140 }
00141
00142 if (!ifile.empty() && !istring.empty()) {
00143 std::cerr << "Cannot specify --input-string and --input-file together\n";
00144 return 1;
00145 }
00146 if (ifile == "-") ifile.clear();
00147 std::ifstream infile;
00148 std::istringstream instring;
00149 if (!ifile.empty()) {
00150 infile.open(ifile.c_str());
00151 if (!infile.is_open()) {
00152 std::cerr << "Cannot open " << ifile << " for reading\n";
00153 return 1;
00154 }
00155 } else if (!istring.empty()) {
00156 std::string::size_type m = 0;
00157 while (true) {
00158 m = istring.find(lsep, m);
00159 if (m == std::string::npos)
00160 break;
00161 istring[m] = '\n';
00162 }
00163 instring.str(istring);
00164 }
00165 std::istream* input = !ifile.empty() ? &infile :
00166 (!istring.empty() ? &instring : &std::cin);
00167
00168 std::ofstream outfile;
00169 if (ofile == "-") ofile.clear();
00170 if (!ofile.empty()) {
00171 outfile.open(ofile.c_str());
00172 if (!outfile.is_open()) {
00173 std::cerr << "Cannot open " << ofile << " for writing\n";
00174 return 1;
00175 }
00176 }
00177 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
00178
00179 int retval = 0;
00180 try {
00181 const Geoid g(geoid, dir, cubic);
00182 try {
00183 if (cacheall)
00184 g.CacheAll();
00185 else if (cachearea)
00186 g.CacheArea(caches, cachew, cachen, cachee);
00187 }
00188 catch (const std::exception& e) {
00189 std::cerr << "ERROR: " << e.what() << "\nProceeding without a cache\n";
00190 }
00191 if (verbose) {
00192 std::cerr << "Geoid file: " << g.GeoidFile() << "\n"
00193 << "Description: " << g.Description() << "\n"
00194 << "Interpolation: " << g.Interpolation() << "\n"
00195 << "Date & Time: " << g.DateTime() << "\n"
00196 << "Offset (m): " << g.Offset() << "\n"
00197 << "Scale (m): " << g.Scale() << "\n"
00198 << "Max error (m): " << g.MaxError() << "\n"
00199 << "RMS error (m): " << g.RMSError() << "\n";
00200 if (g.Cache())
00201 std::cerr<< "Caching:"
00202 << "\n SW Corner: " << g.CacheSouth() << " " << g.CacheWest()
00203 << "\n NE Corner: " << g.CacheNorth() << " " << g.CacheEast()
00204 << "\n";
00205 }
00206
00207 GeoCoords p;
00208 std::string s, suff;
00209 const char* spaces = " \t\n\v\f\r,";
00210 while (std::getline(*input, s)) {
00211 try {
00212 std::string eol("\n");
00213 if (!cdelim.empty()) {
00214 std::string::size_type m = s.find(cdelim);
00215 if (m != std::string::npos) {
00216 eol = " " + s.substr(m) + "\n";
00217 std::string::size_type m1 =
00218 m > 0 ? s.find_last_not_of(spaces, m - 1) : std::string::npos;
00219 s = s.substr(0, m1 != std::string::npos ? m1 + 1 : m);
00220 }
00221 }
00222 real height = 0;
00223 if (zonenum != UTMUPS::INVALID) {
00224
00225
00226 std::string::size_type pa = 0, pb = 0;
00227 real easting = 0, northing = 0;
00228 for (int i = 0; i < (heightmult ? 3 : 2); ++i) {
00229 if (pb == std::string::npos)
00230 throw GeographicErr("Incomplete input: " + s);
00231
00232 pa = s.find_first_not_of(spaces, pb);
00233 if (pa == std::string::npos)
00234 throw GeographicErr("Incomplete input: " + s);
00235
00236 pb = s.find_first_of(spaces, pa);
00237 (i == 2 ? height : (i == 0 ? easting : northing)) =
00238 Utility::num<real>(s.substr(pa, (pb == std::string::npos ?
00239 pb : pb - pa)));
00240 }
00241 p.Reset(zonenum, northp, easting, northing);
00242 if (heightmult) {
00243 suff = pb == std::string::npos ? "" : s.substr(pb);
00244 s = s.substr(0, pa);
00245 }
00246 } else {
00247 if (heightmult) {
00248
00249
00250
00251
00252 std::string::size_type pb = s.find_last_not_of(spaces);
00253 std::string::size_type pa = s.find_last_of(spaces, pb);
00254 if (pa == std::string::npos || pb == std::string::npos)
00255 throw GeographicErr("Incomplete input: " + s);
00256 height = Utility::num<real>(s.substr(pa + 1, pb - pa));
00257 s = s.substr(0, pa + 1);
00258 }
00259 p.Reset(s);
00260 }
00261 if (heightmult) {
00262 real h = g(p.Latitude(), p.Longitude());
00263 *output << s
00264 << Utility::str(height + real(heightmult) * h, 4)
00265 << suff << eol;
00266 } else {
00267 if (gradp) {
00268 real gradn, grade;
00269 real h = g(p.Latitude(), p.Longitude(), gradn, grade);
00270 *output << Utility::str(h, 4) << " "
00271 << Utility::str(gradn * 1e6, 2)
00272 << (Math::isnan(gradn) ? " " : "e-6 ")
00273 << Utility::str(grade * 1e6, 2)
00274 << (Math::isnan(grade) ? "" : "e-6")
00275 << eol;
00276 } else {
00277 real h = g(p.Latitude(), p.Longitude());
00278 *output << Utility::str(h, 4) << eol;
00279 }
00280 }
00281 }
00282 catch (const std::exception& e) {
00283 *output << "ERROR: " << e.what() << "\n";
00284 retval = 1;
00285 }
00286 }
00287 }
00288 catch (const std::exception& e) {
00289 std::cerr << "Error reading " << geoid << ": " << e.what() << "\n";
00290 retval = 1;
00291 }
00292 return retval;
00293 }
00294 catch (const std::exception& e) {
00295 std::cerr << "Caught exception: " << e.what() << "\n";
00296 return 1;
00297 }
00298 catch (...) {
00299 std::cerr << "Caught unknown exception\n";
00300 return 1;
00301 }
00302 }