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 }
GeographicLib::Math::extra_digits
static int extra_digits()
Definition: Math.hpp:187
GeographicLib::Math::NaN
static T NaN()
Definition: Math.hpp:830
inverse
const EIGEN_DEVICE_FUNC InverseReturnType inverse() const
Definition: ArrayCwiseUnaryOps.h:411
s
RealScalar s
Definition: level1_cplx_impl.h:126
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
GeographicLib::Utility::str
static std::string str(T x, int p=-1)
Definition: Utility.hpp:276
GeographicLib
Namespace for GeographicLib.
Definition: JacobiConformal.hpp:15
GeographicLib::DMS::DecodeLatLon
static void DecodeLatLon(const std::string &dmsa, const std::string &dmsb, real &lat, real &lon, bool longfirst=false)
Definition: src/DMS.cpp:252
GeographicLib::Rhumb
Solve of the direct and inverse rhumb problems.
Definition: Rhumb.hpp:66
Rhumb.hpp
Header for GeographicLib::Rhumb and GeographicLib::RhumbLine classes.
GeographicLib::Rhumb::Inverse
void Inverse(real lat1, real lon1, real lat2, real lon2, real &s12, real &azi12, real &S12) const
Definition: Rhumb.hpp:338
GeographicLib::Rhumb::Direct
void Direct(real lat1, real lon1, real azi12, real s12, real &lat2, real &lon2, real &S12) const
Definition: Rhumb.hpp:271
GeographicLib::GeographicErr
Exception handling for GeographicLib.
Definition: Constants.hpp:389
real
Math::real real
Definition: RhumbSolve.cpp:31
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
GeographicLib::Math::real
double real
Definition: Math.hpp:129
GeographicLib::DMS::NUMBER
@ NUMBER
Definition: DMS.hpp:67
GeographicLib::DMS::Encode
static std::string Encode(real angle, component trailing, unsigned prec, flag ind=NONE, char dmssep=char(0))
Definition: src/DMS.cpp:299
arg
Definition: cast.h:1412
Utility.hpp
Header for GeographicLib::Utility class.
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
GeographicLib::DMS::LONGITUDE
@ LONGITUDE
Definition: DMS.hpp:56
arg
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE ArgReturnType arg() const
Definition: ArrayCwiseUnaryOps.h:66
str::str
str(const char *c, const SzType &n)
Definition: pytypes.h:1563
LatLonString
std::string LatLonString(real lat, real lon, int prec, bool dms, char dmssep, bool longfirst)
Definition: RhumbSolve.cpp:33
GeographicLib::DMS::LATITUDE
@ LATITUDE
Definition: DMS.hpp:51
GeographicLib::RhumbLine
Find a sequence of points on a single rhumb line.
Definition: Rhumb.hpp:437
str
Definition: pytypes.h:1558
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
main
int main(int argc, const char *const argv[])
Definition: RhumbSolve.cpp:50
GeographicLib::Constants::WGS84_a
static T WGS84_a()
Definition: Constants.hpp:159
AzimuthString
std::string AzimuthString(real azi, int prec, bool dms, char dmssep)
Definition: RhumbSolve.cpp:45
GEOGRAPHICLIB_VERSION_STRING
#define GEOGRAPHICLIB_VERSION_STRING
Definition: Config.h:3
GeographicLib::Rhumb::Line
RhumbLine Line(real lat1, real lon1, real azi12) const
Definition: src/Rhumb.cpp:168
min
#define min(a, b)
Definition: datatypes.h:19
GeographicLib::Constants::WGS84_f
static T WGS84_f()
Definition: Constants.hpp:169
GeographicLib::RhumbLine::Position
void Position(real s12, real &lat2, real &lon2, real &S12) const
Definition: Rhumb.hpp:512
lon
static const double lon
Definition: testGeographicLib.cpp:34
GeographicLib::DMS::AZIMUTH
@ AZIMUTH
Definition: DMS.hpp:62
GeographicLib::DMS::DecodeAzimuth
static Math::real DecodeAzimuth(const std::string &azistr)
Definition: src/DMS.cpp:290
max
#define max(a, b)
Definition: datatypes.h:20
gtsam.examples.ShonanAveragingCLI.str
str
Definition: ShonanAveragingCLI.py:115
real
Definition: main.h:100
GeographicLib::Utility::set_digits
static int set_digits(int ndigits=0)
Definition: Utility.cpp:48
DMS.hpp
Header for GeographicLib::DMS class.
lat
static const double lat
Definition: testGeographicLib.cpp:34


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:04:01