GeographicLib  2.6
GeodSolve.cpp
Go to the documentation of this file.
1 /**
2  * \file GeodSolve.cpp
3  * \brief Command line utility for geodesic calculations
4  *
5  * Copyright (c) Charles Karney (2009-2023) <karney@alum.mit.edu> and licensed
6  * under the MIT/X11 License. For more information, see
7  * https://geographiclib.sourceforge.io/
8  *
9  * See the <a href="GeodSolve.1.html">man page</a> for usage information.
10  **********************************************************************/
11 
12 #include <iostream>
13 #include <string>
14 #include <sstream>
15 #include <fstream>
18 #include <GeographicLib/DMS.hpp>
20 
21 #if defined(_MSC_VER)
22 // Squelch warnings about potentially uninitialized local variables
23 # pragma warning (disable: 4701)
24 #endif
25 
26 #include "GeodSolve.usage"
27 
29 
30 std::string LatLonString(real lat, real lon, int prec, bool dms, char dmssep,
31  bool longfirst) {
32  using namespace GeographicLib;
33  std::string
34  latstr = dms ? DMS::Encode(lat, prec + 5, DMS::LATITUDE, dmssep) :
35  DMS::Encode(lat, prec + 5, DMS::NUMBER),
36  lonstr = dms ? DMS::Encode(lon, prec + 5, DMS::LONGITUDE, dmssep) :
37  DMS::Encode(lon, prec + 5, DMS::NUMBER);
38  return
39  (longfirst ? lonstr : latstr) + " " + (longfirst ? latstr : lonstr);
40 }
41 
42 std::string AzimuthString(real azi, int prec, bool dms, char dmssep) {
43  using namespace GeographicLib;
44  return dms ? DMS::Encode(azi, prec + 5, DMS::AZIMUTH, dmssep) :
45  DMS::Encode(azi, prec + 5, DMS::NUMBER);
46 }
47 
48 std::string DistanceStrings(real s12, real a12,
49  bool full, bool arcmode, int prec, bool dms) {
50  using namespace GeographicLib;
51  std::string s;
52  if (full || !arcmode)
53  s += Utility::str(s12, prec);
54  if (full)
55  s += " ";
56  if (full || arcmode)
57  s += DMS::Encode(a12, prec + 5, dms ? DMS::NONE : DMS::NUMBER);
58  return s;
59 }
60 
61 real ReadDistance(const std::string& s, bool arcmode, bool fraction = false) {
62  using namespace GeographicLib;
63  return fraction ? Utility::fract<real>(s) :
64  (arcmode ? DMS::DecodeAngle(s) : Utility::val<real>(s));
65 }
66 
67 int main(int argc, const char* const argv[]) {
68  try {
69  using namespace GeographicLib;
70  enum { NONE = 0, LINE, DIRECT, INVERSE };
72  bool inverse = false, arcmode = false,
73  dms = false, full = false, exact = false, unroll = false,
74  longfirst = false, azi2back = false, fraction = false,
75  arcmodeline = false;
76  real
77  a = Constants::WGS84_a(),
78  f = Constants::WGS84_f();
79  real lat1, lon1, azi1, lat2, lon2, azi2, s12, m12, a12, M12, M21, S12,
80  mult = 1;
81  int linecalc = NONE, prec = 3;
82  std::string istring, ifile, ofile, cdelim;
83  char lsep = ';', dmssep = char(0);
84 
85  for (int m = 1; m < argc; ++m) {
86  std::string arg(argv[m]);
87  if (arg == "-i") {
88  inverse = true;
89  linecalc = NONE;
90  } else if (arg == "-a")
91  arcmode = !arcmode;
92  else if (arg == "-F")
93  fraction = true;
94  else if (arg == "-L") {
95  inverse = false;
96  linecalc = LINE;
97  if (m + 3 >= argc) return usage(1, true);
98  try {
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]));
102  }
103  catch (const std::exception& e) {
104  std::cerr << "Error decoding arguments of -L: " << e.what() << "\n";
105  return 1;
106  }
107  m += 3;
108  } else if (arg == "-D") {
109  inverse = false;
110  linecalc = DIRECT;
111  if (m + 4 >= argc) return usage(1, true);
112  try {
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]));
116  s12 = ReadDistance(std::string(argv[m + 4]), arcmode);
117  arcmodeline = arcmode;
118  }
119  catch (const std::exception& e) {
120  std::cerr << "Error decoding arguments of -D: " << e.what() << "\n";
121  return 1;
122  }
123  m += 4;
124  } else if (arg == "-I") {
125  inverse = false;
126  linecalc = INVERSE;
127  if (m + 4 >= argc) return usage(1, true);
128  try {
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);
133  }
134  catch (const std::exception& e) {
135  std::cerr << "Error decoding arguments of -I: " << e.what() << "\n";
136  return 1;
137  }
138  m += 4;
139  } else if (arg == "-e") {
140  if (m + 2 >= argc) return usage(1, true);
141  try {
142  a = Utility::val<real>(std::string(argv[m + 1]));
143  f = Utility::fract<real>(std::string(argv[m + 2]));
144  }
145  catch (const std::exception& e) {
146  std::cerr << "Error decoding arguments of -e: " << e.what() << "\n";
147  return 1;
148  }
149  m += 2;
150  } else if (arg == "-u")
151  unroll = true;
152  else if (arg == "-d") {
153  dms = true;
154  dmssep = '\0';
155  } else if (arg == "-:") {
156  dms = true;
157  dmssep = ':';
158  } else if (arg == "-w")
159  longfirst = !longfirst;
160  else if (arg == "-b")
161  azi2back = true;
162  else if (arg == "-f")
163  full = true;
164  else if (arg == "-p") {
165  if (++m == argc) return usage(1, true);
166  try {
167  prec = Utility::val<int>(std::string(argv[m]));
168  }
169  catch (const std::exception&) {
170  std::cerr << "Precision " << argv[m] << " is not a number\n";
171  return 1;
172  }
173  } else if (arg == "-E")
174  exact = true;
175  else if (arg == "--input-string") {
176  if (++m == argc) return usage(1, true);
177  istring = argv[m];
178  } else if (arg == "--input-file") {
179  if (++m == argc) return usage(1, true);
180  ifile = argv[m];
181  } else if (arg == "--output-file") {
182  if (++m == argc) return usage(1, true);
183  ofile = argv[m];
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";
188  return 1;
189  }
190  lsep = argv[m][0];
191  } else if (arg == "--comment-delimiter") {
192  if (++m == argc) return usage(1, true);
193  cdelim = argv[m];
194  } else if (arg == "--version") {
195  std::cout << argv[0] << ": GeographicLib version "
196  << GEOGRAPHICLIB_VERSION_STRING << "\n";
197  return 0;
198  } else
199  return usage(!(arg == "-h" || arg == "--help"), arg != "--help");
200  }
201 
202  if (!ifile.empty() && !istring.empty()) {
203  std::cerr << "Cannot specify --input-string and --input-file together\n";
204  return 1;
205  }
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";
213  return 1;
214  }
215  } else if (!istring.empty()) {
216  std::string::size_type m = 0;
217  while (true) {
218  m = istring.find(lsep, m);
219  if (m == std::string::npos)
220  break;
221  istring[m] = '\n';
222  }
223  instring.str(istring);
224  }
225  std::istream* input = !ifile.empty() ? &infile :
226  (!istring.empty() ? &instring : &std::cin);
227 
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";
234  return 1;
235  }
236  }
237  std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
238 
239  unsigned outmask = Geodesic::LATITUDE | Geodesic::LONGITUDE |
240  Geodesic::AZIMUTH; // basic output quantities
241  outmask |= inverse ? Geodesic::DISTANCE : // distance-related flags
243  // longitude unrolling
244  outmask |= unroll ? Geodesic::LONG_UNROLL : Geodesic::NONE;
245  // full output -- don't use Geodesic::ALL since this includes DISTANCE_IN
246  outmask |= full ? (Geodesic::DISTANCE | Geodesic::REDUCEDLENGTH |
249 
250  const Geodesic geods(a, f, exact);
251  GeodesicLine ls;
252  if (linecalc) {
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) :
258  // linecalc == LINE
259  geods.Line(lat1, lon1, azi1, outmask);
260  mult = fraction ? ls.GenDistance(arcmode) : 1;
261  if (linecalc == INVERSE) azi1 = ls.Azimuth();
262  }
263 
264  // Max precision = 10: 0.1 nm in distance, 10^-15 deg (= 0.11 nm),
265  // 10^-11 sec (= 0.3 nm).
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;
269  int retval = 0;
270  while (std::getline(*input, s)) {
271  try {
272  eol = "\n";
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";
277  s = s.substr(0, m);
278  }
279  }
280  str.clear(); str.str(s);
281  if (inverse) {
282  if (!(str >> slat1 >> slon1 >> slat2 >> slon2))
283  throw GeographicErr("Incomplete input: " + s);
284  if (str >> strc)
285  throw GeographicErr("Extraneous input: " + strc);
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);
290  if (full) {
291  if (unroll) {
292  real e;
293  lon2 = lon1 + Math::AngDiff(lon1, lon2, e);
294  lon2 += e;
295  } else {
296  lon1 = Math::AngNormalize(lon1);
297  lon2 = Math::AngNormalize(lon2);
298  }
299  *output << LatLonString(lat1, lon1, prec, dms, dmssep, longfirst)
300  << " ";
301  }
302  *output << AzimuthString(azi1, prec, dms, dmssep) << " ";
303  if (full)
304  *output << LatLonString(lat2, lon2, prec, dms, dmssep, longfirst)
305  << " ";
306  if (azi2back) {
307  using std::copysign;
308  // map +/-0 -> -/+180; +/-180 -> -/+0
309  // this depends on abs(azi2) <= 180
310  azi2 = copysign(azi2 + copysign(real(Math::hd), -azi2), -azi2);
311  }
312  *output << AzimuthString(azi2, prec, dms, dmssep) << " "
313  << DistanceStrings(s12, a12, full, arcmode, prec, dms);
314  if (full)
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));
319  *output << eol;
320  } else {
321  if (linecalc) {
322  if (!(str >> ss12))
323  throw GeographicErr("Incomplete input: " + s);
324  if (str >> strc)
325  throw GeographicErr("Extraneous input: " + strc);
326  // In fraction mode input is read as a distance
327  s12 = ReadDistance(ss12, !fraction && arcmode, fraction) * mult;
328  a12 = ls.GenPosition(arcmode, s12, outmask,
329  lat2, lon2, azi2, s12, m12, M12, M21, S12);
330  } else {
331  if (!(str >> slat1 >> slon1 >> sazi1 >> ss12))
332  throw GeographicErr("Incomplete input: " + s);
333  if (str >> strc)
334  throw GeographicErr("Extraneous input: " + strc);
335  DMS::DecodeLatLon(slat1, slon1, lat1, lon1, longfirst);
336  azi1 = DMS::DecodeAzimuth(sazi1);
337  s12 = ReadDistance(ss12, arcmode);
338  a12 = geods.GenDirect(lat1, lon1, azi1, arcmode, s12, outmask,
339  lat2, lon2, azi2, s12, m12, M12, M21, S12);
340  }
341  if (full)
342  *output
343  << LatLonString(lat1, unroll ? lon1 : Math::AngNormalize(lon1),
344  prec, dms, dmssep, longfirst)
345  << " " << AzimuthString(azi1, prec, dms, dmssep) << " ";
346  if (azi2back) {
347  using std::copysign;
348  // map +/-0 -> -/+180; +/-180 -> -/+0
349  // this depends on abs(azi2) <= 180
350  azi2 = copysign(azi2 + copysign(real(Math::hd), -azi2), -azi2);
351  }
352  *output << LatLonString(lat2, lon2, prec, dms, dmssep, longfirst)
353  << " " << AzimuthString(azi2, prec, dms, dmssep);
354  if (full)
355  *output << " "
356  << DistanceStrings(s12, a12, full, arcmode, prec, dms)
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));
361  *output << eol;
362  }
363  }
364  catch (const std::exception& e) {
365  // Write error message cout so output lines match input lines
366  *output << "ERROR: " << e.what() << "\n";
367  retval = 1;
368  }
369  }
370  return retval;
371  }
372  catch (const std::exception& e) {
373  std::cerr << "Caught exception: " << e.what() << "\n";
374  return 1;
375  }
376  catch (...) {
377  std::cerr << "Caught unknown exception\n";
378  return 1;
379  }
380 }
Header for GeographicLib::GeodesicLine class.
static Math::real DecodeAngle(const std::string &angstr)
Definition: DMS.cpp:404
int main(int argc, const char *const argv[])
Definition: GeodSolve.cpp:67
GeographicLib::Math::real real
Definition: GeodSolve.cpp:28
Header for GeographicLib::Utility class.
Math::real GenDistance(bool arcmode) const
static constexpr int hd
degrees per half turn
Definition: Math.hpp:145
std::string LatLonString(real lat, real lon, int prec, bool dms, char dmssep, bool longfirst)
Definition: GeodSolve.cpp:30
Math::real Azimuth() const
static int extra_digits()
Definition: Math.cpp:49
int usage(int retval, bool)
Definition: Geod3ODE.cpp:41
Header for GeographicLib::Geodesic class.
static std::string Encode(real angle, component trailing, unsigned prec, flag ind=NONE, char dmssep=char(0))
Definition: DMS.cpp:422
static T AngNormalize(T x)
Definition: Math.cpp:69
static Math::real DecodeAzimuth(const std::string &azistr)
Definition: DMS.cpp:413
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
static std::string str(T x, int p=-1)
Definition: Utility.hpp:161
static T AngDiff(T x, T y, T &e)
Definition: Math.cpp:80
static void DecodeLatLon(const std::string &dmsa, const std::string &dmsb, real &lat, real &lon, bool longfirst=false)
Definition: DMS.cpp:374
GeographicLib::Math::real real
Definition: Geod3Solve.cpp:25
static int set_digits(int ndigits=0)
Definition: Utility.cpp:184
Exception handling for GeographicLib.
Definition: Constants.hpp:344
std::string AzimuthString(real azi, int prec, bool dms, char dmssep)
Definition: GeodSolve.cpp:42
real ReadDistance(const std::string &s, bool arcmode, bool fraction=false)
Definition: GeodSolve.cpp:61
std::string DistanceStrings(real s12, real a12, bool full, bool arcmode, int prec, bool dms)
Definition: GeodSolve.cpp:48
Math::real GenPosition(bool arcmode, real s12_a12, unsigned outmask, real &lat2, real &lon2, real &azi2, real &s12, real &m12, real &M12, real &M21, real &S12) const
Geodesic calculations
Definition: Geodesic.hpp:175
Header for GeographicLib::DMS class.