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