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

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

31 try {

34 Utility::set_digits();

35 bool azimuthal = false, cassini = false, gnomonic = false, reverse = false,

36 longfirst = false;

37 real lat0 = 0, lon0 = 0;

39 a = Constants::WGS84_a(),

40 f = Constants::WGS84_f();

41 int prec = 6;

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

43 char lsep = ';';

44

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

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

47 if (arg == "-r")

48 reverse = true;

49 else if (arg == "-c" || arg == "-z" || arg == "-g") {

50 cassini = azimuthal = gnomonic = false;

51 cassini = arg == "-c";

52 azimuthal = arg == "-z";

53 gnomonic = arg == "-g";

54 if (m + 2 >= argc) return usage(1, true);

55 try {

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

57 lat0, lon0, longfirst);

58 }

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

60 std::cerr << "Error decoding arguments of " << arg << ": "

61 << e.what() << "\n";

62 return 1;

63 }

64 m += 2;

65 } else if (arg == "-e") {

66 if (m + 2 >= argc) return usage(1, true);

67 try {

68 a = Utility::val(std::string(argv[m + 1]));

69 f = Utility::fract(std::string(argv[m + 2]));

70 }

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

72 std::cerr << "Error decoding arguments of -e: " << e.what() << "\n";

73 return 1;

74 }

75 m += 2;

76 } else if (arg == "-w")

77 longfirst = !longfirst;

78 else if (arg == "-p") {

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

80 try {

81 prec = Utility::val(std::string(argv[m]));

82 }

83 catch (const std::exception&) {

84 std::cerr << "Precision " << argv[m] << " is not a number\n";

85 return 1;

86 }

87 } else if (arg == "--input-string") {

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

89 istring = argv[m];

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

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

92 ifile = argv[m];

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

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

95 ofile = argv[m];

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

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

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

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

100 return 1;

101 }

102 lsep = argv[m][0];

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

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

105 cdelim = argv[m];

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

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

108 << GEOGRAPHICLIB_VERSION_STRING << "\n";

109 return 0;

110 } else

111 return usage(!(arg == "-h" || arg == "--help"), arg != "--help");

112 }

113

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

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

116 return 1;

117 }

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

119 std::ifstream infile;

120 std::istringstream instring;

121 if (!ifile.empty()) {

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

123 if (!infile.is_open()) {

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

125 return 1;

126 }

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

128 std:🧵:size_type m = 0;

129 while (true) {

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

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

132 break;

133 istring[m] = '\n';

134 }

135 instring.str(istring);

136 }

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

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

139

140 std::ofstream outfile;

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

142 if (!ofile.empty()) {

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

144 if (!outfile.is_open()) {

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

146 return 1;

147 }

148 }

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

150

151 if (!(azimuthal || cassini || gnomonic)) {

152 std::cerr << "Must specify \"-z lat0 lon0\" or "

153 << "\"-c lat0 lon0\" or \"-g lat0 lon0\"\n";

154 return 1;

155 }

156

162

163

164

165 prec = std::min(10 + Math::extra_digits(), std::max(0, prec));

166 std::string s, eol, stra, strb, strc;

167 std::istringstream str;

168 int retval = 0;

169 std::cout << std::fixed;

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

171 try {

172 eol = "\n";

173 if (!cdelim.empty()) {

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

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

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

177 s = s.substr(0, m);

178 }

179 }

180 str.clear(); str.str(s);

181 real lat, lon, x, y, azi, rk;

182 if (!(str >> stra >> strb))

184 if (reverse) {

185 x = Utility::val(stra);

186 y = Utility::val(strb);

187 } else

188 DMS::DecodeLatLon(stra, strb, lat, lon, longfirst);

189 if (str >> strc)

190 throw GeographicErr("Extraneous input: " + strc);

191 if (reverse) {

192 if (cassini)

193 cs.Reverse(x, y, lat, lon, azi, rk);

194 else if (azimuthal)

195 az.Reverse(lat0, lon0, x, y, lat, lon, azi, rk);

196 else

197 gn.Reverse(lat0, lon0, x, y, lat, lon, azi, rk);

198 *output << Utility::str(longfirst ? lon : lat, prec + 5) << " "

199 << Utility::str(longfirst ? lat : lon, prec + 5) << " "

200 << Utility::str(azi, prec + 5) << " "

201 << Utility::str(rk, prec + 6) << eol;

202 } else {

203 if (cassini)

204 cs.Forward(lat, lon, x, y, azi, rk);

205 else if (azimuthal)

206 az.Forward(lat0, lon0, lat, lon, x, y, azi, rk);

207 else

208 gn.Forward(lat0, lon0, lat, lon, x, y, azi, rk);

209 *output << Utility::str(x, prec) << " "

210 << Utility::str(y, prec) << " "

211 << Utility::str(azi, prec + 5) << " "

212 << Utility::str(rk, prec + 6) << eol;

213 }

214 }

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

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

217 retval = 1;

218 }

219 }

220 return retval;

221 }

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

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

224 return 1;

225 }

226 catch (...) {

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

228 return 1;

229 }

230}