GeographicLib: GeoidEval.cpp Source File (original) (raw)

28int main(int argc, const char* const argv[]) {

29 try {

32 Utility::set_digits();

33 bool cacheall = false, cachearea = false, verbose = false, cubic = true;

34 real caches, cachew, cachen, cachee;

35 std::string dir;

36 std::string geoid = Geoid::DefaultGeoidName();

38 std::string istring, ifile, ofile, cdelim;

39 char lsep = ';';

40 bool northp = false, longfirst = false;

41 int zonenum = UTMUPS::INVALID;

42

43 for (int m = 1; m < argc; ++m) {

44 std::string arg(argv[m]);

45 if (arg == "-a") {

46 cacheall = true;

47 cachearea = false;

48 }

49 else if (arg == "-c") {

50 if (m + 4 >= argc) return usage(1, true);

51 cacheall = false;

52 cachearea = true;

53 try {

54 DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),

55 caches, cachew, longfirst);

56 DMS::DecodeLatLon(std::string(argv[m + 3]), std::string(argv[m + 4]),

57 cachen, cachee, longfirst);

58 }

59 catch (const std::exception& e) {

60 std::cerr << "Error decoding argument of -c: " << e.what() << "\n";

61 return 1;

62 }

63 m += 4;

64 } else if (arg == "--msltohae")

65 heightmult = Geoid::GEOIDTOELLIPSOID;

66 else if (arg == "--haetomsl")

67 heightmult = Geoid::ELLIPSOIDTOGEOID;

68 else if (arg == "-w")

69 longfirst = !longfirst;

70 else if (arg == "-z") {

71 if (++m == argc) return usage(1, true);

72 std::string zone = argv[m];

73 try {

74 UTMUPS::DecodeZone(zone, zonenum, northp);

75 }

76 catch (const std::exception& e) {

77 std::cerr << "Error decoding zone: " << e.what() << "\n";

78 return 1;

79 }

80 if (!(zonenum >= UTMUPS::MINZONE && zonenum <= UTMUPS::MAXZONE)) {

81 std::cerr << "Illegal zone " << zone << "\n";

82 return 1;

83 }

84 } else if (arg == "-n") {

85 if (++m == argc) return usage(1, true);

86 geoid = argv[m];

87 } else if (arg == "-d") {

88 if (++m == argc) return usage(1, true);

89 dir = argv[m];

90 } else if (arg == "-l")

91 cubic = false;

92 else if (arg == "-v")

93 verbose = true;

94 else if (arg == "--input-string") {

95 if (++m == argc) return usage(1, true);

96 istring = argv[m];

97 } else if (arg == "--input-file") {

98 if (++m == argc) return usage(1, true);

99 ifile = argv[m];

100 } else if (arg == "--output-file") {

101 if (++m == argc) return usage(1, true);

102 ofile = argv[m];

103 } else if (arg == "--line-separator") {

104 if (++m == argc) return usage(1, true);

105 if (std::string(argv[m]).size() != 1) {

106 std::cerr << "Line separator must be a single character\n";

107 return 1;

108 }

109 lsep = argv[m][0];

110 } else if (arg == "--comment-delimiter") {

111 if (++m == argc) return usage(1, true);

112 cdelim = argv[m];

113 } else if (arg == "--version") {

114 std::cout << argv[0] << ": GeographicLib version "

115 << GEOGRAPHICLIB_VERSION_STRING << "\n";

116 return 0;

117 } else {

118 int retval = usage(!(arg == "-h" || arg == "--help"), arg != "--help");

119 if (arg == "-h")

120 std::cout

121 << "\nDefault geoid path = \"" << Geoid::DefaultGeoidPath()

122 << "\"\nDefault geoid name = \"" << Geoid::DefaultGeoidName()

123 << "\"\n";

124 return retval;

125 }

126 }

127

128 if (!ifile.empty() && !istring.empty()) {

129 std::cerr << "Cannot specify --input-string and --input-file together\n";

130 return 1;

131 }

132 if (ifile == "-") ifile.clear();

133 std::ifstream infile;

134 std::istringstream instring;

135 if (!ifile.empty()) {

136 infile.open(ifile.c_str());

137 if (!infile.is_open()) {

138 std::cerr << "Cannot open " << ifile << " for reading\n";

139 return 1;

140 }

141 } else if (!istring.empty()) {

142 std:🧵:size_type m = 0;

143 while (true) {

144 m = istring.find(lsep, m);

145 if (m == std:🧵:npos)

146 break;

147 istring[m] = '\n';

148 }

149 instring.str(istring);

150 }

