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