GeographicLib  1.35
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;
42  bool lcc = false, albers = false, reverse = false;
43  real lat1 = 0, lat2 = 0, lon0 = 0, k1 = 1;
44  real
45  a = Constants::WGS84_a<real>(),
46  f = Constants::WGS84_f<real>();
47  std::string istring, ifile, ofile, cdelim;
48  char lsep = ';';
49 
50  for (int m = 1; m < argc; ++m) {
51  std::string arg(argv[m]);
52  if (arg == "-r")
53  reverse = true;
54  else if (arg == "-c" || arg == "-a") {
55  lcc = arg == "-c";
56  albers = arg == "-a";
57  if (m + 2 >= argc) return usage(1, true);
58  try {
59  for (int i = 0; i < 2; ++i) {
60  DMS::flag ind;
61  (i ? lat2 : lat1) = DMS::Decode(std::string(argv[++m]), ind);
62  if (ind == DMS::LONGITUDE)
63  throw GeographicErr("Bad hemisphere");
64  }
65  }
66  catch (const std::exception& e) {
67  std::cerr << "Error decoding arguments of " << arg << ": "
68  << e.what() << "\n";
69  return 1;
70  }
71  } else if (arg == "-l") {
72  if (++m == argc) return usage(1, true);
73  try {
74  DMS::flag ind;
75  lon0 = DMS::Decode(std::string(argv[m]), ind);
76  if (ind == DMS::LATITUDE)
77  throw GeographicErr("Bad hemisphere");
78  if (!(lon0 >= -540 && lon0 < 540))
79  throw GeographicErr("Bad longitude");
80  lon0 = Math::AngNormalize(lon0);
81  }
82  catch (const std::exception& e) {
83  std::cerr << "Error decoding argument of " << arg << ": "
84  << e.what() << "\n";
85  return 1;
86  }
87  } else if (arg == "-k") {
88  if (++m == argc) return usage(1, true);
89  try {
90  k1 = Utility::num<real>(std::string(argv[m]));
91  }
92  catch (const std::exception& e) {
93  std::cerr << "Error decoding argument of " << arg << ": "
94  << e.what() << "\n";
95  return 1;
96  }
97  } else if (arg == "-e") {
98  if (m + 2 >= argc) return usage(1, true);
99  try {
100  a = Utility::num<real>(std::string(argv[m + 1]));
101  f = Utility::fract<real>(std::string(argv[m + 2]));
102  }
103  catch (const std::exception& e) {
104  std::cerr << "Error decoding arguments of -e: " << e.what() << "\n";
105  return 1;
106  }
107  m += 2;
108  } else if (arg == "--input-string") {
109  if (++m == argc) return usage(1, true);
110  istring = argv[m];
111  } else if (arg == "--input-file") {
112  if (++m == argc) return usage(1, true);
113  ifile = argv[m];
114  } else if (arg == "--output-file") {
115  if (++m == argc) return usage(1, true);
116  ofile = argv[m];
117  } else if (arg == "--line-separator") {
118  if (++m == argc) return usage(1, true);
119  if (std::string(argv[m]).size() != 1) {
120  std::cerr << "Line separator must be a single character\n";
121  return 1;
122  }
123  lsep = argv[m][0];
124  } else if (arg == "--comment-delimiter") {
125  if (++m == argc) return usage(1, true);
126  cdelim = argv[m];
127  } else if (arg == "--version") {
128  std::cout
129  << argv[0] << ": GeographicLib version "
130  << GEOGRAPHICLIB_VERSION_STRING << "\n";
131  return 0;
132  } else
133  return usage(!(arg == "-h" || arg == "--help"), arg != "--help");
134  }
135 
136  if (!ifile.empty() && !istring.empty()) {
137  std::cerr << "Cannot specify --input-string and --input-file together\n";
138  return 1;
139  }
140  if (ifile == "-") ifile.clear();
141  std::ifstream infile;
142  std::istringstream instring;
143  if (!ifile.empty()) {
144  infile.open(ifile.c_str());
145  if (!infile.is_open()) {
146  std::cerr << "Cannot open " << ifile << " for reading\n";
147  return 1;
148  }
149  } else if (!istring.empty()) {
150  std::string::size_type m = 0;
151  while (true) {
152  m = istring.find(lsep, m);
153  if (m == std::string::npos)
154  break;
155  istring[m] = '\n';
156  }
157  instring.str(istring);
158  }
159  std::istream* input = !ifile.empty() ? &infile :
160  (!istring.empty() ? &instring : &std::cin);
161 
162  std::ofstream outfile;
163  if (ofile == "-") ofile.clear();
164  if (!ofile.empty()) {
165  outfile.open(ofile.c_str());
166  if (!outfile.is_open()) {
167  std::cerr << "Cannot open " << ofile << " for writing\n";
168  return 1;
169  }
170  }
171  std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
172 
173  if (!(lcc || albers)) {
174  std::cerr << "Must specify \"-c lat1 lat2\" or "
175  << "\"-a lat1 lat2\"\n";
176  return 1;
177  }
178 
179  const LambertConformalConic lproj =
180  lcc ? LambertConformalConic(a, f, lat1, lat2, k1)
181  : LambertConformalConic(1, 0, 0, 0, 1);
182  const AlbersEqualArea aproj =
183  albers ? AlbersEqualArea(a, f, lat1, lat2, k1)
184  : AlbersEqualArea(1, 0, 0, 0, 1);
185 
186  std::string s;
187  int retval = 0;
188  std::cout << std::fixed;
189  while (std::getline(*input, s)) {
190  try {
191  std::string eol("\n");
192  if (!cdelim.empty()) {
193  std::string::size_type m = s.find(cdelim);
194  if (m != std::string::npos) {
195  eol = " " + s.substr(m) + "\n";
196  s = s.substr(0, m);
197  }
198  }
199  std::istringstream str(s);
200  real lat, lon, x, y, gamma, k;
201  std::string stra, strb;
202  if (!(str >> stra >> strb))
203  throw GeographicErr("Incomplete input: " + s);
204  if (reverse) {
205  x = Utility::num<real>(stra);
206  y = Utility::num<real>(strb);
207  } else
208  DMS::DecodeLatLon(stra, strb, lat, lon);
209  std::string strc;
210  if (str >> strc)
211  throw GeographicErr("Extraneous input: " + strc);
212  if (reverse) {
213  if (lcc)
214  lproj.Reverse(lon0, x, y, lat, lon, gamma, k);
215  else
216  aproj.Reverse(lon0, x, y, lat, lon, gamma, k);
217  *output << Utility::str<real>(lat, 15) << " "
218  << Utility::str<real>(lon, 15) << " "
219  << Utility::str<real>(gamma, 16) << " "
220  << Utility::str<real>(k, 16) << eol;
221  } else {
222  if (lcc)
223  lproj.Forward(lon0, lat, lon, x, y, gamma, k);
224  else
225  aproj.Forward(lon0, lat, lon, x, y, gamma, k);
226  *output << Utility::str<real>(x, 10) << " "
227  << Utility::str<real>(y, 10) << " "
228  << Utility::str<real>(gamma, 16) << " "
229  << Utility::str<real>(k, 16) << eol;
230  }
231  }
232  catch (const std::exception& e) {
233  *output << "ERROR: " << e.what() << "\n";
234  retval = 1;
235  }
236  }
237  return retval;
238  }
239  catch (const std::exception& e) {
240  std::cerr << "Caught exception: " << e.what() << "\n";
241  return 1;
242  }
243  catch (...) {
244  std::cerr << "Caught unknown exception\n";
245  return 1;
246  }
247 }
static T AngNormalize(T x)
Definition: Math.hpp:388
GeographicLib::Math::real real
Definition: GeodSolve.cpp:40
Header for GeographicLib::Utility class.
Lambert conformal conic projection.
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
int main(int argc, char *argv[])
Definition: ConicProj.cpp:38
Exception handling for GeographicLib.
Definition: Constants.hpp:320
Header for GeographicLib::DMS class.