GeographicLib  1.40
 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-2012) <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;
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  }
115  else if (arg == "-d") {
116  dms = true;
117  dmssep = '\0';
118  } else if (arg == "-:") {
119  dms = true;
120  dmssep = ':';
121  } else if (arg == "-b")
122  azi2sense = 180;
123  else if (arg == "-f")
124  full = true;
125  else if (arg == "-p") {
126  if (++m == argc) return usage(1, true);
127  try {
128  prec = Utility::num<int>(std::string(argv[m]));
129  }
130  catch (const std::exception&) {
131  std::cerr << "Precision " << argv[m] << " is not a number\n";
132  return 1;
133  }
134  } else if (arg == "-E")
135  exact = true;
136  else if (arg == "--input-string") {
137  if (++m == argc) return usage(1, true);
138  istring = argv[m];
139  } else if (arg == "--input-file") {
140  if (++m == argc) return usage(1, true);
141  ifile = argv[m];
142  } else if (arg == "--output-file") {
143  if (++m == argc) return usage(1, true);
144  ofile = argv[m];
145  } else if (arg == "--line-separator") {
146  if (++m == argc) return usage(1, true);
147  if (std::string(argv[m]).size() != 1) {
148  std::cerr << "Line separator must be a single character\n";
149  return 1;
150  }
151  lsep = argv[m][0];
152  } else if (arg == "--comment-delimiter") {
153  if (++m == argc) return usage(1, true);
154  cdelim = argv[m];
155  } else if (arg == "--version") {
156  std::cout
157  << 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  const Geodesic geod(a, f);
202  const GeodesicExact geode(a, f);
203  GeodesicLine l;
205  if (linecalc) {
206  if (exact)
207  le = geode.Line(lat1, lon1, azi1);
208  else
209  l = geod.Line(lat1, lon1, azi1);
210  }
211 
212  // Max precision = 10: 0.1 nm in distance, 10^-15 deg (= 0.11 nm),
213  // 10^-11 sec (= 0.3 nm).
214  prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
215  std::string s;
216  int retval = 0;
217  while (std::getline(*input, s)) {
218  try {
219  std::string eol("\n");
220  if (!cdelim.empty()) {
221  std::string::size_type m = s.find(cdelim);
222  if (m != std::string::npos) {
223  eol = " " + s.substr(m) + "\n";
224  s = s.substr(0, m);
225  }
226  }
227  std::istringstream str(s);
228  if (inverse) {
229  std::string slat1, slon1, slat2, slon2;
230  if (!(str >> slat1 >> slon1 >> slat2 >> slon2))
231  throw GeographicErr("Incomplete input: " + s);
232  std::string strc;
233  if (str >> strc)
234  throw GeographicErr("Extraneous input: " + strc);
235  DMS::DecodeLatLon(slat1, slon1, lat1, lon1);
236  DMS::DecodeLatLon(slat2, slon2, lat2, lon2);
237  a12 = exact ?
238  geode.Inverse(lat1, lon1, lat2, lon2, s12, azi1, azi2,
239  m12, M12, M21, S12) :
240  geod.Inverse(lat1, lon1, lat2, lon2, s12, azi1, azi2,
241  m12, M12, M21, S12);
242  if (full)
243  *output << LatLonString(lat1, lon1, prec, dms, dmssep) << " ";
244  *output << AzimuthString(azi1, prec, dms, dmssep) << " ";
245  if (full)
246  *output << LatLonString(lat2, lon2, prec, dms, dmssep) << " ";
247  *output << AzimuthString(azi2 + azi2sense, prec, dms, dmssep) << " "
248  << DistanceStrings(s12, a12, full, arcmode, prec, dms);
249  if (full)
250  *output << " " << Utility::str(m12, prec)
251  << " " << Utility::str(M12, prec+7)
252  << " " << Utility::str(M21, prec+7)
253  << " " << Utility::str(S12, std::max(prec-7, 0));
254  *output << eol;
255  } else {
256  if (linecalc) {
257  std::string ss12;
258  if (!(str >> ss12))
259  throw GeographicErr("Incomplete input: " + s);
260  std::string strc;
261  if (str >> strc)
262  throw GeographicErr("Extraneous input: " + strc);
263  s12 = ReadDistance(ss12, arcmode);
264  if (arcmode)
265  exact ?
266  le.ArcPosition(s12, lat2, lon2, azi2, a12, m12, M12, M21, S12) :
267  l.ArcPosition(s12, lat2, lon2, azi2, a12, m12, M12, M21, S12);
268  else
269  a12 = exact ?
270  le.Position(s12, lat2, lon2, azi2, m12, M12, M21, S12) :
271  l.Position(s12, lat2, lon2, azi2, m12, M12, M21, S12);
272  } else {
273  std::string slat1, slon1, sazi1, ss12;
274  if (!(str >> slat1 >> slon1 >> sazi1 >> ss12))
275  throw GeographicErr("Incomplete input: " + s);
276  std::string strc;
277  if (str >> strc)
278  throw GeographicErr("Extraneous input: " + strc);
279  DMS::DecodeLatLon(slat1, slon1, lat1, lon1);
280  azi1 = DMS::DecodeAzimuth(sazi1);
281  s12 = ReadDistance(ss12, arcmode);
282  if (arcmode)
283  exact ?
284  geode.ArcDirect(lat1, lon1, azi1, s12, lat2, lon2, azi2, a12,
285  m12, M12, M21, S12) :
286  geod.ArcDirect(lat1, lon1, azi1, s12, lat2, lon2, azi2, a12,
287  m12, M12, M21, S12);
288  else
289  a12 = exact ?
290  geode.Direct(lat1, lon1, azi1, s12, lat2, lon2, azi2,
291  m12, M12, M21, S12) :
292  geod.Direct(lat1, lon1, azi1, s12, lat2, lon2, azi2,
293  m12, M12, M21, S12);
294  }
295  if (arcmode)
296  std::swap(s12, a12);
297  if (full)
298  *output << LatLonString(lat1, lon1, prec, dms, dmssep) << " "
299  << AzimuthString(azi1, prec, dms, dmssep) << " ";
300  *output << LatLonString(lat2, lon2, prec, dms, dmssep) << " "
301  << AzimuthString(azi2 + azi2sense, prec, dms, dmssep);
302  if (full)
303  *output << " "
304  << DistanceStrings(s12, a12, full, arcmode, prec, dms)
305  << " " << Utility::str(m12, prec)
306  << " " << Utility::str(M12, prec+7)
307  << " " << Utility::str(M21, prec+7)
308  << " " << Utility::str(S12, std::max(prec-7, 0));
309  *output << eol;
310  }
311  }
312  catch (const std::exception& e) {
313  // Write error message cout so output lines match input lines
314  *output << "ERROR: " << e.what() << "\n";
315  retval = 1;
316  }
317  }
318  return retval;
319  }
320  catch (const std::exception& e) {
321  std::cerr << "Caught exception: " << e.what() << "\n";
322  return 1;
323  }
324  catch (...) {
325  std::cerr << "Caught unknown exception\n";
326  return 1;
327  }
328 }
void ArcDirect(real lat1, real lon1, real azi1, real a12, real &lat2, real &lon2, real &azi2, real &s12, real &m12, real &M12, real &M21, real &S12) const
Header for GeographicLib::GeodesicLine class.
static Math::real DecodeAngle(const std::string &angstr)
Definition: DMS.cpp:246
Math::real Position(real s12, real &lat2, real &lon2, real &azi2, real &m12, real &M12, real &M21, real &S12) const
Math::real Direct(real lat1, real lon1, real azi1, real s12, real &lat2, real &lon2, real &azi2, real &m12, real &M12, real &M21, real &S12) const
Definition: Geodesic.hpp:393
GeodesicLine Line(real lat1, real lon1, real azi1, unsigned caps=ALL) const
Definition: Geodesic.cpp:119
GeographicLib::Math::real real
Definition: GeodSolve.cpp:32
Header for GeographicLib::Utility class.
Math::real Inverse(real lat1, real lon1, real lat2, real lon2, real &s12, real &azi1, real &azi2, real &m12, real &M12, real &M21, real &S12) const
Definition: Geodesic.hpp:692
Math::real Position(real s12, real &lat2, real &lon2, real &azi2, real &m12, real &M12, real &M21, real &S12) const
Math::real Direct(real lat1, real lon1, real azi1, real s12, real &lat2, real &lon2, real &azi2, real &m12, real &M12, real &M21, real &S12) const
real ReadDistance(const std::string &s, bool arcmode)
Definition: GeodSolve.cpp:62
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:266
static Math::real DecodeAzimuth(const std::string &azistr)
Definition: DMS.cpp:255
static void DecodeLatLon(const std::string &dmsa, const std::string &dmsb, real &lat, real &lon, bool swaplatlong=false)
Definition: DMS.cpp:214
GeodesicLineExact Line(real lat1, real lon1, real azi1, unsigned caps=ALL) const
Header for GeographicLib::GeodesicLineExact class.
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
static std::string str(T x, int p=-1)
Definition: Utility.hpp:276
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
Exact geodesic calculations.
Header for GeographicLib::GeodesicExact class.
void ArcDirect(real lat1, real lon1, real azi1, real a12, real &lat2, real &lon2, real &azi2, real &s12, real &m12, real &M12, real &M21, real &S12) const
Definition: Geodesic.hpp:506
Exception handling for GeographicLib.
Definition: Constants.hpp:361
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
void ArcPosition(real a12, real &lat2, real &lon2, real &azi2, real &s12, real &m12, real &M12, real &M21, real &S12) const
Math::real Inverse(real lat1, real lon1, real lat2, real lon2, real &s12, real &azi1, real &azi2, real &m12, real &M12, real &M21, real &S12) const
std::string DistanceStrings(real s12, real a12, bool full, bool arcmode, int prec, bool dms)
Definition: GeodSolve.cpp:49
void ArcPosition(real a12, real &lat2, real &lon2, real &azi2, real &s12, real &m12, real &M12, real &M21, real &S12) const
Geodesic calculations
Definition: Geodesic.hpp:171
Header for GeographicLib::DMS class.