GeographicLib 2.4

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