GeographicLib 2.3

RhumbSolve.cpp
Go to the documentation of this file.
1/**
2 * \file RhumbSolve.cpp
3 * \brief Command line utility for rhumb line calculations
4 *
5 * Copyright (c) Charles Karney (2014-2023) <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="RhumbSolve.1.html">man page</a> for usage information.
10 **********************************************************************/
11
12#include <iostream>
13#include <string>
14#include <sstream>
15#include <fstream>
16#include <cmath>
17#include <limits>
18#include <GeographicLib/Rhumb.hpp>
19#include <GeographicLib/DMS.hpp>
20#include <GeographicLib/Utility.hpp>
21
22#if defined(_MSC_VER)
23// Squelch warnings about constant conditional expressions and potentially
24// uninitialized local variables
25# pragma warning (disable: 4127 4701)
26#endif
27
28#include "RhumbSolve.usage"
29 using namespace GeographicLib;
30 typedef Math::real real;
31
32std::string LatLonString(real lat, real lon, int prec, bool dms, char dmssep,
33 bool longfirst) {
34 using namespace GeographicLib;
35 std::string
36 latstr = dms ? DMS::Encode(lat, prec + 5, DMS::LATITUDE, dmssep) :
37 DMS::Encode(lat, prec + 5, DMS::NUMBER),
38 lonstr = dms ? DMS::Encode(lon, prec + 5, DMS::LONGITUDE, dmssep) :
39 DMS::Encode(lon, prec + 5, DMS::NUMBER);
40 return
41 (longfirst ? lonstr : latstr) + " " + (longfirst ? latstr : lonstr);
42}
43
44std::string AzimuthString(real azi, int prec, bool dms, char dmssep) {
45 return dms ? DMS::Encode(azi, prec + 5, DMS::AZIMUTH, dmssep) :
46 DMS::Encode(azi, prec + 5, DMS::NUMBER);
47}
48
49 int main(int argc, const char* const argv[]) {
50 try {
51 Utility::set_digits();
52 bool linecalc = false, inverse = false, dms = false, exact = false,
53 unroll = false, longfirst = false;
54 real
55 a = Constants::WGS84_a(),
56 f = Constants::WGS84_f();
57 real lat1, lon1, azi12 = Math::NaN(), lat2, lon2, s12, S12;
58 int prec = 3;
59 std::string istring, ifile, ofile, cdelim;
60 char lsep = ';', dmssep = char(0);
61
62 for (int m = 1; m < argc; ++m) {
63 std::string arg(argv[m]);
64 if (arg == "-i") {
65 inverse = true;
66 linecalc = false;
67 } else if (arg == "-L") {
68 inverse = false;
69 linecalc = true;
70 if (m + 3 >= argc) return usage(1, true);
71 try {
72 DMS::DecodeLatLon(std::string(argv[m + 1]), std::string(argv[m + 2]),
73 lat1, lon1, longfirst);
74 azi12 = DMS::DecodeAzimuth(std::string(argv[m + 3]));
75 }
76 catch (const std::exception& e) {
77 std::cerr << "Error decoding arguments of -L: " << e.what() << "\n";
78 return 1;
79 }
80 m += 3;
81 } else if (arg == "-e") {
82 if (m + 2 >= argc) return usage(1, true);
83 try {
84 a = Utility::val<real>(std::string(argv[m + 1]));
85 f = Utility::fract<real>(std::string(argv[m + 2]));
86 }
87 catch (const std::exception& e) {
88 std::cerr << "Error decoding arguments of -e: " << e.what() << "\n";
89 return 1;
90 }
91 m += 2;
92 } else if (arg == "-u")
93 unroll = true;
94 else if (arg == "-d") {
95 dms = true;
96 dmssep = '\0';
97 } else if (arg == "-:") {
98 dms = true;
99 dmssep = ':';
100 } else if (arg == "-w")
101 longfirst = !longfirst;
102 else if (arg == "-p") {
103 if (++m == argc) return usage(1, true);
104 try {
105 prec = Utility::val<int>(std::string(argv[m]));
106 }
107 catch (const std::exception&) {
108 std::cerr << "Precision " << argv[m] << " is not a number\n";
109 return 1;
110 }
111 } else if (arg == "-E")
112 exact = true;
113 else if (arg == "--input-string") {
114 if (++m == argc) return usage(1, true);
115 istring = argv[m];
116 } else if (arg == "--input-file") {
117 if (++m == argc) return usage(1, true);
118 ifile = argv[m];
119 } else if (arg == "--output-file") {
120 if (++m == argc) return usage(1, true);
121 ofile = argv[m];
122 } else if (arg == "--line-separator") {
123 if (++m == argc) return usage(1, true);
124 if (std::string(argv[m]).size() != 1) {
125 std::cerr << "Line separator must be a single character\n";
126 return 1;
127 }
128 lsep = argv[m][0];
129 } else if (arg == "--comment-delimiter") {
130 if (++m == argc) return usage(1, true);
131 cdelim = argv[m];
132 } else if (arg == "--version") {
133 std::cout << argv[0] << ": GeographicLib version "
134 << GEOGRAPHICLIB_VERSION_STRING << "\n";
135 return 0;
136 } else
137 return usage(!(arg == "-h" || arg == "--help"), arg != "--help");
138 }
139
140 if (!ifile.empty() && !istring.empty()) {
141 std::cerr << "Cannot specify --input-string and --input-file together\n";
142 return 1;
143 }
144 if (ifile == "-") ifile.clear();
145 std::ifstream infile;
146 std::istringstream instring;
147 if (!ifile.empty()) {
148 infile.open(ifile.c_str());
149 if (!infile.is_open()) {
150 std::cerr << "Cannot open " << ifile << " for reading\n";
151 return 1;
152 }
153 } else if (!istring.empty()) {
154 std::string::size_type m = 0;
155 while (true) {
156 m = istring.find(lsep, m);
157 if (m == std::string::npos)
158 break;
159 istring[m] = '\n';
160 }
161 instring.str(istring);
162 }
163 std::istream* input = !ifile.empty() ? &infile :
164 (!istring.empty() ? &instring : &std::cin);
165
166 std::ofstream outfile;
167 if (ofile == "-") ofile.clear();
168 if (!ofile.empty()) {
169 outfile.open(ofile.c_str());
170 if (!outfile.is_open()) {
171 std::cerr << "Cannot open " << ofile << " for writing\n";
172 return 1;
173 }
174 }
175 std::ostream* output = !ofile.empty() ? &outfile : &std::cout;
176
177 const Rhumb rh(a, f, exact);
178 const RhumbLine rhl(linecalc ? rh.Line(lat1, lon1, azi12) :
179 rh.Line(0, 0, Math::qd));
180 // Max precision = 10: 0.1 nm in distance, 10^-15 deg (= 0.11 nm),
181 // 10^-11 sec (= 0.3 nm).
182 prec = std::min(10 + Math::extra_digits(), std::max(0, prec));
183 std::string s, eol, slat1, slon1, slat2, slon2, sazi, ss12, strc;
184 std::istringstream str;
185 int retval = 0;
186 while (std::getline(*input, s)) {
187 try {
188 eol = "\n";
189 if (!cdelim.empty()) {
190 std::string::size_type m = s.find(cdelim);
191 if (m != std::string::npos) {
192 eol = " " + s.substr(m) + "\n";
193 s = s.substr(0, m);
194 }
195 }
196 str.clear(); str.str(s);
197 if (linecalc) {
198 if (!(str >> ss12))
199 throw GeographicErr("Incomplete input: " + s);
200 if (str >> strc)
201 throw GeographicErr("Extraneous input: " + strc);
202 s12 = Utility::val<real>(ss12);
203 rhl.GenPosition(s12, Rhumb::ALL | (unroll ? Rhumb::LONG_UNROLL : 0),
204 lat2, lon2, S12);
205 *output << LatLonString(lat2, lon2, prec, dms, dmssep, longfirst)
206 << " " << Utility::str(S12, std::max(prec-7, 0)) << eol;
207 } else if (inverse) {
208 if (!(str >> slat1 >> slon1 >> slat2 >> slon2))
209 throw GeographicErr("Incomplete input: " + s);
210 if (str >> strc)
211 throw GeographicErr("Extraneous input: " + strc);
212 DMS::DecodeLatLon(slat1, slon1, lat1, lon1, longfirst);
213 DMS::DecodeLatLon(slat2, slon2, lat2, lon2, longfirst);
214 rh.Inverse(lat1, lon1, lat2, lon2, s12, azi12, S12);
215 *output << AzimuthString(azi12, prec, dms, dmssep) << " "
216 << Utility::str(s12, prec) << " "
217 << Utility::str(S12, std::max(prec-7, 0)) << eol;
218 } else { // direct
219 if (!(str >> slat1 >> slon1 >> sazi >> ss12))
220 throw GeographicErr("Incomplete input: " + s);
221 if (str >> strc)
222 throw GeographicErr("Extraneous input: " + strc);
223 DMS::DecodeLatLon(slat1, slon1, lat1, lon1, longfirst);
224 azi12 = DMS::DecodeAzimuth(sazi);
225 s12 = Utility::val<real>(ss12);
226 rh.GenDirect(lat1, lon1, azi12, s12,
227 Rhumb::ALL | (unroll ? Rhumb::LONG_UNROLL : 0),
228 lat2, lon2, S12);
229 *output << LatLonString(lat2, lon2, prec, dms, dmssep, longfirst)
230 << " " << Utility::str(S12, std::max(prec-7, 0)) << eol;
231 }
232 }
233 catch (const std::exception& e) {
234 // Write error message cout so output lines match input lines
235 *output << "ERROR: " << e.what() << "\n";
236 retval = 1;
237 }
238 }
239 return retval;
240 }
241 catch (const std::exception& e) {
242 std::cerr << "Caught exception: " << e.what() << "\n";
243 return 1;
244 }
245 catch (...) {
246 std::cerr << "Caught unknown exception\n";
247 return 1;
248 }
249}
DMS.hpp
Header for GeographicLib::DMS class.
real
GeographicLib::Math::real real
Definition: GeodSolve.cpp:29
real
Math::real real
Definition: RhumbSolve.cpp:30
main
int main(int argc, const char *const argv[])
Definition: RhumbSolve.cpp:49
AzimuthString
std::string AzimuthString(real azi, int prec, bool dms, char dmssep)
Definition: RhumbSolve.cpp:44
LatLonString
std::string LatLonString(real lat, real lon, int prec, bool dms, char dmssep, bool longfirst)
Definition: RhumbSolve.cpp:32
Rhumb.hpp
Header for GeographicLib::Rhumb and GeographicLib::RhumbLine classes.
Utility.hpp
Header for GeographicLib::Utility class.
GeographicLib::GeographicErr
Exception handling for GeographicLib.
Definition: Constants.hpp:316
GeographicLib::Math::real
double real
Definition: Math.hpp:100
GeographicLib::RhumbLine
Find a sequence of points on a single rhumb line.
Definition: Rhumb.hpp:381
GeographicLib::RhumbLine::GenPosition
void GenPosition(real s12, unsigned outmask, real &lat2, real &lon2, real &S12) const
Definition: Rhumb.cpp:288
GeographicLib::Rhumb
Solve of the direct and inverse rhumb problems.
Definition: Rhumb.hpp:79
GeographicLib::Rhumb::Line
RhumbLine Line(real lat1, real lon1, real azi12) const
Definition: Rhumb.cpp:245
GeographicLib::Rhumb::Inverse
void Inverse(real lat1, real lon1, real lat2, real lon2, real &s12, real &azi12, real &S12) const
Definition: Rhumb.hpp:267
GeographicLib
Namespace for GeographicLib.
Definition: Accumulator.cpp:12