GeographicLib  1.35
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;
47  bool azimuthal = false, cassini = false, gnomonic = false, reverse = false;
48  real lat0 = 0, lon0 = 0;
49  real
50  a = Constants::WGS84_a<real>(),
51  f = Constants::WGS84_f<real>();
52  std::string istring, ifile, ofile, cdelim;
53  char lsep = ';';
54 
55  for (int m = 1; m < argc; ++m) {
56  std::string arg(argv[m]);
57  if (arg == "-r")
58  reverse = true;
59  else if (arg == "-c" || arg == "-z" || arg == "-g") {
60  cassini = arg == "-c";
61  azimuthal = arg == "-z";
62  gnomonic = arg == "-g";
63  if (m + 2 >= argc) return usage(1, true);
64  try {
65  DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),
66  lat0, lon0);
67  }
68  catch (const std::exception& e) {
69  std::cerr << "Error decoding arguments of " << arg << ": "
70  << e.what() << "\n";
71  return 1;
72  }
73  m += 2;
74  } else if (arg == "-e") {
75  if (m + 2 >= argc) return usage(1, true);
76  try {
77  a = Utility::num<real>(std::string(argv[m + 1]));
78  f = Utility::fract<real>(std::string(argv[m + 2]));
79  }
80  catch (const std::exception& e) {
81  std::cerr << "Error decoding arguments of -e: " << e.what() << "\n";
82  return 1;
83  }
84  m += 2;
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  std::string s;
163  int retval = 0;
164  std::cout << std::fixed;
165  while (std::getline(*input, s)) {
166  try {
167  std::string eol("\n");
168  if (!cdelim.empty()) {
169  std::string::size_type m = s.find(cdelim);
170  if (m != std::string::npos) {
171  eol = " " + s.substr(m) + "\n";
172  s = s.substr(0, m);
173  }
174  }
175  std::istringstream str(s);
176  real lat, lon, x, y, azi, rk;
177  std::string stra, strb;
178  if (!(str >> stra >> strb))
179  throw GeographicErr("Incomplete input: " + s);
180  if (reverse) {
181  x = Utility::num<real>(stra);
182  y = Utility::num<real>(strb);
183  } else
184  DMS::DecodeLatLon(stra, strb, lat, lon);
185  std::string strc;
186  if (str >> strc)
187  throw GeographicErr("Extraneous input: " + strc);
188  if (reverse) {
189  if (cassini)
190  cs.Reverse(x, y, lat, lon, azi, rk);
191  else if (azimuthal)
192  az.Reverse(lat0, lon0, x, y, lat, lon, azi, rk);
193  else
194  gn.Reverse(lat0, lon0, x, y, lat, lon, azi, rk);
195  *output << Utility::str<real>(lat, 15) << " "
196  << Utility::str<real>(lon, 15) << " "
197  << Utility::str<real>(azi, 15) << " "
198  << Utility::str<real>(rk, 16) << eol;
199  } else {
200  if (cassini)
201  cs.Forward(lat, lon, x, y, azi, rk);
202  else if (azimuthal)
203  az.Forward(lat0, lon0, lat, lon, x, y, azi, rk);
204  else
205  gn.Forward(lat0, lon0, lat, lon, x, y, azi, rk);
206  *output << Utility::str<real>(x, 10) << " "
207  << Utility::str<real>(y, 10) << " "
208  << Utility::str<real>(azi, 15) << " "
209  << Utility::str<real>(rk, 16) << eol;
210  }
211  }
212  catch (const std::exception& e) {
213  *output << "ERROR: " << e.what() << "\n";
214  retval = 1;
215  }
216  }
217  return retval;
218  }
219  catch (const std::exception& e) {
220  std::cerr << "Caught exception: " << e.what() << "\n";
221  return 1;
222  }
223  catch (...) {
224  std::cerr << "Caught unknown exception\n";
225  return 1;
226  }
227 }
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:43
int main(int argc, char *argv[])
Header for GeographicLib::Utility class.
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.
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
void Forward(real lat0, real lon0, real lat, real lon, real &x, real &y, real &azi, real &rk) const
Definition: Gnomonic.cpp:24
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:320
Header for GeographicLib::AzimuthalEquidistant class.
Geodesic calculations
Definition: Geodesic.hpp:169
Header for GeographicLib::DMS class.