GeographicLib 2.4

Loading...
Searching...
No Matches
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 potentially uninitialized local variables
25# pragma warning (disable: 4701)
26#endif
27
28#include "GeodesicProj.usage"
29
30 int main(int argc, const char* const argv[]) {
31 try {
32 using namespace GeographicLib;
33 typedef Math::real real;
34 Utility::set_digits();
35 bool azimuthal = false, cassini = false, gnomonic = false, reverse = false,
36 longfirst = false;
37 real lat0 = 0, lon0 = 0;
38 real
39 a = Constants::WGS84_a(),
40 f = Constants::WGS84_f();
41 int prec = 6;
42 std::string istring, ifile, ofile, cdelim;
43 char lsep = ';';
44
45 for (int m = 1; m < argc; ++m) {
46 std::string arg(argv[m]);
47 if (arg == "-r")
48 reverse = true;
49 else if (arg == "-c" || arg == "-z" || arg == "-g") {
50 cassini = azimuthal = gnomonic = false;
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, longfirst);
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::val<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 == "-w")
77 longfirst = !longfirst;
78 else if (arg == "-p") {
79 if (++m == argc) return usage(1, true);
80 try {
81 prec = Utility::val<int>(std::string(argv[m]));
82 }
83 catch (const std::exception&) {
84 std::cerr << "Precision " << argv[m] << " is not a number\n";
85 return 1;
86 }
87 } else if (arg == "--input-string") {
88 if (++m == argc) return usage(1, true);
89 istring = argv[m];
90 } else if (arg == "--input-file") {
91 if (++m == argc) return usage(1, true);
92 ifile = argv[m];
93 } else if (arg == "--output-file") {
94 if (++m == argc) return usage(1, true);
95 ofile = argv[m];
96 } else if (arg == "--line-separator") {
97 if (++m == argc) return usage(1, true);
98 if (std::string(argv[m]).size() != 1) {
99 std::cerr << "Line separator must be a single character\n";
100 return 1;
101 }
102 lsep = argv[m][0];
103 } else if (arg == "--comment-delimiter") {
104 if (++m == argc) return usage(1, true);
105 cdelim = argv[m];
106 } else if (arg == "--version") {
107 std::cout << argv[0] << ": GeographicLib version "
108 << GEOGRAPHICLIB_VERSION_STRING << "\n";
109 return 0;
110 } else
111 return usage(!(arg == "-h" || arg == "--help"), arg != "--help");
112 }
113
114 if (!ifile.empty() && !istring.empty()) {
115 std::cerr << "Cannot specify --input-string and --input-file together\n";
116 return 1;
117 }
118 if (ifile == "-") ifile.clear();
119 std::ifstream infile;
120 std::istringstream instring;
121 if (!ifile.empty()) {
122 infile.open(ifile.c_str());
123 if (!infile.is_open()) {
124 std::cerr << "Cannot open " << ifile << " for reading\n";
125 return 1;
126 }
127 } else if (!istring.empty()) {
128 std::string::size_type m = 0;
129 while (true) {
130 m = istring.find(lsep, m);
131 if (m == std::string::npos)
132 break;
133 istring[m] = '\n';
134 }
135 instring.str(istring);
136 }
137 std::istream* input = !ifile.empty() ? &infile :
138 (!istring.empty() ? &instring : &std::cin);
139
140 std::ofstream outfile;
141 if (ofile == "-") ofile.clear();
142 if (!ofile.empty()) {
143 outfile.open(ofile.c_str());
144 if (!outfile.is_open()) {
145 std::cerr << "Cannot open " << ofile << " for writing\n";
146 return 1;
147 }
148 }
149 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
150
151 if (!(azimuthal || cassini || gnomonic)) {
152 std::cerr << "Must specify \"-z lat0 lon0\" or "
153 << "\"-c lat0 lon0\" or \"-g lat0 lon0\"\n";
154 return 1;
155 }
156
157 const Geodesic geod(a, f);
158 const CassiniSoldner cs = cassini ?
159 CassiniSoldner(lat0, lon0, geod) : CassiniSoldner(geod);
160 const AzimuthalEquidistant az(geod);
161 const Gnomonic gn(geod);
162
163 // Max precision = 10: 0.1 nm in distance, 10^-15 deg (= 0.11 nm),
164 // 10^-11 sec (= 0.3 nm).
165 prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
166 std::string s, eol, stra, strb, strc;
167 std::istringstream str;
168 int retval = 0;
169 std::cout << std::fixed;
170 while (std::getline(*input, s)) {
171 try {
172 eol = "\n";
173 if (!cdelim.empty()) {
174 std::string::size_type m = s.find(cdelim);
175 if (m != std::string::npos) {
176 eol = " " + s.substr(m) + "\n";
177 s = s.substr(0, m);
178 }
179 }
180 str.clear(); str.str(s);
181 real lat, lon, x, y, azi, rk;
182 if (!(str >> stra >> strb))
183 throw GeographicErr("Incomplete input: " + s);
184 if (reverse) {
185 x = Utility::val<real>(stra);
186 y = Utility::val<real>(strb);
187 } else
188 DMS::DecodeLatLon(stra, strb, lat, lon, longfirst);
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(longfirst ? lon : lat, prec + 5) << " "
199 << Utility::str(longfirst ? lat : 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}
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:28
main
int main(int argc, const char *const argv[])
Definition GeodesicProj.cpp:30
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:79
GeographicLib::CassiniSoldner::Forward
void Forward(real lat, real lon, real &x, real &y, real &azi, real &rk) const
Definition CassiniSoldner.cpp:34
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:29
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:46
GeographicLib::Math::real
double real
Definition Math.hpp:110
GeographicLib
Namespace for GeographicLib.
Definition Accumulator.cpp:12