GeographicLib  1.40
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ConicProj.cpp
Go to the documentation of this file.
1 /**
2  * \file ConicProj.cpp
3  * \brief Command line utility for conical projections
4  *
5  * Copyright (c) Charles Karney (2009-2012) <charles@karney.com> and licensed
6  * under the MIT/X11 License. For more information, see
7  * http://geographiclib.sourceforge.net/
8  *
9  * See the <a href="ConicProj.1.html">man page</a> for usage information.
10  **********************************************************************/
11 
12 #include <iostream>
13 #include <sstream>
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 "ConicProj.usage"
29 
30 int main(int argc, char* argv[]) {
31  try {
32  using namespace GeographicLib;
33  typedef Math::real real;
35  bool lcc = false, albers = false, reverse = false;
36  real lat1 = 0, lat2 = 0, lon0 = 0, k1 = 1;
37  real
38  a = Constants::WGS84_a(),
39  f = Constants::WGS84_f();
40  int prec = 6;
41  std::string istring, ifile, ofile, cdelim;
42  char lsep = ';';
43 
44  for (int m = 1; m < argc; ++m) {
45  std::string arg(argv[m]);
46  if (arg == "-r")
47  reverse = true;
48  else if (arg == "-c" || arg == "-a") {
49  lcc = arg == "-c";
50  albers = arg == "-a";
51  if (m + 2 >= argc) return usage(1, true);
52  try {
53  for (int i = 0; i < 2; ++i) {
54  DMS::flag ind;
55  (i ? lat2 : lat1) = DMS::Decode(std::string(argv[++m]), ind);
56  if (ind == DMS::LONGITUDE)
57  throw GeographicErr("Bad hemisphere");
58  }
59  }
60  catch (const std::exception& e) {
61  std::cerr << "Error decoding arguments of " << arg << ": "
62  << e.what() << "\n";
63  return 1;
64  }
65  } else if (arg == "-l") {
66  if (++m == argc) return usage(1, true);
67  try {
68  DMS::flag ind;
69  lon0 = DMS::Decode(std::string(argv[m]), ind);
70  if (ind == DMS::LATITUDE)
71  throw GeographicErr("Bad hemisphere");
72  if (!(lon0 >= -540 && lon0 < 540))
73  throw GeographicErr("Bad longitude");
74  lon0 = Math::AngNormalize(lon0);
75  }
76  catch (const std::exception& e) {
77  std::cerr << "Error decoding argument of " << arg << ": "
78  << e.what() << "\n";
79  return 1;
80  }
81  } else if (arg == "-k") {
82  if (++m == argc) return usage(1, true);
83  try {
84  k1 = Utility::num<real>(std::string(argv[m]));
85  }
86  catch (const std::exception& e) {
87  std::cerr << "Error decoding argument of " << arg << ": "
88  << e.what() << "\n";
89  return 1;
90  }
91  } else if (arg == "-e") {
92  if (m + 2 >= argc) return usage(1, true);
93  try {
94  a = Utility::num<real>(std::string(argv[m + 1]));
95  f = Utility::fract<real>(std::string(argv[m + 2]));
96  }
97  catch (const std::exception& e) {
98  std::cerr << "Error decoding arguments of -e: " << e.what() << "\n";
99  return 1;
100  }
101  m += 2;
102  } else if (arg == "-p") {
103  if (++m == argc) return usage(1, true);
104  try {
105  prec = Utility::num<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 == "--input-string") {
112  if (++m == argc) return usage(1, true);
113  istring = argv[m];
114  } else if (arg == "--input-file") {
115  if (++m == argc) return usage(1, true);
116  ifile = argv[m];
117  } else if (arg == "--output-file") {
118  if (++m == argc) return usage(1, true);
119  ofile = argv[m];
120  } else if (arg == "--line-separator") {
121  if (++m == argc) return usage(1, true);
122  if (std::string(argv[m]).size() != 1) {
123  std::cerr << "Line separator must be a single character\n";
124  return 1;
125  }
126  lsep = argv[m][0];
127  } else if (arg == "--comment-delimiter") {
128  if (++m == argc) return usage(1, true);
129  cdelim = argv[m];
130  } else if (arg == "--version") {
131  std::cout
132  << argv[0] << ": GeographicLib version "
133  << GEOGRAPHICLIB_VERSION_STRING << "\n";
134  return 0;
135  } else
136  return usage(!(arg == "-h" || arg == "--help"), arg != "--help");
137  }
138 
139  if (!ifile.empty() && !istring.empty()) {
140  std::cerr << "Cannot specify --input-string and --input-file together\n";
141  return 1;
142  }
143  if (ifile == "-") ifile.clear();
144  std::ifstream infile;
145  std::istringstream instring;
146  if (!ifile.empty()) {
147  infile.open(ifile.c_str());
148  if (!infile.is_open()) {
149  std::cerr << "Cannot open " << ifile << " for reading\n";
150  return 1;
151  }
152  } else if (!istring.empty()) {
153  std::string::size_type m = 0;
154  while (true) {
155  m = istring.find(lsep, m);
156  if (m == std::string::npos)
157  break;
158  istring[m] = '\n';
159  }
160  instring.str(istring);
161  }
162  std::istream* input = !ifile.empty() ? &infile :
163  (!istring.empty() ? &instring : &std::cin);
164 
165  std::ofstream outfile;
166  if (ofile == "-") ofile.clear();
167  if (!ofile.empty()) {
168  outfile.open(ofile.c_str());
169  if (!outfile.is_open()) {
170  std::cerr << "Cannot open " << ofile << " for writing\n";
171  return 1;
172  }
173  }
174  std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
175 
176  if (!(lcc || albers)) {
177  std::cerr << "Must specify \"-c lat1 lat2\" or "
178  << "\"-a lat1 lat2\"\n";
179  return 1;
180  }
181 
182  const LambertConformalConic lproj =
183  lcc ? LambertConformalConic(a, f, lat1, lat2, k1)
184  : LambertConformalConic(1, 0, 0, 0, 1);
185  const AlbersEqualArea aproj =
186  albers ? AlbersEqualArea(a, f, lat1, lat2, k1)
187  : AlbersEqualArea(1, 0, 0, 0, 1);
188 
189  // Max precision = 10: 0.1 nm in distance, 10^-15 deg (= 0.11 nm),
190  // 10^-11 sec (= 0.3 nm).
191  prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
192  std::string s;
193  int retval = 0;
194  std::cout << std::fixed;
195  while (std::getline(*input, s)) {
196  try {
197  std::string eol("\n");
198  if (!cdelim.empty()) {
199  std::string::size_type m = s.find(cdelim);
200  if (m != std::string::npos) {
201  eol = " " + s.substr(m) + "\n";
202  s = s.substr(0, m);
203  }
204  }
205  std::istringstream str(s);
206  real lat, lon, x, y, gamma, k;
207  std::string stra, strb;
208  if (!(str >> stra >> strb))
209  throw GeographicErr("Incomplete input: " + s);
210  if (reverse) {
211  x = Utility::num<real>(stra);
212  y = Utility::num<real>(strb);
213  } else
214  DMS::DecodeLatLon(stra, strb, lat, lon);
215  std::string strc;
216  if (str >> strc)
217  throw GeographicErr("Extraneous input: " + strc);
218  if (reverse) {
219  if (lcc)
220  lproj.Reverse(lon0, x, y, lat, lon, gamma, k);
221  else
222  aproj.Reverse(lon0, x, y, lat, lon, gamma, k);
223  *output << Utility::str(lat, prec + 5) << " "
224  << Utility::str(lon, prec + 5) << " "
225  << Utility::str(gamma, prec + 6) << " "
226  << Utility::str(k, prec + 6) << eol;
227  } else {
228  if (lcc)
229  lproj.Forward(lon0, lat, lon, x, y, gamma, k);
230  else
231  aproj.Forward(lon0, lat, lon, x, y, gamma, k);
232  *output << Utility::str(x, prec) << " "
233  << Utility::str(y, prec) << " "
234  << Utility::str(gamma, prec + 6) << " "
235  << Utility::str(k, prec + 6) << eol;
236  }
237  }
238  catch (const std::exception& e) {
239  *output << "ERROR: " << e.what() << "\n";
240  retval = 1;
241  }
242  }
243  return retval;
244  }
245  catch (const std::exception& e) {
246  std::cerr << "Caught exception: " << e.what() << "\n";
247  return 1;
248  }
249  catch (...) {
250  std::cerr << "Caught unknown exception\n";
251  return 1;
252  }
253 }
static T AngNormalize(T x)
Definition: Math.hpp:400
void Forward(real lon0, real lat, real lon, real &x, real &y, real &gamma, real &k) const
void Reverse(real lon0, real x, real y, real &lat, real &lon, real &gamma, real &k) const
GeographicLib::Math::real real
Definition: GeodSolve.cpp:32
Header for GeographicLib::Utility class.
Lambert conformal conic projection.
static int extra_digits()
Definition: Math.hpp:185
Header for GeographicLib::AlbersEqualArea class.
Albers equal area conic projection.
Header for GeographicLib::LambertConformalConic class.
static void DecodeLatLon(const std::string &dmsa, const std::string &dmsb, real &lat, real &lon, bool swaplatlong=false)
Definition: DMS.cpp:214
static Math::real Decode(const std::string &dms, flag &ind)
Definition: DMS.cpp:28
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
static std::string str(T x, int p=-1)
Definition: Utility.hpp:276
int main(int argc, char *argv[])
Definition: ConicProj.cpp:30
static int set_digits(int ndigits=0)
Definition: Utility.cpp:48
Exception handling for GeographicLib.
Definition: Constants.hpp:361
void Forward(real lon0, real lat, real lon, real &x, real &y, real &gamma, real &k) const
void Reverse(real lon0, real x, real y, real &lat, real &lon, real &gamma, real &k) const
Header for GeographicLib::DMS class.