67int main(
int argc,
const char*
const argv[]) {
70 enum { NONE = 0, LINE, DIRECT, INVERSE };
71 Utility::set_digits();
72 bool inverse =
false, arcmode =
false,
73 dms =
false, full =
false, exact =
false, unroll =
false,
74 longfirst =
false, azi2back =
false, fraction =
false,
77 a = Constants::WGS84_a(),
78 f = Constants::WGS84_f();
79 real lat1, lon1, azi1, lat2, lon2, azi2, s12, m12, a12, M12, M21, S12,
81 int linecalc = NONE, prec = 3;
82 std::string istring, ifile, ofile, cdelim;
83 char lsep =
';', dmssep = char(0);
85 for (
int m = 1; m < argc; ++m) {
86 std::string arg(argv[m]);
90 }
else if (arg ==
"-a")
94 else if (arg ==
"-L") {
97 if (m + 3 >= argc)
return usage(1,
true);
99 DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),
100 lat1, lon1, longfirst);
101 azi1 = DMS::DecodeAzimuth(std::string(argv[m + 3]));
103 catch (
const std::exception& e) {
104 std::cerr <<
"Error decoding arguments of -L: " << e.what() <<
"\n";
108 }
else if (arg ==
"-D") {
111 if (m + 4 >= argc)
return usage(1,
true);
113 DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),
114 lat1, lon1, longfirst);
115 azi1 = DMS::DecodeAzimuth(std::string(argv[m + 3]));
117 arcmodeline = arcmode;
119 catch (
const std::exception& e) {
120 std::cerr <<
"Error decoding arguments of -D: " << e.what() <<
"\n";
124 }
else if (arg ==
"-I") {
127 if (m + 4 >= argc)
return usage(1,
true);
129 DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),
130 lat1, lon1, longfirst);
131 DMS::DecodeLatLon(std::string(argv[m + 3]), std::string(argv[m + 4]),
132 lat2, lon2, longfirst);
134 catch (
const std::exception& e) {
135 std::cerr <<
"Error decoding arguments of -I: " << e.what() <<
"\n";
139 }
else if (arg ==
"-e") {
140 if (m + 2 >= argc)
return usage(1,
true);
142 a = Utility::val<real>(std::string(argv[m + 1]));
143 f = Utility::fract<real>(std::string(argv[m + 2]));
145 catch (
const std::exception& e) {
146 std::cerr <<
"Error decoding arguments of -e: " << e.what() <<
"\n";
150 }
else if (arg ==
"-u")
152 else if (arg ==
"-d") {
155 }
else if (arg ==
"-:") {
158 }
else if (arg ==
"-w")
159 longfirst = !longfirst;
160 else if (arg ==
"-b")
162 else if (arg ==
"-f")
164 else if (arg ==
"-p") {
165 if (++m == argc)
return usage(1,
true);
167 prec = Utility::val<int>(std::string(argv[m]));
169 catch (
const std::exception&) {
170 std::cerr <<
"Precision " << argv[m] <<
" is not a number\n";
173 }
else if (arg ==
"-E")
175 else if (arg ==
"--input-string") {
176 if (++m == argc)
return usage(1,
true);
178 }
else if (arg ==
"--input-file") {
179 if (++m == argc)
return usage(1,
true);
181 }
else if (arg ==
"--output-file") {
182 if (++m == argc)
return usage(1,
true);
184 }
else if (arg ==
"--line-separator") {
185 if (++m == argc)
return usage(1,
true);
186 if (std::string(argv[m]).size() != 1) {
187 std::cerr <<
"Line separator must be a single character\n";
191 }
else if (arg ==
"--comment-delimiter") {
192 if (++m == argc)
return usage(1,
true);
194 }
else if (arg ==
"--version") {
195 std::cout << argv[0] <<
": GeographicLib version "
196 << GEOGRAPHICLIB_VERSION_STRING <<
"\n";
199 return usage(!(arg ==
"-h" || arg ==
"--help"), arg !=
"--help");
202 if (!ifile.empty() && !istring.empty()) {
203 std::cerr <<
"Cannot specify --input-string and --input-file together\n";
206 if (ifile ==
"-") ifile.clear();
207 std::ifstream infile;
208 std::istringstream instring;
209 if (!ifile.empty()) {
210 infile.open(ifile.c_str());
211 if (!infile.is_open()) {
212 std::cerr <<
"Cannot open " << ifile <<
" for reading\n";
215 }
else if (!istring.empty()) {
216 std::string::size_type m = 0;
218 m = istring.find(lsep, m);
219 if (m == std::string::npos)
223 instring.str(istring);
225 std::istream* input = !ifile.empty() ? &infile :
226 (!istring.empty() ? &instring : &std::cin);
228 std::ofstream outfile;
229 if (ofile ==
"-") ofile.clear();
230 if (!ofile.empty()) {
231 outfile.open(ofile.c_str());
232 if (!outfile.is_open()) {
233 std::cerr <<
"Cannot open " << ofile <<
" for writing\n";
237 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
239 unsigned outmask = Geodesic::LATITUDE | Geodesic::LONGITUDE |
241 outmask |= inverse ? Geodesic::DISTANCE :
242 (arcmode ? Geodesic::NONE : Geodesic::DISTANCE_IN);
244 outmask |= unroll ? Geodesic::LONG_UNROLL : Geodesic::NONE;
246 outmask |= full ? (Geodesic::DISTANCE | Geodesic::REDUCEDLENGTH |
247 Geodesic::GEODESICSCALE | Geodesic::AREA) :
253 if (linecalc == LINE) fraction =
false;
254 ls = linecalc == DIRECT ?
255 geods.
GenDirectLine(lat1, lon1, azi1, arcmodeline, s12, outmask) :
256 linecalc == INVERSE ?
257 geods.
InverseLine(lat1, lon1, lat2, lon2, outmask) :
259 geods.
Line(lat1, lon1, azi1, outmask);
261 if (linecalc == INVERSE) azi1 = ls.
Azimuth();
266 prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
267 std::string s, eol, slat1, slon1, slat2, slon2, sazi1, ss12, strc;
268 std::istringstream str;
270 while (std::getline(*input, s)) {
273 if (!cdelim.empty()) {
274 std::string::size_type m = s.find(cdelim);
275 if (m != std::string::npos) {
276 eol =
" " + s.substr(m) +
"\n";
280 str.clear(); str.str(s);
282 if (!(str >> slat1 >> slon1 >> slat2 >> slon2))
286 DMS::DecodeLatLon(slat1, slon1, lat1, lon1, longfirst);
287 DMS::DecodeLatLon(slat2, slon2, lat2, lon2, longfirst);
288 a12 = geods.GenInverse(lat1, lon1, lat2, lon2, outmask,
289 s12, azi1, azi2, m12, M12, M21, S12);
293 lon2 = lon1 + Math::AngDiff(lon1, lon2, e);
296 lon1 = Math::AngNormalize(lon1);
297 lon2 = Math::AngNormalize(lon2);
299 *output <<
LatLonString(lat1, lon1, prec, dms, dmssep, longfirst)
304 *output <<
LatLonString(lat2, lon2, prec, dms, dmssep, longfirst)
310 azi2 = copysign(azi2 + copysign(
real(Math::hd), -azi2), -azi2);
315 *output <<
" " << Utility::str(m12, prec)
316 <<
" " << Utility::str(M12, prec+7)
317 <<
" " << Utility::str(M21, prec+7)
318 <<
" " << Utility::str(S12, std::max(prec-7, 0));
327 s12 =
ReadDistance(ss12, !fraction && arcmode, fraction) * mult;
329 lat2, lon2, azi2, s12, m12, M12, M21, S12);
331 if (!(str >> slat1 >> slon1 >> sazi1 >> ss12))
335 DMS::DecodeLatLon(slat1, slon1, lat1, lon1, longfirst);
336 azi1 = DMS::DecodeAzimuth(sazi1);
338 a12 = geods.
GenDirect(lat1, lon1, azi1, arcmode, s12, outmask,
339 lat2, lon2, azi2, s12, m12, M12, M21, S12);
343 <<
LatLonString(lat1, unroll ? lon1 : Math::AngNormalize(lon1),
344 prec, dms, dmssep, longfirst)
350 azi2 = copysign(azi2 + copysign(
real(Math::hd), -azi2), -azi2);
352 *output <<
LatLonString(lat2, lon2, prec, dms, dmssep, longfirst)
357 <<
" " << Utility::str(m12, prec)
358 <<
" " << Utility::str(M12, prec+7)
359 <<
" " << Utility::str(M21, prec+7)
360 <<
" " << Utility::str(S12, std::max(prec-7, 0));
364 catch (
const std::exception& e) {
366 *output <<
"ERROR: " << e.what() <<
"\n";
372 catch (
const std::exception& e) {
373 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
377 std::cerr <<
"Caught unknown exception\n";