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