GeographicLib  1.43
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 << argv[0] << ": GeographicLib version "
106  << GEOGRAPHICLIB_VERSION_STRING << "\n";
107  return 0;
108  } else
109  return usage(!(arg == "-h" || arg == "--help"), arg != "--help");
110  }
111 
112  if (!ifile.empty() && !istring.empty()) {
113  std::cerr << "Cannot specify --input-string and --input-file together\n";
114  return 1;
115  }
116  if (ifile == "-") ifile.clear();
117  std::ifstream infile;
118  std::istringstream instring;
119  if (!ifile.empty()) {
120  infile.open(ifile.c_str());
121  if (!infile.is_open()) {
122  std::cerr << "Cannot open " << ifile << " for reading\n";
123  return 1;
124  }
125  } else if (!istring.empty()) {
126  std::string::size_type m = 0;
127  while (true) {
128  m = istring.find(lsep, m);
129  if (m == std::string::npos)
130  break;
131  istring[m] = '\n';
132  }
133  instring.str(istring);
134  }
135  std::istream* input = !ifile.empty() ? &infile :
136  (!istring.empty() ? &instring : &std::cin);
137 
138  std::ofstream outfile;
139  if (ofile == "-") ofile.clear();
140  if (!ofile.empty()) {
141  outfile.open(ofile.c_str());
142  if (!outfile.is_open()) {
143  std::cerr << "Cannot open " << ofile << " for writing\n";
144  return 1;
145  }
146  }
147  std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
148 
149  if (!(azimuthal || cassini || gnomonic)) {
150  std::cerr << "Must specify \"-z lat0 lon0\" or "
151  << "\"-c lat0 lon0\" or \"-g lat0 lon0\"\n";
152  return 1;
153  }
154 
155  const Geodesic geod(a, f);
156  const CassiniSoldner cs = cassini ?
157  CassiniSoldner(lat0, lon0, geod) : CassiniSoldner(geod);
158  const AzimuthalEquidistant az(geod);
159  const Gnomonic gn(geod);
160 
161  // Max precision = 10: 0.1 nm in distance, 10^-15 deg (= 0.11 nm),
162  // 10^-11 sec (= 0.3 nm).
163  prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
164  std::string s;
165  int retval = 0;
166  std::cout << std::fixed;
167  while (std::getline(*input, s)) {
168  try {
169  std::string eol("\n");
170  if (!cdelim.empty()) {
171  std::string::size_type m = s.find(cdelim);
172  if (m != std::string::npos) {
173  eol = " " + s.substr(m) + "\n";
174  s = s.substr(0, m);
175  }
176  }
177  std::istringstream str(s);
178  real lat, lon, x, y, azi, rk;
179  std::string stra, strb;
180  if (!(str >> stra >> strb))
181  throw GeographicErr("Incomplete input: " + s);
182  if (reverse) {
183  x = Utility::num<real>(stra);
184  y = Utility::num<real>(strb);
185  } else
186  DMS::DecodeLatLon(stra, strb, lat, lon);
187  std::string strc;
188  if (str >> strc)
189  throw GeographicErr("Extraneous input: " + strc);
190  if (reverse) {
191  if (cassini)
192  cs.Reverse(x, y, lat, lon, azi, rk);
193  else if (azimuthal)
194  az.Reverse(lat0, lon0, x, y, lat, lon, azi, rk);
195  else
196  gn.Reverse(lat0, lon0, x, y, lat, lon, azi, rk);
197  *output << Utility::str(lat, prec + 5) << " "
198  << Utility::str(lon, prec + 5) << " "
199  << Utility::str(azi, prec + 5) << " "
200  << Utility::str(rk, prec + 6) << eol;
201  } else {
202  if (cassini)
203  cs.Forward(lat, lon, x, y, azi, rk);
204  else if (azimuthal)
205  az.Forward(lat0, lon0, lat, lon, x, y, azi, rk);
206  else
207  gn.Forward(lat0, lon0, lat, lon, x, y, azi, rk);
208  *output << Utility::str(x, prec) << " "
209  << Utility::str(y, prec) << " "
210  << Utility::str(azi, prec + 5) << " "
211  << Utility::str(rk, prec + 6) << eol;
212  }
213  }
214  catch (const std::exception& e) {
215  *output << "ERROR: " << e.what() << "\n";
216  retval = 1;
217  }
218  }
219  return retval;
220  }
221  catch (const std::exception& e) {
222  std::cerr << "Caught exception: " << e.what() << "\n";
223  return 1;
224  }
225  catch (...) {
226  std::cerr << "Caught unknown exception\n";
227  return 1;
228  }
229 }
void Forward(real lat, real lon, real &x, real &y, real &azi, real &rk) const
Header for GeographicLib::CassiniSoldner class.
GeographicLib::Math::real real
Definition: GeodSolve.cpp:32
int main(int argc, char *argv[])
Header for GeographicLib::Utility class.
Gnomonic projection
Definition: Gnomonic.hpp:101
void Forward(real lat0, real lon0, real lat, real lon, real &x, real &y, real &azi, real &rk) const
Cassini-Soldner projection.
Header for GeographicLib::Gnomonic class.
static int extra_digits()
Definition: Math.hpp:185
Header for GeographicLib::Geodesic class.
Azimuthal equidistant projection.
void Forward(real lat0, real lon0, real lat, real lon, real &x, real &y, real &azi, real &rk) const
Definition: Gnomonic.cpp:29
static void DecodeLatLon(const std::string &dmsa, const std::string &dmsb, real &lat, real &lon, bool swaplatlong=false)
Definition: DMS.cpp:246
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
void Reverse(real x, real y, real &lat, real &lon, real &azi, real &rk) const
void Reverse(real lat0, real lon0, real x, real y, real &lat, real &lon, real &azi, real &rk) const
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 Reverse(real lat0, real lon0, real x, real y, real &lat, real &lon, real &azi, real &rk) const
Definition: Gnomonic.cpp:48
Exception handling for GeographicLib.
Definition: Constants.hpp:382
Header for GeographicLib::AzimuthalEquidistant class.
Geodesic calculations
Definition: Geodesic.hpp:171
Header for GeographicLib::DMS class.