GeographicLib  1.40
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GeodesicProj.cpp
Go to the documentation of this file.
1 /**
2  * \file GeodesicProj.cpp
3  * \brief Command line utility for geodesic 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="GeodesicProj.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>
21 #include <GeographicLib/DMS.hpp>
23 
24 #if defined(_MSC_VER)
25 // Squelch warnings about constant conditional expressions and potentially
26 // uninitialized local variables
27 # pragma warning (disable: 4127 4701)
28 #endif
29 
30 #include "GeodesicProj.usage"
31 
32 int main(int argc, char* argv[]) {
33  try {
34  using namespace GeographicLib;
35  typedef Math::real real;
37  bool azimuthal = false, cassini = false, gnomonic = false, reverse = false;
38  real lat0 = 0, lon0 = 0;
39  real
40  a = Constants::WGS84_a(),
41  f = Constants::WGS84_f();
42  int prec = 6;
43  std::string istring, ifile, ofile, cdelim;
44  char lsep = ';';
45 
46  for (int m = 1; m < argc; ++m) {
47  std::string arg(argv[m]);
48  if (arg == "-r")
49  reverse = true;
50  else if (arg == "-c" || arg == "-z" || arg == "-g") {
51  cassini = arg == "-c";
52  azimuthal = arg == "-z";
53  gnomonic = arg == "-g";
54  if (m + 2 >= argc) return usage(1, true);
55  try {
56  DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),
57  lat0, lon0);
58  }
59  catch (const std::exception& e) {
60  std::cerr << "Error decoding arguments of " << arg << ": "
61  << e.what() << "\n";
62  return 1;
63  }
64  m += 2;
65  } else if (arg == "-e") {
66  if (m + 2 >= argc) return usage(1, true);
67  try {
68  a = Utility::num<real>(std::string(argv[m + 1]));
69  f = Utility::fract<real>(std::string(argv[m + 2]));
70  }
71  catch (const std::exception& e) {
72  std::cerr << "Error decoding arguments of -e: " << e.what() << "\n";
73  return 1;
74  }
75  m += 2;
76  } else if (arg == "-p") {
77  if (++m == argc) return usage(1, true);
78  try {
79  prec = Utility::num<int>(std::string(argv[m]));
80  }
81  catch (const std::exception&) {
82  std::cerr << "Precision " << argv[m] << " is not a number\n";
83  return 1;
84  }
85  } else if (arg == "--input-string") {
86  if (++m == argc) return usage(1, true);
87  istring = argv[m];
88  } else if (arg == "--input-file") {
89  if (++m == argc) return usage(1, true);
90  ifile = argv[m];
91  } else if (arg == "--output-file") {
92  if (++m == argc) return usage(1, true);
93  ofile = argv[m];
94  } else if (arg == "--line-separator") {
95  if (++m == argc) return usage(1, true);
96  if (std::string(argv[m]).size() != 1) {
97  std::cerr << "Line separator must be a single character\n";
98  return 1;
99  }
100  lsep = argv[m][0];
101  } else if (arg == "--comment-delimiter") {
102  if (++m == argc) return usage(1, true);
103  cdelim = argv[m];
104  } else if (arg == "--version") {
105  std::cout
106  << argv[0] << ": GeographicLib version "
107  << GEOGRAPHICLIB_VERSION_STRING << "\n";
108  return 0;
109  } else
110  return usage(!(arg == "-h" || arg == "--help"), arg != "--help");
111  }
112 
113  if (!ifile.empty() && !istring.empty()) {
114  std::cerr << "Cannot specify --input-string and --input-file together\n";
115  return 1;
116  }
117  if (ifile == "-") ifile.clear();
118  std::ifstream infile;
119  std::istringstream instring;
120  if (!ifile.empty()) {
121  infile.open(ifile.c_str());
122  if (!infile.is_open()) {
123  std::cerr << "Cannot open " << ifile << " for reading\n";
124  return 1;
125  }
126  } else if (!istring.empty()) {
127  std::string::size_type m = 0;
128  while (true) {
129  m = istring.find(lsep, m);
130  if (m == std::string::npos)
131  break;
132  istring[m] = '\n';
133  }
134  instring.str(istring);
135  }
136  std::istream* input = !ifile.empty() ? &infile :
137  (!istring.empty() ? &instring : &std::cin);
138 
139  std::ofstream outfile;
140  if (ofile == "-") ofile.clear();
141  if (!ofile.empty()) {
142  outfile.open(ofile.c_str());
143  if (!outfile.is_open()) {
144  std::cerr << "Cannot open " << ofile << " for writing\n";
145  return 1;
146  }
147  }
148  std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
149 
150  if (!(azimuthal || cassini || gnomonic)) {
151  std::cerr << "Must specify \"-z lat0 lon0\" or "
152  << "\"-c lat0 lon0\" or \"-g lat0 lon0\"\n";
153  return 1;
154  }
155 
156  const Geodesic geod(a, f);
157  const CassiniSoldner cs = cassini ?
158  CassiniSoldner(lat0, lon0, geod) : CassiniSoldner(geod);
159  const AzimuthalEquidistant az(geod);
160  const Gnomonic gn(geod);
161 
162  // Max precision = 10: 0.1 nm in distance, 10^-15 deg (= 0.11 nm),
163  // 10^-11 sec (= 0.3 nm).
164  prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
165  std::string s;
166  int retval = 0;
167  std::cout << std::fixed;
168  while (std::getline(*input, s)) {
169  try {
170  std::string eol("\n");
171  if (!cdelim.empty()) {
172  std::string::size_type m = s.find(cdelim);
173  if (m != std::string::npos) {
174  eol = " " + s.substr(m) + "\n";
175  s = s.substr(0, m);
176  }
177  }
178  std::istringstream str(s);
179  real lat, lon, x, y, azi, rk;
180  std::string stra, strb;
181  if (!(str >> stra >> strb))
182  throw GeographicErr("Incomplete input: " + s);
183  if (reverse) {
184  x = Utility::num<real>(stra);
185  y = Utility::num<real>(strb);
186  } else
187  DMS::DecodeLatLon(stra, strb, lat, lon);
188  std::string strc;
189  if (str >> strc)
190  throw GeographicErr("Extraneous input: " + strc);
191  if (reverse) {
192  if (cassini)
193  cs.Reverse(x, y, lat, lon, azi, rk);
194  else if (azimuthal)
195  az.Reverse(lat0, lon0, x, y, lat, lon, azi, rk);
196  else
197  gn.Reverse(lat0, lon0, x, y, lat, lon, azi, rk);
198  *output << Utility::str(lat, prec + 5) << " "
199  << Utility::str(lon, prec + 5) << " "
200  << Utility::str(azi, prec + 5) << " "
201  << Utility::str(rk, prec + 6) << eol;
202  } else {
203  if (cassini)
204  cs.Forward(lat, lon, x, y, azi, rk);
205  else if (azimuthal)
206  az.Forward(lat0, lon0, lat, lon, x, y, azi, rk);
207  else
208  gn.Forward(lat0, lon0, lat, lon, x, y, azi, rk);
209  *output << Utility::str(x, prec) << " "
210  << Utility::str(y, prec) << " "
211  << Utility::str(azi, prec + 5) << " "
212  << Utility::str(rk, prec + 6) << eol;
213  }
214  }
215  catch (const std::exception& e) {
216  *output << "ERROR: " << e.what() << "\n";
217  retval = 1;
218  }
219  }
220  return retval;
221  }
222  catch (const std::exception& e) {
223  std::cerr << "Caught exception: " << e.what() << "\n";
224  return 1;
225  }
226  catch (...) {
227  std::cerr << "Caught unknown exception\n";
228  return 1;
229  }
230 }
Header for GeographicLib::CassiniSoldner class.
GeographicLib::Math::real real
Definition: GeodSolve.cpp:32
void Reverse(real lat0, real lon0, real x, real y, real &lat, real &lon, real &azi, real &rk) const
Definition: Gnomonic.cpp:48
int main(int argc, char *argv[])
Header for GeographicLib::Utility class.
void Forward(real lat, real lon, real &x, real &y, real &azi, real &rk) const
Gnomonic projection
Definition: Gnomonic.hpp:101
Cassini-Soldner projection.
void Reverse(real lat0, real lon0, real x, real y, real &lat, real &lon, real &azi, real &rk) const
Header for GeographicLib::Gnomonic class.
void Reverse(real x, real y, real &lat, real &lon, real &azi, real &rk) const
static int extra_digits()
Definition: Math.hpp:185
Header for GeographicLib::Geodesic class.
Azimuthal equidistant projection.
static void DecodeLatLon(const std::string &dmsa, const std::string &dmsb, real &lat, real &lon, bool swaplatlong=false)
Definition: DMS.cpp:214
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
static std::string str(T x, int p=-1)
Definition: Utility.hpp:276
static int set_digits(int ndigits=0)
Definition: Utility.cpp:48
void Forward(real lat0, real lon0, real lat, real lon, real &x, real &y, real &azi, real &rk) const
Definition: Gnomonic.cpp:29
void Forward(real lat0, real lon0, real lat, real lon, real &x, real &y, real &azi, real &rk) const
Exception handling for GeographicLib.
Definition: Constants.hpp:361
Header for GeographicLib::AzimuthalEquidistant class.
Geodesic calculations
Definition: Geodesic.hpp:171
Header for GeographicLib::DMS class.