GeodesicProj.cpp
Go to the documentation of this file.
1 
12 #include <iostream>
13 #include <string>
14 #include <sstream>
15 #include <fstream>
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 "GeodesicProj.usage"
30 
31 int main(int argc, const char* const argv[]) {
32  try {
33  using namespace GeographicLib;
34  typedef Math::real real;
36  bool azimuthal = false, cassini = false, gnomonic = false, reverse = false,
37  longfirst = false;
38  real lat0 = 0, lon0 = 0;
39  real
42  int prec = 6;
43  std::string istring, ifile, ofile, cdelim;
44  char lsep = ';';
45 
46  for (int m = 1; m < argc; ++m) {
47  std::string arg(argv[m]);
48  if (arg == "-r")
49  reverse = true;
50  else if (arg == "-c" || arg == "-z" || arg == "-g") {
51  cassini = azimuthal = gnomonic = false;
52  cassini = arg == "-c";
53  azimuthal = arg == "-z";
54  gnomonic = arg == "-g";
55  if (m + 2 >= argc) return usage(1, true);
56  try {
57  DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),
58  lat0, lon0, longfirst);
59  }
60  catch (const std::exception& e) {
61  std::cerr << "Error decoding arguments of " << arg << ": "
62  << e.what() << "\n";
63  return 1;
64  }
65  m += 2;
66  } else if (arg == "-e") {
67  if (m + 2 >= argc) return usage(1, true);
68  try {
69  a = Utility::val<real>(std::string(argv[m + 1]));
70  f = Utility::fract<real>(std::string(argv[m + 2]));
71  }
72  catch (const std::exception& e) {
73  std::cerr << "Error decoding arguments of -e: " << e.what() << "\n";
74  return 1;
75  }
76  m += 2;
77  } else if (arg == "-w")
78  longfirst = !longfirst;
79  else if (arg == "-p") {
80  if (++m == argc) return usage(1, true);
81  try {
82  prec = Utility::val<int>(std::string(argv[m]));
83  }
84  catch (const std::exception&) {
85  std::cerr << "Precision " << argv[m] << " is not a number\n";
86  return 1;
87  }
88  } else if (arg == "--input-string") {
89  if (++m == argc) return usage(1, true);
90  istring = argv[m];
91  } else if (arg == "--input-file") {
92  if (++m == argc) return usage(1, true);
93  ifile = argv[m];
94  } else if (arg == "--output-file") {
95  if (++m == argc) return usage(1, true);
96  ofile = argv[m];
97  } else if (arg == "--line-separator") {
98  if (++m == argc) return usage(1, true);
99  if (std::string(argv[m]).size() != 1) {
100  std::cerr << "Line separator must be a single character\n";
101  return 1;
102  }
103  lsep = argv[m][0];
104  } else if (arg == "--comment-delimiter") {
105  if (++m == argc) return usage(1, true);
106  cdelim = argv[m];
107  } else if (arg == "--version") {
108  std::cout << argv[0] << ": GeographicLib version "
109  << GEOGRAPHICLIB_VERSION_STRING << "\n";
110  return 0;
111  } else
112  return usage(!(arg == "-h" || arg == "--help"), arg != "--help");
113  }
114 
115  if (!ifile.empty() && !istring.empty()) {
116  std::cerr << "Cannot specify --input-string and --input-file together\n";
117  return 1;
118  }
119  if (ifile == "-") ifile.clear();
120  std::ifstream infile;
121  std::istringstream instring;
122  if (!ifile.empty()) {
123  infile.open(ifile.c_str());
124  if (!infile.is_open()) {
125  std::cerr << "Cannot open " << ifile << " for reading\n";
126  return 1;
127  }
128  } else if (!istring.empty()) {
129  std::string::size_type m = 0;
130  while (true) {
131  m = istring.find(lsep, m);
132  if (m == std::string::npos)
133  break;
134  istring[m] = '\n';
135  }
136  instring.str(istring);
137  }
138  std::istream* input = !ifile.empty() ? &infile :
139  (!istring.empty() ? &instring : &std::cin);
140 
141  std::ofstream outfile;
142  if (ofile == "-") ofile.clear();
143  if (!ofile.empty()) {
144  outfile.open(ofile.c_str());
145  if (!outfile.is_open()) {
146  std::cerr << "Cannot open " << ofile << " for writing\n";
147  return 1;
148  }
149  }
150  std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
151 
152  if (!(azimuthal || cassini || gnomonic)) {
153  std::cerr << "Must specify \"-z lat0 lon0\" or "
154  << "\"-c lat0 lon0\" or \"-g lat0 lon0\"\n";
155  return 1;
156  }
157 
158  const Geodesic geod(a, f);
159  const CassiniSoldner cs = cassini ?
160  CassiniSoldner(lat0, lon0, geod) : CassiniSoldner(geod);
161  const AzimuthalEquidistant az(geod);
162  const Gnomonic gn(geod);
163 
164  // Max precision = 10: 0.1 nm in distance, 10^-15 deg (= 0.11 nm),
165  // 10^-11 sec (= 0.3 nm).
166  prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
167  std::string s, eol, stra, strb, strc;
168  std::istringstream str;
169  int retval = 0;
170  std::cout << std::fixed;
171  while (std::getline(*input, s)) {
172  try {
173  eol = "\n";
174  if (!cdelim.empty()) {
175  std::string::size_type m = s.find(cdelim);
176  if (m != std::string::npos) {
177  eol = " " + s.substr(m) + "\n";
178  s = s.substr(0, m);
179  }
180  }
181  str.clear(); str.str(s);
182  real lat, lon, x, y, azi, rk;
183  if (!(str >> stra >> strb))
184  throw GeographicErr("Incomplete input: " + s);
185  if (reverse) {
186  x = Utility::val<real>(stra);
187  y = Utility::val<real>(strb);
188  } else
189  DMS::DecodeLatLon(stra, strb, lat, lon, longfirst);
190  if (str >> strc)
191  throw GeographicErr("Extraneous input: " + strc);
192  if (reverse) {
193  if (cassini)
194  cs.Reverse(x, y, lat, lon, azi, rk);
195  else if (azimuthal)
196  az.Reverse(lat0, lon0, x, y, lat, lon, azi, rk);
197  else
198  gn.Reverse(lat0, lon0, x, y, lat, lon, azi, rk);
199  *output << Utility::str(longfirst ? lon : lat, prec + 5) << " "
200  << Utility::str(longfirst ? lat : lon, prec + 5) << " "
201  << Utility::str(azi, prec + 5) << " "
202  << Utility::str(rk, prec + 6) << eol;
203  } else {
204  if (cassini)
205  cs.Forward(lat, lon, x, y, azi, rk);
206  else if (azimuthal)
207  az.Forward(lat0, lon0, lat, lon, x, y, azi, rk);
208  else
209  gn.Forward(lat0, lon0, lat, lon, x, y, azi, rk);
210  *output << Utility::str(x, prec) << " "
211  << Utility::str(y, prec) << " "
212  << Utility::str(azi, prec + 5) << " "
213  << Utility::str(rk, prec + 6) << eol;
214  }
215  }
216  catch (const std::exception& e) {
217  *output << "ERROR: " << e.what() << "\n";
218  retval = 1;
219  }
220  }
221  return retval;
222  }
223  catch (const std::exception& e) {
224  std::cerr << "Caught exception: " << e.what() << "\n";
225  return 1;
226  }
227  catch (...) {
228  std::cerr << "Caught unknown exception\n";
229  return 1;
230  }
231 }
Matrix3f m
void Forward(real lat, real lon, real &x, real &y, real &azi, real &rk) const
#define max(a, b)
Definition: datatypes.h:20
Header for GeographicLib::CassiniSoldner class.
float real
Definition: datatypes.h:10
Scalar * y
static const double lat
Header for GeographicLib::Utility class.
#define min(a, b)
Definition: datatypes.h:19
Gnomonic projection
Definition: Gnomonic.hpp:102
void Forward(real lat0, real lon0, real lat, real lon, real &x, real &y, real &azi, real &rk) const
Cassini-Soldner projection.
Header for GeographicLib::Gnomonic class.
static int extra_digits()
Definition: Math.hpp:187
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
Header for GeographicLib::Geodesic class.
const double lat0
Azimuthal equidistant projection.
void Forward(real lat0, real lon0, real lat, real lon, real &x, real &y, real &azi, real &rk) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const ArgReturnType arg() const
#define GEOGRAPHICLIB_VERSION_STRING
Definition: Config.h:3
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Namespace for GeographicLib.
void Reverse(real x, real y, real &lat, real &lon, real &azi, real &rk) const
Array< double, 1, 3 > e(1./3., 0.5, 2.)
RealScalar s
void Reverse(real lat0, real lon0, real x, real y, real &lat, real &lon, real &azi, real &rk) const
const double lon0
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 Reverse(real lat0, real lon0, real x, real y, real &lat, real &lon, real &azi, real &rk) const
Exception handling for GeographicLib.
Definition: Constants.hpp:389
int main(int argc, const char *const argv[])
void reverse(const MatrixType &m)
static const double lon
Header for GeographicLib::AzimuthalEquidistant class.
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Geodesic calculations
Definition: Geodesic.hpp:172
Header for GeographicLib::DMS class.


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