TransverseMercatorProj.cpp
Go to the documentation of this file.
1 
13 #include <iostream>
14 #include <string>
15 #include <sstream>
16 #include <fstream>
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 "TransverseMercatorProj.usage"
29 
30 int main(int argc, const char* const argv[]) {
31  try {
32  using namespace GeographicLib;
33  typedef Math::real real;
35  bool exact = true, extended = false, series = false, reverse = false,
36  longfirst = false;
37  real
41  lon0 = 0;
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 == "-t") {
51  exact = true;
52  extended = true;
53  series = false;
54  } else if (arg == "-s") {
55  exact = false;
56  extended = false;
57  series = true;
58  } else if (arg == "-l") {
59  if (++m >= argc) return usage(1, true);
60  try {
61  DMS::flag ind;
62  lon0 = DMS::Decode(std::string(argv[m]), ind);
63  if (ind == DMS::LATITUDE)
64  throw GeographicErr("Bad hemisphere");
66  }
67  catch (const std::exception& e) {
68  std::cerr << "Error decoding argument of " << arg << ": "
69  << e.what() << "\n";
70  return 1;
71  }
72  } else if (arg == "-k") {
73  if (++m >= argc) return usage(1, true);
74  try {
75  k0 = Utility::val<real>(std::string(argv[m]));
76  }
77  catch (const std::exception& e) {
78  std::cerr << "Error decoding argument of " << arg << ": "
79  << e.what() << "\n";
80  return 1;
81  }
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  } else if (arg == "-w")
94  longfirst = !longfirst;
95  else if (arg == "-p") {
96  if (++m == argc) return usage(1, true);
97  try {
98  prec = Utility::val<int>(std::string(argv[m]));
99  }
100  catch (const std::exception&) {
101  std::cerr << "Precision " << argv[m] << " is not a number\n";
102  return 1;
103  }
104  } else if (arg == "--input-string") {
105  if (++m == argc) return usage(1, true);
106  istring = argv[m];
107  } else if (arg == "--input-file") {
108  if (++m == argc) return usage(1, true);
109  ifile = argv[m];
110  } else if (arg == "--output-file") {
111  if (++m == argc) return usage(1, true);
112  ofile = argv[m];
113  } else if (arg == "--line-separator") {
114  if (++m == argc) return usage(1, true);
115  if (std::string(argv[m]).size() != 1) {
116  std::cerr << "Line separator must be a single character\n";
117  return 1;
118  }
119  lsep = argv[m][0];
120  } else if (arg == "--comment-delimiter") {
121  if (++m == argc) return usage(1, true);
122  cdelim = argv[m];
123  } else if (arg == "--version") {
124  std::cout << argv[0] << ": GeographicLib version "
125  << GEOGRAPHICLIB_VERSION_STRING << "\n";
126  return 0;
127  } else
128  return usage(!(arg == "-h" || arg == "--help"), arg != "--help");
129  }
130 
131  if (!ifile.empty() && !istring.empty()) {
132  std::cerr << "Cannot specify --input-string and --input-file together\n";
133  return 1;
134  }
135  if (ifile == "-") ifile.clear();
136  std::ifstream infile;
137  std::istringstream instring;
138  if (!ifile.empty()) {
139  infile.open(ifile.c_str());
140  if (!infile.is_open()) {
141  std::cerr << "Cannot open " << ifile << " for reading\n";
142  return 1;
143  }
144  } else if (!istring.empty()) {
145  std::string::size_type m = 0;
146  while (true) {
147  m = istring.find(lsep, m);
148  if (m == std::string::npos)
149  break;
150  istring[m] = '\n';
151  }
152  instring.str(istring);
153  }
154  std::istream* input = !ifile.empty() ? &infile :
155  (!istring.empty() ? &instring : &std::cin);
156 
157  std::ofstream outfile;
158  if (ofile == "-") ofile.clear();
159  if (!ofile.empty()) {
160  outfile.open(ofile.c_str());
161  if (!outfile.is_open()) {
162  std::cerr << "Cannot open " << ofile << " for writing\n";
163  return 1;
164  }
165  }
166  std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
167 
168  const TransverseMercator& TMS =
169  series ? TransverseMercator(a, f, k0) : TransverseMercator(1, 0, 1);
170 
171  const TransverseMercatorExact& TME =
172  exact ? TransverseMercatorExact(a, f, k0, extended)
173  : TransverseMercatorExact(1, real(0.1), 1, false);
174 
175  // Max precision = 10: 0.1 nm in distance, 10^-15 deg (= 0.11 nm),
176  // 10^-11 sec (= 0.3 nm).
177  prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
178  std::string s, eol, stra, strb, strc;
179  std::istringstream str;
180  int retval = 0;
181  std::cout << std::fixed;
182  while (std::getline(*input, s)) {
183  try {
184  eol = "\n";
185  if (!cdelim.empty()) {
186  std::string::size_type m = s.find(cdelim);
187  if (m != std::string::npos) {
188  eol = " " + s.substr(m) + "\n";
189  s = s.substr(0, m);
190  }
191  }
192  str.clear(); str.str(s);
193  real lat, lon, x, y;
194  if (!(str >> stra >> strb))
195  throw GeographicErr("Incomplete input: " + s);
196  if (reverse) {
197  x = Utility::val<real>(stra);
198  y = Utility::val<real>(strb);
199  } else
200  DMS::DecodeLatLon(stra, strb, lat, lon, longfirst);
201  if (str >> strc)
202  throw GeographicErr("Extraneous input: " + strc);
203  real gamma, k;
204  if (reverse) {
205  if (series)
206  TMS.Reverse(lon0, x, y, lat, lon, gamma, k);
207  else
208  TME.Reverse(lon0, x, y, lat, lon, gamma, k);
209  *output << Utility::str(longfirst ? lon : lat, prec + 5) << " "
210  << Utility::str(longfirst ? lat : lon, prec + 5) << " "
211  << Utility::str(gamma, prec + 6) << " "
212  << Utility::str(k, prec + 6) << eol;
213  } else {
214  if (series)
215  TMS.Forward(lon0, lat, lon, x, y, gamma, k);
216  else
217  TME.Forward(lon0, lat, lon, x, y, gamma, k);
218  *output << Utility::str(x, prec) << " "
219  << Utility::str(y, prec) << " "
220  << Utility::str(gamma, prec + 6) << " "
221  << Utility::str(k, prec + 6) << eol;
222  }
223  }
224  catch (const std::exception& e) {
225  *output << "ERROR: " << e.what() << "\n";
226  retval = 1;
227  }
228  }
229  return retval;
230  }
231  catch (const std::exception& e) {
232  std::cerr << "Caught exception: " << e.what() << "\n";
233  return 1;
234  }
235  catch (...) {
236  std::cerr << "Caught unknown exception\n";
237  return 1;
238  }
239 }
ind
std::vector< int > ind
Definition: Slicing_stdvector_cxx11.cpp:1
GeographicLib::Math::AngNormalize
static T AngNormalize(T x)
Definition: Math.hpp:440
GeographicLib::Math::extra_digits
static int extra_digits()
Definition: Math.hpp:187
s
RealScalar s
Definition: level1_cplx_impl.h:126
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
GeographicLib::TransverseMercatorExact
An exact implementation of the transverse Mercator projection.
Definition: TransverseMercatorExact.hpp:83
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
x
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
Definition: gnuplot_common_settings.hh:12
real
float real
Definition: datatypes.h:10
GeographicLib::TransverseMercatorExact::Reverse
void Reverse(real lon0, real x, real y, real &lat, real &lon, real &gamma, real &k) const
Definition: src/TransverseMercatorExact.cpp:408
GeographicLib::DMS::Decode
static Math::real Decode(const std::string &dms, flag &ind)
Definition: src/DMS.cpp:28
GeographicLib::GeographicErr
Exception handling for GeographicLib.
Definition: Constants.hpp:389
k0
double k0(double x)
Definition: k0.c:131
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
main
int main(int argc, const char *const argv[])
Definition: TransverseMercatorProj.cpp:30
GeographicLib::Math::real
double real
Definition: Math.hpp:129
TransverseMercator.hpp
Header for GeographicLib::TransverseMercator class.
example::lon0
const double lon0
Definition: testGPSFactor.cpp:41
GeographicLib::TransverseMercator::Forward
void Forward(real lon0, real lat, real lon, real &x, real &y, real &gamma, real &k) const
Definition: src/TransverseMercator.cpp:350
arg
Definition: cast.h:1277
Utility.hpp
Header for GeographicLib::Utility class.
GeographicLib::DMS::flag
flag
Definition: DMS.hpp:41
GeographicLib::Constants::UTM_k0
static T UTM_k0()
Definition: Constants.hpp:255
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
arg
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE ArgReturnType arg() const
Definition: ArrayCwiseUnaryOps.h:66
gamma
#define gamma
Definition: mconf.h:85
str::str
str(const char *c, const SzType &n)
Definition: pytypes.h:1529
GeographicLib::DMS::LATITUDE
@ LATITUDE
Definition: DMS.hpp:51
y
Scalar * y
Definition: level1_cplx_impl.h:124
str
Definition: pytypes.h:1524
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
GeographicLib::Constants::WGS84_a
static T WGS84_a()
Definition: Constants.hpp:159
GeographicLib::TransverseMercator::Reverse
void Reverse(real lon0, real x, real y, real &lat, real &lon, real &gamma, real &k) const
Definition: src/TransverseMercator.cpp:516
GeographicLib::TransverseMercatorExact::Forward
void Forward(real lon0, real lat, real lon, real &x, real &y, real &gamma, real &k) const
Definition: src/TransverseMercatorExact.cpp:348
GEOGRAPHICLIB_VERSION_STRING
#define GEOGRAPHICLIB_VERSION_STRING
Definition: Config.h:3
min
#define min(a, b)
Definition: datatypes.h:19
GeographicLib::Constants::WGS84_f
static T WGS84_f()
Definition: Constants.hpp:169
reverse
void reverse(const MatrixType &m)
Definition: array_reverse.cpp:16
lon
static const double lon
Definition: testGeographicLib.cpp:34
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
TransverseMercatorExact.hpp
Header for GeographicLib::TransverseMercatorExact class.
DMS.hpp
Header for GeographicLib::DMS class.
GeographicLib::TransverseMercator
Transverse Mercator projection.
Definition: TransverseMercator.hpp:93
lat
static const double lat
Definition: testGeographicLib.cpp:34


gtsam
Author(s):
autogenerated on Tue Jun 25 2024 03:07:37