GeographicLib 2.3

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