GeographicLib  1.43
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-2015) <charles@karney.com> and licensed
6  * under the MIT/X11 License. For more information, see
7  * http://geographiclib.sourceforge.net/
8  *
9  * See the <a href="GeodSolve.1.html">man page</a> for usage information.
10  **********************************************************************/
11 
12 #include <iostream>
13 #include <sstream>
14 #include <string>
15 #include <sstream>
16 #include <fstream>
21 #include <GeographicLib/DMS.hpp>
23 
24 #if defined(_MSC_VER)
25 // Squelch warnings about constant conditional expressions and potentially
26 // uninitialized local variables
27 # pragma warning (disable: 4127 4701)
28 #endif
29 
30 #include "GeodSolve.usage"
31 
33 
34 std::string LatLonString(real lat, real lon, int prec, bool dms, char dmssep) {
35  using namespace GeographicLib;
36  return dms ?
37  DMS::Encode(lat, prec + 5, DMS::LATITUDE, dmssep) + " " +
38  DMS::Encode(lon, prec + 5, DMS::LONGITUDE, dmssep) :
39  DMS::Encode(lat, prec + 5, DMS::NUMBER) + " " +
40  DMS::Encode(lon, prec + 5, DMS::NUMBER);
41 }
42 
43 std::string AzimuthString(real azi, int prec, bool dms, char dmssep) {
44  using namespace GeographicLib;
45  return dms ? DMS::Encode(azi, prec + 5, DMS::AZIMUTH, dmssep) :
46  DMS::Encode(azi >= 180 ? azi - 360 : azi, prec + 5, DMS::NUMBER);
47 }
48 
49 std::string DistanceStrings(real s12, real a12,
50  bool full, bool arcmode, int prec, bool dms) {
51  using namespace GeographicLib;
52  std::string s;
53  if (full || !arcmode)
54  s += Utility::str(s12, prec);
55  if (full)
56  s += " ";
57  if (full || arcmode)
58  s += DMS::Encode(a12, prec + 5, dms ? DMS::NONE : DMS::NUMBER);
59  return s;
60 }
61 
62 real ReadDistance(const std::string& s, bool arcmode) {
63  using namespace GeographicLib;
64  return arcmode ? DMS::DecodeAngle(s) : Utility::num<real>(s);
65 }
66 
67 int main(int argc, char* argv[]) {
68  try {
69  using namespace GeographicLib;
71  bool linecalc = false, inverse = false, arcmode = false,
72  dms = false, full = false, exact = false, unroll = false;
73  real
74  a = Constants::WGS84_a(),
75  f = Constants::WGS84_f();
76  real lat1, lon1, azi1, lat2, lon2, azi2, s12, m12, a12, M12, M21, S12;
77  real azi2sense = 0;
78  int prec = 3;
79  std::string istring, ifile, ofile, cdelim;
80  char lsep = ';', dmssep = char(0);
81 
82  for (int m = 1; m < argc; ++m) {
83  std::string arg(argv[m]);
84  if (arg == "-i") {
85  inverse = true;
86  linecalc = false;
87  } else if (arg == "-a")
88  arcmode = true;
89  else if (arg == "-l") {
90  inverse = false;
91  linecalc = true;
92  if (m + 3 >= argc) return usage(1, true);
93  try {
94  DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),
95  lat1, lon1);
96  azi1 = DMS::DecodeAzimuth(std::string(argv[m + 3]));
97  }
98  catch (const std::exception& e) {
99  std::cerr << "Error decoding arguments of -l: " << e.what() << "\n";
100  return 1;
101  }
102  m += 3;
103  } else if (arg == "-e") {
104  if (m + 2 >= argc) return usage(1, true);
105  try {
106  a = Utility::num<real>(std::string(argv[m + 1]));
107  f = Utility::fract<real>(std::string(argv[m + 2]));
108  }
109  catch (const std::exception& e) {
110  std::cerr << "Error decoding arguments of -e: " << e.what() << "\n";
111  return 1;
112  }
113  m += 2;
114  } else if (arg == "-u")
115  unroll = true;
116  else if (arg == "-d") {
117  dms = true;
118  dmssep = '\0';
119  } else if (arg == "-:") {
120  dms = true;
121  dmssep = ':';
122  } else if (arg == "-b")
123  azi2sense = 180;
124  else if (arg == "-f")
125  full = true;
126  else if (arg == "-p") {
127  if (++m == argc) return usage(1, true);
128  try {
129  prec = Utility::num<int>(std::string(argv[m]));
130  }
131  catch (const std::exception&) {
132  std::cerr << "Precision " << argv[m] << " is not a number\n";
133  return 1;
134  }
135  } else if (arg == "-E")
136  exact = true;
137  else if (arg == "--input-string") {
138  if (++m == argc) return usage(1, true);
139  istring = argv[m];
140  } else if (arg == "--input-file") {
141  if (++m == argc) return usage(1, true);
142  ifile = argv[m];
143  } else if (arg == "--output-file") {
144  if (++m == argc) return usage(1, true);
145  ofile = argv[m];
146  } else if (arg == "--line-separator") {
147  if (++m == argc) return usage(1, true);
148  if (std::string(argv[m]).size() != 1) {
149  std::cerr << "Line separator must be a single character\n";
150  return 1;
151  }
152  lsep = argv[m][0];
153  } else if (arg == "--comment-delimiter") {
154  if (++m == argc) return usage(1, true);
155  cdelim = argv[m];
156  } else if (arg == "--version") {
157  std::cout << argv[0] << ": GeographicLib version "
158  << GEOGRAPHICLIB_VERSION_STRING << "\n";
159  return 0;
160  } else
161  return usage(!(arg == "-h" || arg == "--help"), arg != "--help");
162  }
163 
164  if (!ifile.empty() && !istring.empty()) {
165  std::cerr << "Cannot specify --input-string and --input-file together\n";
166  return 1;
167  }
168  if (ifile == "-") ifile.clear();
169  std::ifstream infile;
170  std::istringstream instring;
171  if (!ifile.empty()) {
172  infile.open(ifile.c_str());
173  if (!infile.is_open()) {
174  std::cerr << "Cannot open " << ifile << " for reading\n";
175  return 1;
176  }
177  } else if (!istring.empty()) {
178  std::string::size_type m = 0;
179  while (true) {
180  m = istring.find(lsep, m);
181  if (m == std::string::npos)
182  break;
183  istring[m] = '\n';
184  }
185  instring.str(istring);
186  }
187  std::istream* input = !ifile.empty() ? &infile :
188  (!istring.empty() ? &instring : &std::cin);
189 
190  std::ofstream outfile;
191  if (ofile == "-") ofile.clear();
192  if (!ofile.empty()) {
193  outfile.open(ofile.c_str());
194  if (!outfile.is_open()) {
195  std::cerr << "Cannot open " << ofile << " for writing\n";
196  return 1;
197  }
198  }
199  std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
200 
201  // GeodesicExact mask values are the same as Geodesic
202  unsigned outmask = Geodesic::LATITUDE | Geodesic::LONGITUDE |
203  Geodesic::AZIMUTH; // basic output quantities
204  outmask |= inverse ? Geodesic::DISTANCE : // distance-related flags
206  // longitude unrolling
207  outmask |= unroll ? Geodesic::LONG_UNROLL : Geodesic::NONE;
208  // full output -- don't use Geodesic::ALL since this includes DISTANCE_IN
209  outmask |= full ? (Geodesic::DISTANCE | Geodesic::REDUCEDLENGTH |
212 
213  const Geodesic geods(a, f);
214  const GeodesicExact geode(a, f);
215  GeodesicLine ls;
217  if (linecalc) {
218  if (exact)
219  le = geode.Line(lat1, lon1, azi1, outmask);
220  else
221  ls = geods.Line(lat1, lon1, azi1, outmask);
222  }
223 
224  // Max precision = 10: 0.1 nm in distance, 10^-15 deg (= 0.11 nm),
225  // 10^-11 sec (= 0.3 nm).
226  prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
227  std::string s;
228  int retval = 0;
229  while (std::getline(*input, s)) {
230  try {
231  std::string eol("\n");
232  if (!cdelim.empty()) {
233  std::string::size_type m = s.find(cdelim);
234  if (m != std::string::npos) {
235  eol = " " + s.substr(m) + "\n";
236  s = s.substr(0, m);
237  }
238  }
239  std::istringstream str(s);
240  if (inverse) {
241  std::string slat1, slon1, slat2, slon2;
242  if (!(str >> slat1 >> slon1 >> slat2 >> slon2))
243  throw GeographicErr("Incomplete input: " + s);
244  std::string strc;
245  if (str >> strc)
246  throw GeographicErr("Extraneous input: " + strc);
247  DMS::DecodeLatLon(slat1, slon1, lat1, lon1);
248  DMS::DecodeLatLon(slat2, slon2, lat2, lon2);
249  a12 = exact ?
250  geode.GenInverse(lat1, lon1, lat2, lon2, outmask,
251  s12, azi1, azi2, m12, M12, M21, S12) :
252  geods.GenInverse(lat1, lon1, lat2, lon2, outmask,
253  s12, azi1, azi2, m12, M12, M21, S12);
254  if (full) {
255  lon2 = Math::AngNormalize(lon2);
256  if (unroll)
257  lon2 = lon1 + Math::AngDiff(Math::AngNormalize(lon1), lon2);
258  else
259  lon1 = Math::AngNormalize(lon1);
260  *output << LatLonString(lat1, lon1, prec, dms, dmssep) << " ";
261  }
262  *output << AzimuthString(azi1, prec, dms, dmssep) << " ";
263  if (full)
264  *output << LatLonString(lat2, lon2, prec, dms, dmssep) << " ";
265  *output << AzimuthString(azi2 + azi2sense, prec, dms, dmssep) << " "
266  << DistanceStrings(s12, a12, full, arcmode, prec, dms);
267  if (full)
268  *output << " " << Utility::str(m12, prec)
269  << " " << Utility::str(M12, prec+7)
270  << " " << Utility::str(M21, prec+7)
271  << " " << Utility::str(S12, std::max(prec-7, 0));
272  *output << eol;
273  } else {
274  if (linecalc) {
275  std::string ss12;
276  if (!(str >> ss12))
277  throw GeographicErr("Incomplete input: " + s);
278  std::string strc;
279  if (str >> strc)
280  throw GeographicErr("Extraneous input: " + strc);
281  s12 = ReadDistance(ss12, arcmode);
282  a12 = exact ?
283  le.GenPosition(arcmode, s12, outmask,
284  lat2, lon2, azi2, s12, m12, M12, M21, S12) :
285  ls.GenPosition(arcmode, s12, outmask,
286  lat2, lon2, azi2, s12, m12, M12, M21, S12);
287  } else {
288  std::string slat1, slon1, sazi1, ss12;
289  if (!(str >> slat1 >> slon1 >> sazi1 >> ss12))
290  throw GeographicErr("Incomplete input: " + s);
291  std::string strc;
292  if (str >> strc)
293  throw GeographicErr("Extraneous input: " + strc);
294  DMS::DecodeLatLon(slat1, slon1, lat1, lon1);
295  azi1 = DMS::DecodeAzimuth(sazi1);
296  s12 = ReadDistance(ss12, arcmode);
297  a12 = exact ?
298  geode.GenDirect(lat1, lon1, azi1, arcmode, s12, outmask,
299  lat2, lon2, azi2, s12, m12, M12, M21, S12) :
300  geods.GenDirect(lat1, lon1, azi1, arcmode, s12, outmask,
301  lat2, lon2, azi2, s12, m12, M12, M21, S12);
302  }
303  if (full)
304  *output
305  << LatLonString(lat1, unroll ? lon1 : Math::AngNormalize(lon1),
306  prec, dms, dmssep) << " "
307  << AzimuthString(azi1, prec, dms, dmssep) << " ";
308  *output << LatLonString(lat2, lon2, prec, dms, dmssep) << " "
309  << AzimuthString(azi2 + azi2sense, prec, dms, dmssep);
310  if (full)
311  *output << " "
312  << DistanceStrings(s12, a12, full, arcmode, prec, dms)
313  << " " << Utility::str(m12, prec)
314  << " " << Utility::str(M12, prec+7)
315  << " " << Utility::str(M21, prec+7)
316  << " " << Utility::str(S12, std::max(prec-7, 0));
317  *output << eol;
318  }
319  }
320  catch (const std::exception& e) {
321  // Write error message cout so output lines match input lines
322  *output << "ERROR: " << e.what() << "\n";
323  retval = 1;
324  }
325  }
326  return retval;
327  }
328  catch (const std::exception& e) {
329  std::cerr << "Caught exception: " << e.what() << "\n";
330  return 1;
331  }
332  catch (...) {
333  std::cerr << "Caught unknown exception\n";
334  return 1;
335  }
336 }
static T AngNormalize(T x)
Definition: Math.hpp:445
Header for GeographicLib::GeodesicLine class.
static Math::real DecodeAngle(const std::string &angstr)
Definition: DMS.cpp:278
GeographicLib::Math::real real
Definition: GeodSolve.cpp:32
Header for GeographicLib::Utility class.
real ReadDistance(const std::string &s, bool arcmode)
Definition: GeodSolve.cpp:62
Math::real GenInverse(real lat1, real lon1, real lat2, real lon2, unsigned outmask, real &s12, real &azi1, real &azi2, real &m12, real &M12, real &M21, real &S12) const
static int extra_digits()
Definition: Math.hpp:185
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:298
static Math::real DecodeAzimuth(const std::string &azistr)
Definition: DMS.cpp:287
static void DecodeLatLon(const std::string &dmsa, const std::string &dmsb, real &lat, real &lon, bool swaplatlong=false)
Definition: DMS.cpp:246
GeodesicLineExact Line(real lat1, real lon1, real azi1, unsigned caps=ALL) const
Header for GeographicLib::GeodesicLineExact class.
static std::string str(T x, int p=-1)
Definition: Utility.hpp:276
static T AngDiff(T x, T y)
Definition: Math.hpp:475
std::string LatLonString(real lat, real lon, int prec, bool dms, char dmssep)
Definition: GeodSolve.cpp:34
static int set_digits(int ndigits=0)
Definition: Utility.cpp:48
Math::real GenDirect(real lat1, real lon1, real azi1, bool arcmode, real s12_a12, unsigned outmask, real &lat2, real &lon2, real &azi2, real &s12, real &m12, real &M12, real &M21, real &S12) const
Exact geodesic calculations.
Header for GeographicLib::GeodesicExact class.
Exception handling for GeographicLib.
Definition: Constants.hpp:382
std::string AzimuthString(real azi, int prec, bool dms, char dmssep)
Definition: GeodSolve.cpp:43
int main(int argc, char *argv[])
Definition: GeodSolve.cpp:67
std::string DistanceStrings(real s12, real a12, bool full, bool arcmode, int prec, bool dms)
Definition: GeodSolve.cpp:49
Geodesic calculations
Definition: Geodesic.hpp:171
Header for GeographicLib::DMS class.