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