ConicProj.cpp
Go to the documentation of this file.
1 
12 #include <iostream>
13 #include <string>
14 #include <sstream>
15 #include <fstream>
18 #include <GeographicLib/DMS.hpp>
20 
21 #if defined(_MSC_VER)
22 // Squelch warnings about constant conditional expressions and potentially
23 // uninitialized local variables
24 # pragma warning (disable: 4127 4701)
25 #endif
26 
27 #include "ConicProj.usage"
28 
29 int main(int argc, const char* const argv[]) {
30  try {
31  using namespace GeographicLib;
32  typedef Math::real real;
34  bool lcc = false, albers = false, reverse = false, longfirst = false;
35  real lat1 = 0, lat2 = 0, lon0 = 0, k1 = 1;
36  real
39  int prec = 6;
40  std::string istring, ifile, ofile, cdelim;
41  char lsep = ';';
42 
43  for (int m = 1; m < argc; ++m) {
44  std::string arg(argv[m]);
45  if (arg == "-r")
46  reverse = true;
47  else if (arg == "-c" || arg == "-a") {
48  lcc = arg == "-c";
49  albers = arg == "-a";
50  if (m + 2 >= argc) return usage(1, true);
51  try {
52  for (int i = 0; i < 2; ++i) {
53  DMS::flag ind;
54  (i ? lat2 : lat1) = DMS::Decode(std::string(argv[++m]), ind);
55  if (ind == DMS::LONGITUDE)
56  throw GeographicErr("Bad hemisphere");
57  }
58  }
59  catch (const std::exception& e) {
60  std::cerr << "Error decoding arguments of " << arg << ": "
61  << e.what() << "\n";
62  return 1;
63  }
64  } else if (arg == "-l") {
65  if (++m == argc) return usage(1, true);
66  try {
67  DMS::flag ind;
68  lon0 = DMS::Decode(std::string(argv[m]), ind);
69  if (ind == DMS::LATITUDE)
70  throw GeographicErr("Bad hemisphere");
72  }
73  catch (const std::exception& e) {
74  std::cerr << "Error decoding argument of " << arg << ": "
75  << e.what() << "\n";
76  return 1;
77  }
78  } else if (arg == "-k") {
79  if (++m == argc) return usage(1, true);
80  try {
81  k1 = Utility::val<real>(std::string(argv[m]));
82  }
83  catch (const std::exception& e) {
84  std::cerr << "Error decoding argument of " << arg << ": "
85  << e.what() << "\n";
86  return 1;
87  }
88  } else if (arg == "-e") {
89  if (m + 2 >= argc) return usage(1, true);
90  try {
91  a = Utility::val<real>(std::string(argv[m + 1]));
92  f = Utility::fract<real>(std::string(argv[m + 2]));
93  }
94  catch (const std::exception& e) {
95  std::cerr << "Error decoding arguments of -e: " << e.what() << "\n";
96  return 1;
97  }
98  m += 2;
99  } else if (arg == "-w")
100  longfirst = !longfirst;
101  else if (arg == "-p") {
102  if (++m == argc) return usage(1, true);
103  try {
104  prec = Utility::val<int>(std::string(argv[m]));
105  }
106  catch (const std::exception&) {
107  std::cerr << "Precision " << argv[m] << " is not a number\n";
108  return 1;
109  }
110  } else if (arg == "--input-string") {
111  if (++m == argc) return usage(1, true);
112  istring = argv[m];
113  } else if (arg == "--input-file") {
114  if (++m == argc) return usage(1, true);
115  ifile = argv[m];
116  } else if (arg == "--output-file") {
117  if (++m == argc) return usage(1, true);
118  ofile = argv[m];
119  } else if (arg == "--line-separator") {
120  if (++m == argc) return usage(1, true);
121  if (std::string(argv[m]).size() != 1) {
122  std::cerr << "Line separator must be a single character\n";
123  return 1;
124  }
125  lsep = argv[m][0];
126  } else if (arg == "--comment-delimiter") {
127  if (++m == argc) return usage(1, true);
128  cdelim = argv[m];
129  } else if (arg == "--version") {
130  std::cout << argv[0] << ": GeographicLib version "
131  << GEOGRAPHICLIB_VERSION_STRING << "\n";
132  return 0;
133  } else
134  return usage(!(arg == "-h" || arg == "--help"), arg != "--help");
135  }
136 
137  if (!ifile.empty() && !istring.empty()) {
138  std::cerr << "Cannot specify --input-string and --input-file together\n";
139  return 1;
140  }
141  if (ifile == "-") ifile.clear();
142  std::ifstream infile;
143  std::istringstream instring;
144  if (!ifile.empty()) {
145  infile.open(ifile.c_str());
146  if (!infile.is_open()) {
147  std::cerr << "Cannot open " << ifile << " for reading\n";
148  return 1;
149  }
150  } else if (!istring.empty()) {
151  std::string::size_type m = 0;
152  while (true) {
153  m = istring.find(lsep, m);
154  if (m == std::string::npos)
155  break;
156  istring[m] = '\n';
157  }
158  instring.str(istring);
159  }
160  std::istream* input = !ifile.empty() ? &infile :
161  (!istring.empty() ? &instring : &std::cin);
162 
163  std::ofstream outfile;
164  if (ofile == "-") ofile.clear();
165  if (!ofile.empty()) {
166  outfile.open(ofile.c_str());
167  if (!outfile.is_open()) {
168  std::cerr << "Cannot open " << ofile << " for writing\n";
169  return 1;
170  }
171  }
172  std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
173 
174  if (!(lcc || albers)) {
175  std::cerr << "Must specify \"-c lat1 lat2\" or "
176  << "\"-a lat1 lat2\"\n";
177  return 1;
178  }
179 
180  const LambertConformalConic lproj =
181  lcc ? LambertConformalConic(a, f, lat1, lat2, k1)
182  : LambertConformalConic(1, 0, 0, 0, 1);
183  const AlbersEqualArea aproj =
184  albers ? AlbersEqualArea(a, f, lat1, lat2, k1)
185  : AlbersEqualArea(1, 0, 0, 0, 1);
186 
187  // Max precision = 10: 0.1 nm in distance, 10^-15 deg (= 0.11 nm),
188  // 10^-11 sec (= 0.3 nm).
189  prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
190  std::string s, eol, stra, strb, strc;
191  std::istringstream str;
192  int retval = 0;
193  while (std::getline(*input, s)) {
194  try {
195  eol = "\n";
196  if (!cdelim.empty()) {
197  std::string::size_type m = s.find(cdelim);
198  if (m != std::string::npos) {
199  eol = " " + s.substr(m) + "\n";
200  s = s.substr(0, m);
201  }
202  }
203  str.clear(); str.str(s);
204  real lat, lon, x, y, gamma, k;
205  if (!(str >> stra >> strb))
206  throw GeographicErr("Incomplete input: " + s);
207  if (reverse) {
208  x = Utility::val<real>(stra);
209  y = Utility::val<real>(strb);
210  } else
211  DMS::DecodeLatLon(stra, strb, lat, lon, longfirst);
212  if (str >> strc)
213  throw GeographicErr("Extraneous input: " + strc);
214  if (reverse) {
215  if (lcc)
216  lproj.Reverse(lon0, x, y, lat, lon, gamma, k);
217  else
218  aproj.Reverse(lon0, x, y, lat, lon, gamma, k);
219  *output << Utility::str(longfirst ? lon : lat, prec + 5) << " "
220  << Utility::str(longfirst ? lat : lon, prec + 5) << " "
221  << Utility::str(gamma, prec + 6) << " "
222  << Utility::str(k, prec + 6) << eol;
223  } else {
224  if (lcc)
225  lproj.Forward(lon0, lat, lon, x, y, gamma, k);
226  else
227  aproj.Forward(lon0, lat, lon, x, y, gamma, k);
228  *output << Utility::str(x, prec) << " "
229  << Utility::str(y, prec) << " "
230  << Utility::str(gamma, prec + 6) << " "
231  << Utility::str(k, prec + 6) << eol;
232  }
233  }
234  catch (const std::exception& e) {
235  *output << "ERROR: " << e.what() << "\n";
236  retval = 1;
237  }
238  }
239  return retval;
240  }
241  catch (const std::exception& e) {
242  std::cerr << "Caught exception: " << e.what() << "\n";
243  return 1;
244  }
245  catch (...) {
246  std::cerr << "Caught unknown exception\n";
247  return 1;
248  }
249 }
ind
std::vector< int > ind
Definition: Slicing_stdvector_cxx11.cpp:1
GeographicLib::Math::AngNormalize
static T AngNormalize(T x)
Definition: Math.hpp:440
GeographicLib::AlbersEqualArea
Albers equal area conic projection.
Definition: AlbersEqualArea.hpp:60
GeographicLib::Math::extra_digits
static int extra_digits()
Definition: Math.hpp:187
GeographicLib::LambertConformalConic::Reverse
void Reverse(real lon0, real x, real y, real &lat, real &lon, real &gamma, real &k) const
Definition: src/LambertConformalConic.cpp:373
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
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::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
GeographicLib::AlbersEqualArea::Reverse
void Reverse(real lon0, real x, real y, real &lat, real &lon, real &gamma, real &k) const
Definition: src/AlbersEqualArea.cpp:411
AlbersEqualArea.hpp
Header for GeographicLib::AlbersEqualArea class.
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
LambertConformalConic.hpp
Header for GeographicLib::LambertConformalConic class.
GeographicLib::Math::real
double real
Definition: Math.hpp:129
example::lon0
const double lon0
Definition: testGPSFactor.cpp:41
k1
double k1(double x)
Definition: k1.c:133
arg
Definition: cast.h:1412
Utility.hpp
Header for GeographicLib::Utility class.
GeographicLib::DMS::flag
flag
Definition: DMS.hpp:41
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
gamma
#define gamma
Definition: mconf.h:85
str::str
str(const char *c, const SzType &n)
Definition: pytypes.h:1563
GeographicLib::DMS::LATITUDE
@ LATITUDE
Definition: DMS.hpp:51
y
Scalar * y
Definition: level1_cplx_impl.h:124
str
Definition: pytypes.h:1558
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
GeographicLib::LambertConformalConic::Forward
void Forward(real lon0, real lat, real lon, real &x, real &y, real &gamma, real &k) const
Definition: src/LambertConformalConic.cpp:331
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
GeographicLib::Constants::WGS84_a
static T WGS84_a()
Definition: Constants.hpp:159
GeographicLib::LambertConformalConic
Lambert conformal conic projection.
Definition: LambertConformalConic.hpp:63
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
GeographicLib::AlbersEqualArea::Forward
void Forward(real lon0, real lat, real lon, real &x, real &y, real &gamma, real &k) const
Definition: src/AlbersEqualArea.cpp:386
reverse
void reverse(const MatrixType &m)
Definition: array_reverse.cpp:16
lon
static const double lon
Definition: testGeographicLib.cpp:34
main
int main(int argc, const char *const argv[])
Definition: ConicProj.cpp:29
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
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
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:02:01