151 std::istream* input = !ifile.empty() ? &infile :

152 (!istring.empty() ? &instring : &std::cin);

153

154 std::ofstream outfile;

155 if (ofile == "-") ofile.clear();

156 if (!ofile.empty()) {

157 outfile.open(ofile.c_str());

158 if (!outfile.is_open()) {

159 std::cerr << "Cannot open " << ofile << " for writing\n";

160 return 1;

161 }

162 }

163 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;

164

165 int retval = 0;

166 try {

167 const Geoid g(geoid, dir, cubic);

168 try {

169 if (cacheall)

171 else if (cachearea)

172 g.CacheArea(caches, cachew, cachen, cachee);

173 }

174 catch (const std::exception& e) {

175 std::cerr << "ERROR: " << e.what() << "\nProceeding without a cache\n";

176 }

177 if (verbose) {

178 std::cerr << "Geoid file: " << g.GeoidFile() << "\n"

179 << "Description: " << g.Description() << "\n"

180 << "Interpolation: " << g.Interpolation() << "\n"

181 << "Date & Time: " << g.DateTime() << "\n"

182 << "Offset (m): " << g.Offset() << "\n"

183 << "Scale (m): " << g.Scale() << "\n"

184 << "Max error (m): " << g.MaxError() << "\n"

185 << "RMS error (m): " << g.RMSError() << "\n";

187 std::cerr

188 << "Caching:"

191 << "\n";

192 }

193

195 std::string s, eol, suff;

196 const char* spaces = " \t\n\v\f\r,";

197 while (std::getline(*input, s)) {

198 try {

199 eol = "\n";

200 if (!cdelim.empty()) {

201 std:🧵:size_type m = s.find(cdelim);

202 if (m != std:🧵:npos) {

203 eol = " " + s.substr(m) + "\n";

204 std:🧵:size_type m1 =

205 m > 0 ? s.find_last_not_of(spaces, m - 1) : std:🧵:npos;

206 s = s.substr(0, m1 != std:🧵:npos ? m1 + 1 : m);

207 }

208 }

209 real height = 0;

210 if (zonenum != UTMUPS::INVALID) {

211

212

213 std:🧵:size_type pa = 0, pb = 0;

214 real easting = 0, northing = 0;

215 for (int i = 0; i < (heightmult ? 3 : 2); ++i) {

216 if (pb == std:🧵:npos)

218

219 pa = s.find_first_not_of(spaces, pb);

220 if (pa == std:🧵:npos)

222

223 pb = s.find_first_of(spaces, pa);

224 (i == 2 ? height : (i == 0 ? easting : northing)) =

225 Utility::val(s.substr(pa, (pb == std:🧵:npos ?

226 pb : pb - pa)));

227 }

228 p.Reset(zonenum, northp, easting, northing);

229 if (heightmult) {

230 suff = pb == std:🧵:npos ? "" : s.substr(pb);

231 s = s.substr(0, pa);

232 }

233 } else {

234 if (heightmult) {

235

236

237

238

239 std:🧵:size_type pb = s.find_last_not_of(spaces);

240 std:🧵:size_type pa = s.find_last_of(spaces, pb);

241 if (pa == std:🧵:npos || pb == std:🧵:npos)

243 height = Utility::val(s.substr(pa + 1, pb - pa));

244 s = s.substr(0, pa + 1);

245 }

246 p.Reset(s, true, longfirst);

247 }

248 if (heightmult) {

250 *output << s

251 << Utility::str(height + real(heightmult) * h, 4)

252 << suff << eol;

253 } else {

255 *output << Utility::str(h, 4) << eol;

256 }

257 }

258 catch (const std::exception& e) {

259 *output << "ERROR: " << e.what() << "\n";

260 retval = 1;

261 }

262 }

263 }

264 catch (const std::exception& e) {

265 std::cerr << "Error reading " << geoid << ": " << e.what() << "\n";

266 retval = 1;

267 }

268 return retval;

269 }

270 catch (const std::exception& e) {

271 std::cerr << "Caught exception: " << e.what() << "\n";

272 return 1;

273 }

274 catch (...) {

275 std::cerr << "Caught unknown exception\n";

276 return 1;

277 }

278}