GeographicLib  1.44
RhumbSolve.cpp
Go to the documentation of this file.
1 /**
2  * \file RhumbSolve.cpp
3  * \brief Command line utility for rhumb line calculations
4  *
5  * Copyright (c) Charles Karney (2014-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="RhumbSolve.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>
17 #include <cmath>
18 #include <limits>
19 #include <GeographicLib/Rhumb.hpp>
20 #include <GeographicLib/DMS.hpp>
22 
23 #if defined(_MSC_VER)
24 // Squelch warnings about constant conditional expressions and potentially
25 // uninitialized local variables
26 # pragma warning (disable: 4127 4701)
27 #endif
28 
29 #include "RhumbSolve.usage"
30 
31 using namespace GeographicLib;
32 typedef Math::real real;
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  return dms ? DMS::Encode(azi, prec + 5, DMS::AZIMUTH, dmssep) :
48  DMS::Encode(azi, prec + 5, DMS::NUMBER);
49 }
50 
51 int main(int argc, char* argv[]) {
52  try {
54  bool linecalc = false, inverse = false, dms = false, exact = true,
55  longfirst = false;
56  real
57  a = Constants::WGS84_a(),
58  f = Constants::WGS84_f();
59  real lat1, lon1, azi12 = Math::NaN(), lat2, lon2, s12, S12;
60  int prec = 3;
61  std::string istring, ifile, ofile, cdelim;
62  char lsep = ';', dmssep = char(0);
63 
64  for (int m = 1; m < argc; ++m) {
65  std::string arg(argv[m]);
66  if (arg == "-i") {
67  inverse = true;
68  linecalc = false;
69  } else if (arg == "-l") {
70  inverse = false;
71  linecalc = true;
72  if (m + 3 >= argc) return usage(1, true);
73  try {
74  DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),
75  lat1, lon1, longfirst);
76  azi12 = DMS::DecodeAzimuth(std::string(argv[m + 3]));
77  }
78  catch (const std::exception& e) {
79  std::cerr << "Error decoding arguments of -l: " << e.what() << "\n";
80  return 1;
81  }
82  m += 3;
83  } else if (arg == "-e") {
84  if (m + 2 >= argc) return usage(1, true);
85  try {
86  a = Utility::num<real>(std::string(argv[m + 1]));
87  f = Utility::fract<real>(std::string(argv[m + 2]));
88  }
89  catch (const std::exception& e) {
90  std::cerr << "Error decoding arguments of -e: " << e.what() << "\n";
91  return 1;
92  }
93  m += 2;
94  }
95  else if (arg == "-d") {
96  dms = true;
97  dmssep = '\0';
98  } else if (arg == "-:") {
99  dms = true;
100  dmssep = ':';
101  } else if (arg == "-w")
102  longfirst = true;
103  else if (arg == "-p") {
104  if (++m == argc) return usage(1, true);
105  try {
106  prec = Utility::num<int>(std::string(argv[m]));
107  }
108  catch (const std::exception&) {
109  std::cerr << "Precision " << argv[m] << " is not a number\n";
110  return 1;
111  }
112  } else if (arg == "-s")
113  exact = false;
114  else if (arg == "--input-string") {
115  if (++m == argc) return usage(1, true);
116  istring = argv[m];
117  } else if (arg == "--input-file") {
118  if (++m == argc) return usage(1, true);
119  ifile = argv[m];
120  } else if (arg == "--output-file") {
121  if (++m == argc) return usage(1, true);
122  ofile = argv[m];
123  } else if (arg == "--line-separator") {
124  if (++m == argc) return usage(1, true);
125  if (std::string(argv[m]).size() != 1) {
126  std::cerr << "Line separator must be a single character\n";
127  return 1;
128  }
129  lsep = argv[m][0];
130  } else if (arg == "--comment-delimiter") {
131  if (++m == argc) return usage(1, true);
132  cdelim = argv[m];
133  } else if (arg == "--version") {
134  std::cout << argv[0] << ": GeographicLib version "
135  << GEOGRAPHICLIB_VERSION_STRING << "\n";
136  return 0;
137  } else
138  return usage(!(arg == "-h" || arg == "--help"), arg != "--help");
139  }
140 
141  if (!ifile.empty() && !istring.empty()) {
142  std::cerr << "Cannot specify --input-string and --input-file together\n";
143  return 1;
144  }
145  if (ifile == "-") ifile.clear();
146  std::ifstream infile;
147  std::istringstream instring;
148  if (!ifile.empty()) {
149  infile.open(ifile.c_str());
150  if (!infile.is_open()) {
151  std::cerr << "Cannot open " << ifile << " for reading\n";
152  return 1;
153  }
154  } else if (!istring.empty()) {
155  std::string::size_type m = 0;
156  while (true) {
157  m = istring.find(lsep, m);
158  if (m == std::string::npos)
159  break;
160  istring[m] = '\n';
161  }
162  instring.str(istring);
163  }
164  std::istream* input = !ifile.empty() ? &infile :
165  (!istring.empty() ? &instring : &std::cin);
166 
167  std::ofstream outfile;
168  if (ofile == "-") ofile.clear();
169  if (!ofile.empty()) {
170  outfile.open(ofile.c_str());
171  if (!outfile.is_open()) {
172  std::cerr << "Cannot open " << ofile << " for writing\n";
173  return 1;
174  }
175  }
176  std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
177 
178  const Rhumb rh(a, f, exact);
179  const RhumbLine rhl(linecalc ? rh.Line(lat1, lon1, azi12) :
180  rh.Line(0, 0, 90));
181  // Max precision = 10: 0.1 nm in distance, 10^-15 deg (= 0.11 nm),
182  // 10^-11 sec (= 0.3 nm).
183  prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
184  std::string s, eol, slat1, slon1, slat2, slon2, sazi, ss12, strc;
185  std::istringstream str;
186  int retval = 0;
187  while (std::getline(*input, s)) {
188  try {
189  eol = "\n";
190  if (!cdelim.empty()) {
191  std::string::size_type m = s.find(cdelim);
192  if (m != std::string::npos) {
193  eol = " " + s.substr(m) + "\n";
194  s = s.substr(0, m);
195  }
196  }
197  str.clear(); str.str(s);
198  if (linecalc) {
199  if (!(str >> s12))
200  throw GeographicErr("Incomplete input: " + s);
201  if (str >> strc)
202  throw GeographicErr("Extraneous input: " + strc);
203  rhl.Position(s12, lat2, lon2, S12);
204  *output << LatLonString(lat2, lon2, prec, dms, dmssep, longfirst)
205  << " " << Utility::str(S12, std::max(prec-7, 0)) << eol;
206  } else if (inverse) {
207  if (!(str >> slat1 >> slon1 >> slat2 >> slon2))
208  throw GeographicErr("Incomplete input: " + s);
209  if (str >> strc)
210  throw GeographicErr("Extraneous input: " + strc);
211  DMS::DecodeLatLon(slat1, slon1, lat1, lon1, longfirst);
212  DMS::DecodeLatLon(slat2, slon2, lat2, lon2, longfirst);
213  rh.Inverse(lat1, lon1, lat2, lon2, s12, azi12, S12);
214  *output << AzimuthString(azi12, prec, dms, dmssep) << " "
215  << Utility::str(s12, prec) << " "
216  << Utility::str(S12, std::max(prec-7, 0)) << eol;
217  } else { // direct
218  if (!(str >> slat1 >> slon1 >> sazi >> s12))
219  throw GeographicErr("Incomplete input: " + s);
220  if (str >> strc)
221  throw GeographicErr("Extraneous input: " + strc);
222  DMS::DecodeLatLon(slat1, slon1, lat1, lon1, longfirst);
223  azi12 = DMS::DecodeAzimuth(sazi);
224  rh.Direct(lat1, lon1, azi12, s12, lat2, lon2, S12);
225  *output << LatLonString(lat2, lon2, prec, dms, dmssep, longfirst)
226  << " " << Utility::str(S12, std::max(prec-7, 0)) << eol;
227  }
228  }
229  catch (const std::exception& e) {
230  // Write error message cout so output lines match input lines
231  *output << "ERROR: " << e.what() << "\n";
232  retval = 1;
233  }
234  }
235  return retval;
236  }
237  catch (const std::exception& e) {
238  std::cerr << "Caught exception: " << e.what() << "\n";
239  return 1;
240  }
241  catch (...) {
242  std::cerr << "Caught unknown exception\n";
243  return 1;
244  }
245 }
static T NaN()
Definition: Math.hpp:783
void Inverse(real lat1, real lon1, real lat2, real lon2, real &s12, real &azi12, real &S12) const
Definition: Rhumb.hpp:342
int main(int argc, char *argv[])
Definition: RhumbSolve.cpp:51
GeographicLib::Math::real real
Definition: GeodSolve.cpp:32
Header for GeographicLib::Utility class.
std::string LatLonString(real lat, real lon, int prec, bool dms, char dmssep, bool longfirst)
Definition: RhumbSolve.cpp:34
Header for GeographicLib::Rhumb and GeographicLib::RhumbLine classes.
Math::real real
Definition: RhumbSolve.cpp:32
static int extra_digits()
Definition: Math.hpp:187
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
RhumbLine Line(real lat1, real lon1, real azi12) const
Definition: Rhumb.cpp:167
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
static std::string str(T x, int p=-1)
Definition: Utility.hpp:276
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
Exception handling for GeographicLib.
Definition: Constants.hpp:386
std::string AzimuthString(real azi, int prec, bool dms, char dmssep)
Definition: RhumbSolve.cpp:46
Solve of the direct and inverse rhumb problems.
Definition: Rhumb.hpp:66
Find a sequence of points on a single rhumb line.
Definition: Rhumb.hpp:441
void Direct(real lat1, real lon1, real azi12, real s12, real &lat2, real &lon2, real &S12) const
Definition: Rhumb.hpp:275
Header for GeographicLib::DMS class.