GeographicLib  1.43
 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 << argv[0] << ": GeographicLib version "
132  << GEOGRAPHICLIB_VERSION_STRING << "\n";
133  return 0;
134  } else
135  return usage(!(arg == "-h" || arg == "--help"), arg != "--help");
136  }
137 
138  if (!ifile.empty() && !istring.empty()) {
139  std::cerr << "Cannot specify --input-string and --input-file together\n";
140  return 1;
141  }
142  if (ifile == "-") ifile.clear();
143  std::ifstream infile;
144  std::istringstream instring;
145  if (!ifile.empty()) {
146  infile.open(ifile.c_str());
147  if (!infile.is_open()) {
148  std::cerr << "Cannot open " << ifile << " for reading\n";
149  return 1;
150  }
151  } else if (!istring.empty()) {
152  std::string::size_type m = 0;
153  while (true) {
154  m = istring.find(lsep, m);
155  if (m == std::string::npos)
156  break;
157  istring[m] = '\n';
158  }
159  instring.str(istring);
160  }
161  std::istream* input = !ifile.empty() ? &infile :
162  (!istring.empty() ? &instring : &std::cin);
163 
164  std::ofstream outfile;
165  if (ofile == "-") ofile.clear();
166  if (!ofile.empty()) {
167  outfile.open(ofile.c_str());
168  if (!outfile.is_open()) {
169  std::cerr << "Cannot open " << ofile << " for writing\n";
170  return 1;
171  }
172  }
173  std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
174 
175  if (!(lcc || albers)) {
176  std::cerr << "Must specify \"-c lat1 lat2\" or "
177  << "\"-a lat1 lat2\"\n";
178  return 1;
179  }
180 
181  const LambertConformalConic lproj =
182  lcc ? LambertConformalConic(a, f, lat1, lat2, k1)
183  : LambertConformalConic(1, 0, 0, 0, 1);
184  const AlbersEqualArea aproj =
185  albers ? AlbersEqualArea(a, f, lat1, lat2, k1)
186  : AlbersEqualArea(1, 0, 0, 0, 1);
187 
188  // Max precision = 10: 0.1 nm in distance, 10^-15 deg (= 0.11 nm),
189  // 10^-11 sec (= 0.3 nm).
190  prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
191  std::string s;
192  int retval = 0;
193  std::cout << std::fixed;
194  while (std::getline(*input, s)) {
195  try {
196  std::string eol("\n");
197  if (!cdelim.empty()) {
198  std::string::size_type m = s.find(cdelim);
199  if (m != std::string::npos) {
200  eol = " " + s.substr(m) + "\n";
201  s = s.substr(0, m);
202  }
203  }
204  std::istringstream str(s);
205  real lat, lon, x, y, gamma, k;
206  std::string stra, strb;
207  if (!(str >> stra >> strb))
208  throw GeographicErr("Incomplete input: " + s);
209  if (reverse) {
210  x = Utility::num<real>(stra);
211  y = Utility::num<real>(strb);
212  } else
213  DMS::DecodeLatLon(stra, strb, lat, lon);
214  std::string strc;
215  if (str >> strc)
216  throw GeographicErr("Extraneous input: " + strc);
217  if (reverse) {
218  if (lcc)
219  lproj.Reverse(lon0, x, y, lat, lon, gamma, k);
220  else
221  aproj.Reverse(lon0, x, y, lat, lon, gamma, k);
222  *output << Utility::str(lat, prec + 5) << " "
223  << Utility::str(lon, prec + 5) << " "
224  << Utility::str(gamma, prec + 6) << " "
225  << Utility::str(k, prec + 6) << eol;
226  } else {
227  if (lcc)
228  lproj.Forward(lon0, lat, lon, x, y, gamma, k);
229  else
230  aproj.Forward(lon0, lat, lon, x, y, gamma, k);
231  *output << Utility::str(x, prec) << " "
232  << Utility::str(y, prec) << " "
233  << Utility::str(gamma, prec + 6) << " "
234  << Utility::str(k, prec + 6) << eol;
235  }
236  }
237  catch (const std::exception& e) {
238  *output << "ERROR: " << e.what() << "\n";
239  retval = 1;
240  }
241  }
242  return retval;
243  }
244  catch (const std::exception& e) {
245  std::cerr << "Caught exception: " << e.what() << "\n";
246  return 1;
247  }
248  catch (...) {
249  std::cerr << "Caught unknown exception\n";
250  return 1;
251  }
252 }
static T AngNormalize(T x)
Definition: Math.hpp:445
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:246
static Math::real Decode(const std::string &dms, flag &ind)
Definition: DMS.cpp:28
static std::string str(T x, int p=-1)
Definition: Utility.hpp:276
int main(int argc, char *argv[])
Definition: CartConvert.cpp:30
static int set_digits(int ndigits=0)
Definition: Utility.cpp:48
Exception handling for GeographicLib.
Definition: Constants.hpp:382
Header for GeographicLib::DMS class.