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


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:35:34